Search code examples
pine-scriptalgorithmic-tradingtradingview-api

Trailing STOPLOSS not triggering in tradingview (pine script)


I am new to pinescript and trying to code, entry after longcondtion (ema crossover) and strategy exits 100% quantity if initial stoploss triggers. and books 50% quantity at 1st target and remaining 50% quantity should be trailed with supertrend. when supertrend changes direction it should exit with remaining 50%, But it's not exiting remaining 50% quantity even supertrend crosses. vice versa for short condition

Please suggest me if any one has done similar code, Thanks in advance.

if (longCond )
    strategy.entry("BUY",strategy.long)
    
if (shortCond )
    strategy.entry("SELL",strategy.short)


tar=input(defval=30.0,title="TARGET")

sl=input(defval=30.0,title="STOP LOSS")
tar:=tar/syminfo.mintick
sl:=sl/syminfo.mintick

 //adding stoploss and target option
strategy.exit("x1", from_entry="BUY", qty = 50, profit = tar,loss = sl)

strategy.exit("x1", from_entry="SELL",qty = 50, profit = tar,loss = sl)


if strategy.position_size>0 and abs(strategy.position_size)==50 and crossunder(close,atrStop)
    strategy.exit("x2", from_entry="BUY", qty_percent=100)
    

if strategy.position_size<0 and abs(strategy.position_size)==50 and crossover(close,atrStop)
    strategy.exit("x2", from_entry="SELL",qty_percent=100)
    

**complete code as below

//complete code//@version=4
strategy("SLtrail", overlay = true,calc_on_every_tick=true,default_qty_value=100)

quant=input(title="Trade Quantity",defval=50)
// === LOGIC ===
length = input(type=input.integer,defval=20,minval=1,title="Length")
ratio = input(type=input.integer,defval=3,title="Multiplier (3x length, 4x length, etc)",options=[3,4,5,6,7,8,9,10])
longOnly = input(type=input.bool,defval=false,title="Long Only")
fast = ema(hl2,length)
slow = ema(hl2,length * ratio)
plot(fast,linewidth=2,color=color.orange,title="Fast")
plot(slow,linewidth=2,color=color.blue,title="Slow")

longCond = crossover(fast,slow)
shortCond = crossunder(fast,slow)

//length = input(10, "Length", minval = 2)
src = input(close, "Source")
factor = input(3, "Multiplier", minval = 0.25, step = 0.25)
st(src, atrlen, atrfactor) =>
    var max     = src
    var min     = src
    var uptrend = true
    var stop    = 0.0
    atrM        = nz(atr(atrlen) * atrfactor, tr)
    max         := max(max, src)
    min         := min(min, src)
    stop        := nz(uptrend ? max(stop, max - atrM) : min(stop, min + atrM), src)
    uptrend     := src - stop >= 0.0
    if uptrend != nz(uptrend[1], true)
        max    := src
        min    := src
        stop   := uptrend ? max - atrM : min + atrM
    [stop, uptrend]

[atrStop, uptrend] = st(src, length, factor)

plot(atrStop, "Volatility Stop", style=plot.style_stepline, color= uptrend ? #009688 : #F44336)
bool downtrend = (uptrend != true)


if (longCond )
    strategy.entry("BUY",strategy.long)
    
if (shortCond )
    strategy.entry("SELL",strategy.short)


tar=input(defval=30.0,title="TARGET IN POINTS")

sl=input(defval=30.0,title="STOP LOSS IN POINTS")
tar:=tar/syminfo.mintick
sl:=sl/syminfo.mintick

 //adding stoploss and target option
strategy.exit("x1", from_entry="BUY", qty = 50, profit = tar,loss = sl)

strategy.exit("x1", from_entry="SELL",qty = 50, profit = tar,loss = sl)


if strategy.position_size>0 and abs(strategy.position_size)==50 and crossunder(close,atrStop)
    strategy.exit("x2", from_entry="BUY", qty_percent=100)
   

if strategy.position_size<0 and abs(strategy.position_size)==50 and crossover(close,atrStop)
    strategy.exit("x2", from_entry="SELL",qty_percent=100)
    

strategy.exit(id="LX4",from_entry="BUY",qty_percent=100,loss=sl)
strategy.exit(id="LX4",from_entry="SELL",qty_percent=100,loss=sl)


Solution

  • Try this version:

    //@version=4
    strategy("SLtrail", overlay = true,calc_on_every_tick=true,default_qty_value=100)
    
    quant=input(title="Trade Quantity",defval=50)
    // === LOGIC ===
    length = input(type=input.integer,defval=20,minval=1,title="Length")
    ratio = input(type=input.integer,defval=3,title="Multiplier (3x length, 4x length, etc)",options=[3,4,5,6,7,8,9,10])
    longOnly = input(type=input.bool,defval=false,title="Long Only")
    fast = ema(hl2,length)
    slow = ema(hl2,length * ratio)
    plot(fast,linewidth=2,color=color.orange,title="Fast")
    plot(slow,linewidth=2,color=color.blue,title="Slow")
    
    longCond = crossover(fast,slow)
    shortCond = crossunder(fast,slow)
    
    //length = input(10, "Length", minval = 2)
    src = input(close, "Source")
    factor = input(3, "Multiplier", minval = 0.25, step = 0.25)
    st(src, atrlen, atrfactor) =>
        var max     = src
        var min     = src
        var uptrend = true
        var stop    = 0.0
        atrM        = nz(atr(atrlen) * atrfactor, tr)
        max         := max(max, src)
        min         := min(min, src)
        stop        := nz(uptrend ? max(stop, max - atrM) : min(stop, min + atrM), src)
        uptrend     := src - stop >= 0.0
        if uptrend != nz(uptrend[1], true)
            max    := src
            min    := src
            stop   := uptrend ? max - atrM : min + atrM
        [stop, uptrend]
    
    [atrStop, uptrend] = st(src, length, factor)
    
    plot(atrStop, "Volatility Stop", style=plot.style_stepline, color= uptrend ? #009688 : #F44336)
    bool downtrend = (uptrend != true)
    
    
    if (longCond )
        strategy.entry("BUY",strategy.long)
        
    if (shortCond )
        strategy.entry("SELL",strategy.short)
    
    
    tar=input(defval=30.0,title="TARGET IN POINTS")
    
    sl=input(defval=30.0,title="STOP LOSS IN POINTS")
    tar:=tar/syminfo.mintick
    sl:=sl/syminfo.mintick
    
     //adding stoploss and target option
    strategy.exit("x1", from_entry="BUY", qty = 50, profit = tar,loss = sl)
    
    strategy.exit("x1", from_entry="SELL",qty = 50, profit = tar,loss = sl)
    
    
    if crossunder(close,atrStop)
        strategy.close("BUY", qty_percent=100)
       
    
    if crossover(close,atrStop)
        strategy.close("SELL",qty_percent=100)
        
    
    strategy.exit(id="LX4",from_entry="BUY",qty_percent=100,loss=sl)
    strategy.exit(id="LX4",from_entry="SELL",qty_percent=100,loss=sl)