简体   繁体   中英

pine script different strategy.exit

I'm having a hardtime to have two different strategy.exit . One for the take profit and other to the Trailing Stop Loss. The strategy works fine if I put both in the same strategy.exit line, but I want two different labels on the chart, on for the TP and other to the TSL. If I put two strategy.exit , only Take profit work.

// Get inputs
atrLength = input.int(title="ATR Length", defval=21, minval=1)
useStructure = input.bool(title="Use Structure?", defval=true)
lookback = input.int(title="How Far To Look Back For High/Lows", defval=9, minval=1)
atrStopMultiplier = input.float(title="ATR Multiplier", defval=0.85, minval=0.1, step = 0.05)
// Calculate data
atr = ta.atr(atrLength)
lowestLow = ta.lowest(low, lookback)
highestHigh = ta.highest(high, lookback)
longStop = (useStructure ? lowestLow : close) - atr * atrStopMultiplier
shortStop = (useStructure ? highestHigh : close) + atr * atrStopMultiplier
//-------------------------------------------------------------------------------------- ATR Trailing Stop Loss --------------------------------------------------------------------------
//-------------------------------------------------------------------------------------- Take Profit -------------------------------------------------------------------------------------
longProfitPerc = input.float(title="Long Take Profit (%)", minval=0.0, step=0.1, defval=1) * 0.01
shortProfitPerc = input.float(title="Short Take Profit (%)", minval=0.0, step=0.1, defval=1) * 0.01
// Figure out take profit price
longExitPrice  = strategy.position_avg_price * (1 + longProfitPerc)
shortExitPrice = strategy.position_avg_price * (1 - shortProfitPerc)
// Submit exit orders for take profit
if strategy.position_size > 0 
    strategy.exit(id='Long STOP', limit=longExitPrice, alert_message="close BTCUSDT a=usdm")
    strategy.exit(id = 'L - TSL', stop = longStop)
if strategy.position_size < 0 
    strategy.exit(id='Short STOP', limit=shortExitPrice, alert_message="close BTCUSDT a=usdm")
    strategy.exit(id = 'S - TSL', stop = shortStop)

Have one strategy.exit() and use the comment_profit and comment_loss properties of the strategy.exit() .

strategy.exit(id='Long Exit', limit=longExitPrice, stop = longStop, alert_message="close BTCUSDT a=usdm", comment_profit="Long - TP", comment_loss="Long SL")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM