Search code examples
pine-scriptpine-script-v5

This script doesn't act the same way with Short positions as it does with Long. The SL and TP aren't plotting and the inputs don't seem to register


enter image description hereI'm trying to isolate the info on tradingview so I disabled the "long" inputs. I don't understand why the short inputs aren't functioning. The long side works exactly as I want.

Goal: TP and SL plotted and adhered to for back testing on both long and short positions.

Present Situation: TP and SL function and plot correctly for long positions only.

//@version=5
strategy('Super', overlay=true, format=format.price, precision=2) //timeframe='')

//User Inputs
Periods = input(title='ATR Period', defval=10)
src = input(hl2, title='Source')
Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=3.0)
changeATR = input(title='Change ATR Calculation Method ?', defval=true)
showsignals = input(title='Show Buy/Sell Signals ?', defval=true)
highlighting = input(title='Highlighter On/Off ?', defval=true)

sl_inp = input(2.0, title='Stop Loss %') / 100
tp_inp = input(1.0, title='Take Profit %') / 100



atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2
up = src - Multiplier * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + Multiplier * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title='Up Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0))
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title='UpTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0))
plotshape(buySignal and showsignals ? up : na, title='Buy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
dnPlot = plot(trend == 1 ? na : dn, title='Down Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.red, 0))
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title='DownTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 0))
plotshape(sellSignal and showsignals ? dn : na, title='Sell', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
mPlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? trend == 1 ? color.green : color.white : color.white
shortFillColor = highlighting ? trend == -1 ? color.red : color.white : color.white
fill(mPlot, upPlot, title='UpTrend Highligter', color=longFillColor, transp=90)
fill(mPlot, dnPlot, title='DownTrend Highligter', color=shortFillColor, transp=90)
alertcondition(buySignal, title='SuperTrend Buy', message='SuperTrend Buy!')
alertcondition(sellSignal, title='SuperTrend Sell', message='SuperTrend Sell!')
changeCond = trend != trend[1]
alertcondition(changeCond, title='SuperTrend Direction Change', message='SuperTrend has changed direction!')





//long_stop_level = strategy.position_avg_price * (1 - sl_inp)
//long_take_level = strategy.position_avg_price * (1 + tp_inp)
short_stop_level = strategy.position_avg_price * (-1 - sl_inp)
short_take_level = strategy.position_avg_price * (-1 + tp_inp)

//strategy.entry('long', strategy.long, when=buySignal)// or sellSignal)
strategy.entry('short', strategy.short, when=sellSignal)// or sellSignal)
//strategy.exit('exit long', stop=long_stop_level, limit=long_take_level)
strategy.exit('exit short', stop=short_stop_level, limit=short_take_level)


//plot(long_stop_level, color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2)
//plot(long_take_level, color=color.new(color.green, 0), style=plot.style_linebr, linewidth=2)
plot(short_stop_level, color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2)
plot(short_take_level, color=color.new(color.green, 0), style=plot.style_linebr, linewidth=2)

Solution

  • They are working but your math is wrong. Right now, you multiply your average price with a negative value. Therefore, your TP and SL are also negative prices.

    Also, before placing an exit order, check whether you are in a position.

    enter image description here

    Below calculation will give you the correct results:

    short_stop_level = strategy.position_avg_price * (1 + sl_inp)
    short_take_level = strategy.position_avg_price * (1 - tp_inp)
    
    if (sellSignal)
        strategy.entry('short', strategy.short)
    
    if (strategy.position_size < 0)
        strategy.exit('exit short', stop=short_stop_level, limit=short_take_level)