Marco Baglioni
Marco Baglioni

Reputation: 1

Tradingview - pine script for Take profit and Stop loss (with short tp and sl too!)

I'm new with pine so please help me! I can't use both SL/TP for LONG and SL/TP for SHORT. Only one at time works in backtest. Can someone help me to fix my code? Thanks so much!!!

I have 4 INPUT: LONG TAKE PROFIT % SHORT TAKE PROFIT % LONG STOP LOSS % SHORT STOP LOSS %

//@version=4
strategy("SuperTrend STRATEGY", overlay=true)

buy = buySignal
alertcondition(buy, title='Buy Signal', message='Buy Signal Alert')
sell = sellSignal
alertcondition(sell, title='Sell Signal', message='Sell Signal Alert')
buy1= barssince(buySignal)
sell1 = barssince(sellSignal)
color1 = buy1[1] < sell1[1] ? color.green : buy1[1] > sell1[1] ? color.red : na
barcolor(barcoloring ? color1 : na)

// STEP 1:
// Make inputs that set the take profit % (optional)
longProfitPerc = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=2.3) * 0.01
shortProfitPerc = input(title="Short Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=5.2) * 0.01

// STEP 2:
// Figure out take profit price
longExitPrice  = strategy.position_avg_price * (1 + longProfitPerc)
shortExitPrice = strategy.position_avg_price * (1 - shortProfitPerc)

// STEP 3:
// Submit exit orders based on TAKE PROFIT price
if (strategy.position_size > 0)
    strategy.exit(id="Long TP", limit=longExitPrice)
if (strategy.position_size < 0)
    strategy.exit(id="Short TP", limit=shortExitPrice)

// STEP 4:
// Plot take profit values for confirmation
plot(series=(strategy.position_size > 0) ? longExitPrice : na,
     color=color.green, style=plot.style_circles,
     linewidth=3, title="Long Take Profit")
plot(series=(strategy.position_size < 0) ? shortExitPrice : na,
     color=color.red, style=plot.style_circles,
     linewidth=3, title="Short Take Profit")

// STEP 1 STOP LOSS: 
longLossPerc = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1) * 0.01
shortLossPerc = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1) * 0.01

// STEP 2:
// Determine stop loss price
longStopPrice  = strategy.position_avg_price * (1 - longLossPerc)
shortStopPrice = strategy.position_avg_price * (1 + shortLossPerc)

// STEP 3:
// Submit exit orders based on calculated STOP LOSS price
if (strategy.position_size > 0)
    strategy.exit(id="LONG StopLoss", stop=longStopPrice)
if (strategy.position_size < 0)
    strategy.exit(id="SHORT StopLoss", stop=shortStopPrice)

// STEP 4:
// Plot stop loss values for confirmation
plot(series=(strategy.position_size > 0) ? longStopPrice : na,
     color=color.red, style=plot.style_cross,
     linewidth=2, title="Long Stop Loss")
plot(series=(strategy.position_size < 0) ? shortStopPrice : na,
     color=color.red, style=plot.style_cross,
     linewidth=2, title="Short Stop Loss")
     

Upvotes: 0

Views: 1973

Answers (1)

Andrey D
Andrey D

Reputation: 1714

You should specify stop & limit parameters in one strategy.exit:

strategy.exit(id="LONG Exit", stop=longStopPrice, limit=longExitPrice)

Upvotes: 0

Related Questions