![](/img/trans.png)
[英]Strategy.exit/s fails to executes for both stop loss or trailing take profit condition only exuecutes for the first condition met
[英]Pine Script. strategy.exit for Trailing Stop and Partial Take Profit
我试图在我的策略上有两个退出,一个作为部分止盈,另一个作为追踪止损。 但是我在网上找到了一些代码,我试图把它放在一起,但是追踪止损不起作用。 它接缝与代码的顺序有关。 因为如果我在部分止盈之前执行追踪止损,追踪止损有效,但部分 TP 无效。 我会很感激一些帮助。 谢谢你。
//@version=5
strategy("BASE", overlay=true, initial_capital = 1000)
//######################################################################################## TIME RANGE ######################################################################################
FromDay=input.int(defval=18,title="FromDay",minval=1,maxval=31)
FromMonth=input.int(defval=8,title="FromMonth",minval=1,maxval=12)
FromYear=input.int(defval=2021,title="FromYear",minval=2016)
dateCond = (time >= timestamp('GMT+10', FromYear,FromMonth, FromDay, 00, 00))
//######################################################################################## TIME RANGE ######################################################################################
//######################################################################################## STRATEGY ########################################################################################
// 3 rsi strategy , when all of them are overbought we sell, and vice versa
if ta.crossover(ta.ema(close,20),ta.ema(close,100)) and dateCond
strategy.entry("BUY", strategy.long)
if ta.crossunder(ta.ema(close,20),ta.ema(close,100)) and dateCond
strategy.entry("SELL", strategy.short)
//######################################################################################## STRATEGY ########################################################################################
//######################################################################################## TRAILING STOP AND TAKE PROFIT #################################################################
//Trailing Stop
longTrailPerc = input.float(title='Trailing Long Loss (%)', minval=0.0, step=0.1, defval=1) * 0.01
shortTrailPerc = input.float(title='Trailing Short Loss (%)', minval=0.0, step=0.1, defval=1) * 0.01
longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
stopValue = close * (1 - longTrailPerc)
math.max(stopValue, longStopPrice[1])
else
0
shortStopPrice := if strategy.position_size < 0
stopValue = close * (1 + shortTrailPerc)
math.min(stopValue, shortStopPrice[1])
else
999999
//################################################################### Partial Take Profit ############################
percentAsPoints(pcnt) =>
strategy.position_size != 0 ? math.round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)
lossPnt = percentAsPoints(2)
strategy.exit("x1", qty_percent = 50, profit = percentAsPoints(5), comment = "Loss")//, loss = lossPnt)
strategy.exit("x2", qty_percent = 25, profit = percentAsPoints(20), comment = "Loss")//, loss = lossPnt)
strategy.exit("x3", qty_percent = 25, profit = percentAsPoints(30), comment = "Loss")//, loss = lossPnt)
strategy.exit("x4", profit = percentAsPoints(4), comment = "Loss")//, loss = lossPnt)
profitPercent(price) =>
posSign = strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0
(price - strategy.position_avg_price) / strategy.position_avg_price * posSign * 100
p1 = plot(profitPercent(high), style=plot.style_linebr, title = "open profit % upper bound")
p2 = plot(profitPercent(low), style=plot.style_linebr, title = "open profit % lower bound")
fill(p1, p2, color = color.red)
//####################################################################### Execution ################################
if strategy.position_size > 0
strategy.exit(id='Trailing', stop=longStopPrice, alert_message="close BTCUSDT a=usdm")
if strategy.position_size < 0
strategy.exit(id='Trailing', stop=shortStopPrice, alert_message="close BTCUSDT a=usdm")
您的尾随 SL 代码看起来正确
你写了相同的 strategy.exit ID
if strategy.position_size > 0
strategy.exit(id='Trailing Long', stop=longStopPrice, alert_message="close BTCUSDT a=usdm")
if strategy.position_size < 0
strategy.exit(id='Trailing Short', stop=shortStopPrice, alert_message="close BTCUSDT a=usdm")
2/ 在下面的情况下,x4 退出将不会执行,因为 x1 正在关闭初始 position 的 50%,x2 初始 position 的 25% 和 x3 初始 Z4757FE07FD492A8BEEZA8 的 25%。 所以......没有什么可以让 x4 关闭
strategy.exit("x1", qty_percent = 50, profit = percentAsPoints(5), comment = "Loss")//, loss = lossPnt)
strategy.exit("x2", qty_percent = 25, profit = percentAsPoints(20), comment = "Loss")//, loss = lossPnt)
strategy.exit("x3", qty_percent = 25, profit = percentAsPoints(30), comment = "Loss")//, loss = lossPnt)
strategy.exit("x4", profit = percentAsPoints(4), comment = "Loss")//, loss = lossPnt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.