Pine 脚本初学者,SL 和 TP

发布于 2025-01-20 02:14:07 字数 1812 浏览 4 评论 0原文

我正在尝试设置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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风和你 2025-01-27 02:14:07

如果我理解正确的话,您需要“将我的止损/止盈设置为入场蜡烛之前 15 个柱的最高/最低价格”。那么你需要“修复”SL &入场柱处计算的 TP 值:

//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

ll = ta.lowest(low, 15)
hh = ta.highest(high, 15)

//Entry

if crossover and macdLine < 0 and macdSignal < 0 and close > ta.ema(close, 200)
    strategy.entry("Long", strategy.long)
    if strategy.position_size <= 0
        longStopLoss := ll
        longTakeProfit := 1.5*(close-longStopLoss)+close
if crossunder and macdLine > 0 and macdSignal > 0 and close < ta.ema(close, 200)
    strategy.entry("Short", strategy.short)
    if strategy.position_size >= 0
        shortStopLoss := hh
        shortTakeProfit := close - (1.5*(shortStopLoss-close))

//Exit

strategy.exit(id="LongClose", from_entry = "Long", stop = longStopLoss, profit = longTakeProfit*100)
strategy.exit(id="ShortClose", from_entry = "Short", stop = shortStopLoss, profit = shortTakeProfit*100)

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:

//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

ll = ta.lowest(low, 15)
hh = ta.highest(high, 15)

//Entry

if crossover and macdLine < 0 and macdSignal < 0 and close > ta.ema(close, 200)
    strategy.entry("Long", strategy.long)
    if strategy.position_size <= 0
        longStopLoss := ll
        longTakeProfit := 1.5*(close-longStopLoss)+close
if crossunder and macdLine > 0 and macdSignal > 0 and close < ta.ema(close, 200)
    strategy.entry("Short", strategy.short)
    if strategy.position_size >= 0
        shortStopLoss := hh
        shortTakeProfit := close - (1.5*(shortStopLoss-close))

//Exit

strategy.exit(id="LongClose", from_entry = "Long", stop = longStopLoss, profit = longTakeProfit*100)
strategy.exit(id="ShortClose", from_entry = "Short", stop = shortStopLoss, profit = shortTakeProfit*100)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文