Pine 脚本初学者,SL 和 TP
我正在尝试设置TP和SL,但出了点问题。我需要将SL/TP设置为进入蜡烛前的最高/最低价格。如果我创建没有“ varip”的变量,那么据我了解,它将在每个新蜡烛后设置一个新的SL/TP。因此,只要位置打开,我试图使我的利润和损失变量持续不断,然后通过使用“ varip”再次使其“ na”。另外,我需要将TP设置为SL的1.5倍。 在这里您可以看到利润因子为1.9而不是1.5。另外,SL在错误的位置触发。 先感谢您。
//The strategy
//@version=5
strategy("Momentum Signal", overlay=true, margin_long=100, margin_short=100)
ema12 = ta.ema(close, 12)
ema26 = ta.ema(close, 26)
ema9 = ta.ema(close, 9)
macdLine = ema12 - ema26
macdSignal = ta.ema(macdLine, 9)
crossover = ta.crossover(macdLine, macdSignal)
crossunder = ta.crossunder(macdLine, macdSignal)
//Declaring TP/SL variables
varip float longStopLoss = na
varip float longTakeProfit = na
varip float shortStopLoss = na
varip float shortTakeProfit = na
//Entry
if crossover and macdLine < 0 and macdSignal < 0 and close > ta.ema(close, 200)
strategy.entry("Long", strategy.long)
if crossunder and macdLine > 0 and macdSignal > 0 and close < ta.ema(close, 200)
strategy.entry("Short", strategy.short)
//Exit
if strategy.position_size > 0
longStopLoss := ta.lowest(low, 15)
longTakeProfit := 1.5*(close-longStopLoss)+close
strategy.exit(id="LongClose", stop = longStopLoss, profit = longTakeProfit*100)
else
longStopLoss := na
longTakeProfit := na
if strategy.position_size < 0
shortStopLoss := ta.highest(high, 15)
shortTakeProfit := close - (1.5*(shortStopLoss-close))
strategy.exit(id="ShortClose", stop = shortStopLoss, profit = shortTakeProfit*100)
else
shortStopLoss := na
shortTakeProfit := na
I am trying to set TP and SL but something goes wrong. I need to set my SL/TP at the highest/lowest price of 15 bars before the entry candle. If I create my variables without "varip", then as I understand, it sets a new SL/TP after each new candle. So I tried to make my profit and loss variables constant as long as the position is open, and then make them "na" again by using "varip". Also, I need to set my TP at 1.5 times the SL.
Here you can see that the profit factor is 1.9 instead of 1.5. Also, the SL is triggered in the wrong place.
Thank you in advance.
//The strategy
//@version=5
strategy("Momentum Signal", overlay=true, margin_long=100, margin_short=100)
ema12 = ta.ema(close, 12)
ema26 = ta.ema(close, 26)
ema9 = ta.ema(close, 9)
macdLine = ema12 - ema26
macdSignal = ta.ema(macdLine, 9)
crossover = ta.crossover(macdLine, macdSignal)
crossunder = ta.crossunder(macdLine, macdSignal)
//Declaring TP/SL variables
varip float longStopLoss = na
varip float longTakeProfit = na
varip float shortStopLoss = na
varip float shortTakeProfit = na
//Entry
if crossover and macdLine < 0 and macdSignal < 0 and close > ta.ema(close, 200)
strategy.entry("Long", strategy.long)
if crossunder and macdLine > 0 and macdSignal > 0 and close < ta.ema(close, 200)
strategy.entry("Short", strategy.short)
//Exit
if strategy.position_size > 0
longStopLoss := ta.lowest(low, 15)
longTakeProfit := 1.5*(close-longStopLoss)+close
strategy.exit(id="LongClose", stop = longStopLoss, profit = longTakeProfit*100)
else
longStopLoss := na
longTakeProfit := na
if strategy.position_size < 0
shortStopLoss := ta.highest(high, 15)
shortTakeProfit := close - (1.5*(shortStopLoss-close))
strategy.exit(id="ShortClose", stop = shortStopLoss, profit = shortTakeProfit*100)
else
shortStopLoss := na
shortTakeProfit := na
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您需要“将我的止损/止盈设置为入场蜡烛之前 15 个柱的最高/最低价格”。那么你需要“修复”SL &入场柱处计算的 TP 值:
If I understood you correctly, you need "set my SL/TP at the highest/lowest price of 15 bars before the entry candle." Then you need "to fix" SL & TP values calculated at the entry bar: