首页 > 解决方案 > 如何确保我的交易有 2 种不同的止损(松树脚本)?

问题描述

被困在这个问题上一段时间了。在我的策略脚本中,我允许同时打开 2 个仓位(在同一个方向)。然而,现在最终发生的是,当交易开始时,第一个头寸的止损变为第二个头寸的止损。这造成了相当多的不必要的损失。我想我需要存储第一个止损的价值(价值何时?),但我无法弄清楚。有人知道如何解决这个问题吗?谢谢!

    shortEntry = MACDfilter == "Yes" ? ((shortCond and hist < 0) or (shortCond[1] and hist[1] > 0 and hist <0)) : shortCond
    bgShort = shortEntry ? color.orange : na
    bgcolor(bgShort)
    
    SfixedATRSL = float(na)
    SfixedATRTP = float(na)
    SfixedATRSL := (longEntry and typeTPSL =="ATR - fixed") ? na : shortEntry ? close + (atr * atrStopMultiplier) : SfixedATRSL[1] 
    SfixedATRTP := (longEntry and typeTPSL =="ATR - fixed") ? na : shortEntry ? close - ((atr * atrStopMultiplier)* rr) : SfixedATRTP[1]

if (strategy.position_size < 0 and barstate.isconfirmed)
    strategy.exit(id="Short TP/SL", limit=S_typeTP, stop=S_typeSL)

我尝试了一些有价值的事情,但没有得到我希望的结果..

SL_trade1 = strategy.opentrades == (1 or 2) ? na : valuewhen(strategy.opentrades == 0 and shortEntry,close + (atr * atrStopMultiplier),0)
SL_trade2 = strategy.opentrades == (0 or 1) ? na : valuewhen(strategy.opentrades == 1 and shortEntry,close + (atr * atrStopMultiplier),0)

标签: pine-script

解决方案


推荐阅读