TradingView trailing stop code

I encountered a problem in the merge code. I want to achieve both trailing stop loss and take profit at the same time. Tradingview can now display ESEL、ELXL TRL STP、ESXS TP, but after I merge the if language, I still cant display ELXS TP and ESXS TRL STP, If you change the code order, only 2 entry and exit methods can be displayed. How should I modify it?

[Modify the detailed view of tradingview] //i.stack.imgur.com/IDiqO.jpg

The entire Pine code:

//@version=3 strategy[title="Take profit [% of instrument price]", overlay=true, pyramiding=3] // STEP 1: // Make inputs that set the take profit % [optional] longProfitPerc = input[title="Long Take Profit [%]", type=float, minval=0.0, step=0.1, defval=3] * 0.01 shortProfitPerc = input[title="Short Take Profit [%]", type=float, minval=0.0, step=0.1, defval=3] * 0.01 // Configure trail stop level with input options [optional] longTrailPerc = input[title="Trail Long Loss [%]", type=float, minval=0.0, step=0.1, defval=3] * 0.01 shortTrailPerc = input[title="Trail Short Loss [%]", type=float, minval=0.0, step=0.1, defval=3] * 0.01 // Calculate moving averages fastSMA = sma[close, 20] slowSMA = sma[close, 60] // Calculate trading conditions enterLong = crossover[fastSMA, slowSMA] enterShort = crossunder[fastSMA, slowSMA] // Plot moving averages plot[series=fastSMA, color=teal] plot[series=slowSMA, color=orange] // STEP 2: // Figure out take profit price longExitPrice = strategy.position_avg_price * [1 + longProfitPerc] shortExitPrice = strategy.position_avg_price * [1 - shortProfitPerc] // Plot take profit values for confirmation plot[series=[strategy.position_size > 0] ? longExitPrice : na, color=green, style=circles, linewidth=3, title="Long Take Profit"] plot[series=[strategy.position_size < 0] ? shortExitPrice : na, color=red, style=circles, linewidth=3, title="Short Take Profit"] // Determine trail stop loss prices longStopPrice = 0.0, shortStopPrice = 0.0 longStopPrice := if [strategy.position_size > 0] stopValue = close * [1 - longTrailPerc] max[stopValue, longStopPrice[1]] else 0 shortStopPrice := if [strategy.position_size < 0] stopValue = close * [1 + shortTrailPerc] min[stopValue, shortStopPrice[1]] else 999999 // Plot stop loss values for confirmation plot[series=[strategy.position_size > 0] ? longStopPrice : na, color=fuchsia, style=cross, linewidth=2, title="Long Trail Stop"] plot[series=[strategy.position_size < 0] ? shortStopPrice : na, color=fuchsia, style=cross, linewidth=2, title="Short Trail Stop"] // Submit entry orders if [enterLong] strategy.entry[id="EL", long=true] if [enterShort] strategy.entry[id="ES", long=false] // STEP 3: // Submit exit orders based on take profit price and trail stop loss price if [strategy.position_size > 0] strategy.exit[id="XL TRL STP", stop=longStopPrice] strategy.exit[id="XL TP", limit=longExitPrice] if [strategy.position_size < 0] strategy.exit[id="XS TP", limit=shortExitPrice] strategy.exit[id="XS TRL STP", stop=shortStopPrice]

This is the code that I am going to modify and merge:

// STEP 3: // Submit exit orders based on take profit price and trail stop loss price if [strategy.position_size > 0] strategy.exit[id="XL TRL STP", stop=longStopPrice] if [strategy.position_size < 0] strategy.exit[id="XS TRL STP", stop=shortStopPrice] if [strategy.position_size > 0] strategy.exit[id="XL TP", limit=longExitPrice] if [strategy.position_size < 0] strategy.exit[id="XS TP", limit=shortExitPrice]

Please help me to solve this problem, I will be very grateful!

Video liên quan

Chủ Đề