以最后(吞没)的蜡烛低点为止损点

问题描述 投票:0回答:1

我的松树脚本有一个问题。该策略的想法:-简单的趋势跟踪策略-检查是否在上升-下降趋势中-当吞噬蜡烛形成时,我想在下一个蜡烛上进入交易。

问题:我想把我的止损点设在该吞没蜡烛的低点,TP应该是该(前一个)吞没蜡烛高度的1.25倍。

我对这最后一部分感到很困惑。

有谁能帮助我吗?

所有最好的!

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © c4ss10p314

//@version=4
strategy(title = "3emaFX", shorttitle = "3ema", overlay = true, initial_capital = 100000, process_orders_on_close = false)
price = strategy.position_avg_price

//input variables
hammerBodyHeight = input(title="Hammer Body, Max Height (Pips)",minval=1,maxval=100,step=1,defval=300,confirm=false)
bullEnHeight = input(title="BullEn, Height (Pips)",minval=1,maxval=100,step=1,defval=3,confirm=false)
bullEnAbove = input(title="BullEn Above (Pips)",minval=1,maxval=100,step=1,defval=1,confirm=false)
bullEnBelow = input(title="BullEn, Below (Pips)",minval=1,maxval=100,step=1,defval=0,confirm=false)
bearEnHeight = input(title="BearEn, Height (Pips)",minval=1,maxval=100,step=1,defval=3,confirm=false)
bearEnAbove = input(title="BearEn Above (Pips)",minval=1,maxval=100,step=1,defval=0,confirm=false)
bearEnBelow = input(title="BearEn, Below (Pips)",minval=1,maxval=100,step=1,defval=1,confirm=false)


//Candlestick patterns
invertedHammer = (abs(close - open) / syminfo.mintick <= hammerBodyHeight) and (close > open) and ((high - close) > (close - open)) and ((open - low) <= (close - open) / 2)
hammer = (abs(close - open) / syminfo.mintick <= hammerBodyHeight) and (close > open) and ((close - open) < (open - low)) and ((high - close) <= (close - open) / 2)
bullEngulfing = ((open[1] - close[1]) / syminfo.mintick >= bullEnHeight) and (close > open) and ((close - open[1]) / syminfo.mintick >= bullEnAbove) and ((close[1] - open) / syminfo.mintick >= bullEnBelow)
bearEngulfing = ((close[1] - open[1]) / syminfo.mintick >= bearEnHeight) and (close < open) and ((open - close[1]) / syminfo.mintick >= bearEnAbove) and ((open[1] - close) / syminfo.mintick >= bearEnBelow)
ema8 = ema(close, 8)
ema13 = ema(close, 13)
ema21 = ema(close, 21)

//chart plotters
plotshape(bullEngulfing, style=shape.arrowup, location=location.abovebar, color = color.green, text='BullEng')
plotshape(invertedHammer, style=shape.flag, location=location.abovebar, color = color.green, text='Inv Hammer')
plotshape(hammer, style=shape.flag, location=location.abovebar, color = color.green, text='Hammer')
plotshape(bearEngulfing, style=shape.arrowdown, location=location.belowbar, color = color.red, text='BearEng')
plot(ema8, color = color.blue, linewidth=1)
plot(ema13, color = color.green, linewidth=1)
plot(ema21, color = color.orange, linewidth=1)


//Trade conditions
longEntry = close > ema8 and ema8 > ema13 and bullEngulfing and barstate.isconfirmed
shortEntry = close < ema8 and ema8 < ema13 and bearEngulfing
longExit = close <= ema13


//Risk management variables
// RM - inputs
// position_size = 

clockwork = barssince(valuewhen(bullEngulfing, price, 1)) + 1
EngHeight = abs(open[clockwork] - close[clockwork]) / syminfo.mintick
execProfitTarget = (price + (EngHeight * 1.25))
execStopLoss = (price - EngHeight)

//timeframe
start = timestamp(2019, 6, 3, 0, 0)
end = timestamp(2020, 6, 5 , 0, 0)

//Entry
if time >= start and time <= end
    strategy.entry("Long", strategy.long, strategy.equity[0.01], when = longEntry)
//    strategy.entry("Short", strategy.short, strategy.equity[0.01], when = shortEntry)

// Exit
strategy.exit("Exit Long", from_entry = "Long", profit = execProfitTarget, stop = execStopLoss)
// strategy.close("Close Long", from_entry = "Long", profit = execProfitTarget, loss = execStopLoss)

// Check ob trend die letzten x Kerzen schon trend war
// Line 49: Strategy.equity - andere Lösung (Positionsgröße berechnen)
pine-script trading
1个回答
0
投票

版本1

这应该会让你更接近你的目标。

  • 我们用10%的权益来确定仓位大小。
  • 只有当我们不在交易中时才会触发入场。
  • 止损点指定为价格,而不是ticks。
  • 调试图包含在最后。
  • 修改了日期,以查看更多的交易实例。
// © c4ss10p314

//@version=4
strategy(title = "3emaFXlong", shorttitle = "3emaL", overlay=true, initial_capital = 100000, default_qty_type=strategy.percent_of_equity, default_qty_value = 10)
price = close

//input variables
hammerBodyHeight = input(title="Hammer Body, Max Height (Pips)",minval=1,maxval=100,step=1,defval=300,confirm=false)
bullEnHeight = input(title="BullEn, Height (Pips)",minval=1,maxval=100,step=1,defval=3,confirm=false)
bullEnAbove = input(title="BullEn Above (Pips)",minval=1,maxval=100,step=1,defval=1,confirm=false)
bullEnBelow = input(title="BullEn, Below (Pips)",minval=1,maxval=100,step=1,defval=0,confirm=false)
inpTakeProfit = input(1.25, title = "Example-Take Profit Multiplier", minval = 0.1, maxval = 5, step = 0.1)

//Candlestick patterns
invertedHammer = (abs(close - open) / syminfo.mintick <= hammerBodyHeight) and (close > open) and ((high - close) > (close - open)) and ((open - low) <= (close - open) / 2)
bullEngulfing = ((open[1] - close[1]) / syminfo.mintick >= bullEnHeight) and (close > open) and ((close - open[1]) / syminfo.mintick >= bullEnAbove) and ((close[1] - open) / syminfo.mintick >= bullEnBelow)
ema8 = ema(close, 8)
ema13 = ema(close, 13)
ema21 = ema(close, 21)

//chart plotters
plotshape(bullEngulfing, style=shape.arrowup, location=location.abovebar, color = color.green, text='BullEng')
plotshape(invertedHammer, style=shape.flag, location=location.abovebar, color = color.green, text='Inv Hammer')

plot(ema8, color = color.blue, linewidth=1)
plot(ema13, color = color.green, linewidth=1)
plot(ema21, color = color.orange, linewidth=1)


//Trade conditions
longEntry = close > ema8 and ema8 > ema13 and bullEngulfing
longExit = close <= ema13


//Risk management variables
// RM - inputs
// position_size = 
execProfitTarget = (price * 1.05) / syminfo.mintick
execStopLoss = (price * 0.98)

//timeframe
start = timestamp(2000, 5, 27, 0, 0)
end = timestamp(2020, 6, 30 , 0, 0)

//Entry & exit
if time >= start and time <= end and longEntry and strategy.opentrades == 0
    strategy.entry("Long", strategy.long)
    strategy.exit("Exit Long", from_entry = "Long", profit = execProfitTarget, stop = execStopLoss)

// Debugging.
plotchar(longEntry, "longEntry", "▲", location.top, size = size.tiny)
plotchar(close > ema8, "close > ema8", "•", location.bottom, size = size.tiny)
plotchar(ema8 > ema13, "ema8 > ema13", "—", location.bottom, size = size.tiny)
plotchar(execProfitTarget, "execProfitTarget", "", location.top, size = size.tiny)
plotchar(execStopLoss, "execStopLoss", "", location.top, size = size.tiny)
plotchar(strategy.opentrades, "strategy.opentrades", "", location.top, size = size.tiny)

第二版

// © c4ss10p314

//@version=4
strategy(title = "3emaFXlong", shorttitle = "3emaL", overlay=true, initial_capital = 100000, default_qty_type=strategy.percent_of_equity, default_qty_value = 10)
price = close

//input variables
hammerBodyHeight = input(title="Hammer Body, Max Height (Pips)",minval=1,maxval=100,step=1,defval=300,confirm=false)
bullEnHeight = input(title="BullEn, Height (Pips)",minval=1,maxval=100,step=1,defval=3,confirm=false)
bullEnAbove = input(title="BullEn Above (Pips)",minval=1,maxval=100,step=1,defval=1,confirm=false)
bullEnBelow = input(title="BullEn, Below (Pips)",minval=1,maxval=100,step=1,defval=0,confirm=false)
inpTakeProfit = input(1.25, title = "Example-Take Profit Multiplier", minval = 0.1, maxval = 5, step = 0.1)

//Candlestick patterns
invertedHammer = (abs(close - open) / syminfo.mintick <= hammerBodyHeight) and (close > open) and ((high - close) > (close - open)) and ((open - low) <= (close - open) / 2)
bullEngulfing = ((open[1] - close[1]) / syminfo.mintick >= bullEnHeight) and (close > open) and ((close - open[1]) / syminfo.mintick >= bullEnAbove) and ((close[1] - open) / syminfo.mintick >= bullEnBelow)
ema8 = ema(close, 8)
ema13 = ema(close, 13)
ema21 = ema(close, 21)

//chart plotters
plotshape(bullEngulfing, style=shape.arrowup, location=location.abovebar, color = color.green, text='BullEng')
plotshape(invertedHammer, style=shape.flag, location=location.abovebar, color = color.green, text='Inv Hammer')

plot(ema8, color = color.blue, linewidth=1)
plot(ema13, color = color.green, linewidth=1)
plot(ema21, color = color.orange, linewidth=1)


//Trade conditions
longEntry = close > ema8 and ema8 > ema13 and bullEngulfing
longExit = close <= ema13


//Risk management variables
// RM - inputs
// position_size = 

//timeframe
start = timestamp(2000, 5, 27, 0, 0)
end = timestamp(2020, 6, 30 , 0, 0)

//Entry & exit
var float execProfitTarget = na
var float execProfitPrice = na
var float execStopLoss = na
if time >= start and time <= end and longEntry and strategy.opentrades == 0
    // We know that we enter here only when a bullEngulfing was encountered, 
    // which means the current OHLC prices are those of that engulfing bar.
    execProfitTarget := (abs(close - open) * 1.25) / syminfo.mintick
    execProfitPrice := close + execProfitTarget * syminfo.mintick
    execStopLoss := low
    strategy.entry("Long", strategy.long)
    strategy.exit("Exit Long", from_entry = "Long", profit = execProfitTarget, stop = execStopLoss)

// Plot stop and target.
inTrade = strategy.opentrades != 0
plot(inTrade ? execStopLoss : na, "Stop", color.red, 1, plot.style_circles)
plot(inTrade ? execProfitPrice : na, "Target", color.lime, 1, plot.style_circles)

// Debugging.
// plotchar(strategy.opentrades, "strategy.opentrades", "", location.top, size = size.tiny)
// plotchar(inTrade, "inTrade", "", location.top, size = size.tiny)
// plotchar(longEntry, "longEntry", "▲", location.top, size = size.tiny)
// plotchar(close > ema8, "close > ema8", "•", location.bottom, size = size.tiny)
// plotchar(ema8 > ema13, "ema8 > ema13", "—", location.bottom, size = size.tiny)
// plotchar(execProfitTarget, "execProfitTarget", "", location.top, size = size.tiny)
// plotchar(execStopLoss, "execStopLoss", "", location.top, size = size.tiny)
// plotchar(strategy.opentrades, "strategy.opentrades", "", location.top, size = size.tiny)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.