'Pinescript - Why does my stoploss trigger upon entry stop?

I'm working strategy based on a MTF Stoch RSI indicator I created. In the strategy, based on Stoch RSI higher and medium timeframe direction and entry timeframe crossover, I set an entry stop a few pips below the last candle low and the stoploss above the high for a long trade, and opposite for a short trade. For some reason, the stoploss triggers upon entry and I can't figure it out.

Image of Example of issue

// MTF Stochastic RSI Strategy by // © drbarry92064859
//@version=4
strategy("Stochastic RSI Bands Strategy", "StochRSI Band Strategy", overlay = false, initial_capital = 1000, default_qty_value = 100, default_qty_type = strategy.percent_of_equity)

prevRep = true
// Timeframe settings
etf = input("15", "Entry Timeframe", input.resolution, options=["1","5","15","60"])
mtf = input("240", "Middle Timeframe", input.resolution, options= ["60","240","D"])
htf = input("D", "Higher Timeframe", input.resolution, options=["240","D","W","M"])

//Which Timeframes to use Settings
showetf = input(false, "Show Entry Timeframe", input.bool)
showmtf = input(true, "Show Middle Timeframe", input.bool)
showhtf = input(true, "Show Higher Timeframe", input.bool)

//StochRSI Settings
etf_lengthRSI = input(13, "Entry TimeFrame RSI Length", options=[8, 13, 21, 34])
mtf_lengthRSI = input(13, "Middle TimeFrame RSI Length", options=[8, 13, 21, 34])
htf_lengthRSI = input(13, "Higher TimeFrame RSI Length", options=[8, 13, 21, 34])

htf_srsi_settings(htf_lengthRSI) =>
    htf_lengthStoch = htf_lengthRSI==8 ? 5 : htf_lengthRSI==13 ? 8 : htf_lengthRSI==21 ? 13 : 21
    htf_smoothK = htf_lengthRSI==8 ? 3 : htf_lengthRSI==13 ? 5 : htf_lengthRSI==21 ? 8 : 13
    htf_smoothD = htf_lengthRSI==8 ? 3 : htf_lengthRSI==13 ? 5 : htf_lengthRSI==21 ? 8 : 13
    [htf_lengthStoch, htf_smoothK, htf_smoothD]

mtf_srsi_settings(mtf_lengthRSI) =>
    mtf_lengthStoch = mtf_lengthRSI==8 ? 5 : mtf_lengthRSI==13 ? 8 : mtf_lengthRSI==21 ? 13 : 21
    mtf_smoothK = mtf_lengthRSI==8 ? 3 : mtf_lengthRSI==13 ? 5 : mtf_lengthRSI==21 ? 8 : 13
    mtf_smoothD = mtf_lengthRSI==8 ? 3 : mtf_lengthRSI==13 ? 5 : mtf_lengthRSI==21 ? 8 : 13
    [mtf_lengthStoch, mtf_smoothK, mtf_smoothD]
    
etf_srsi_settings(etf_lengthRSI) =>
    etf_lengthStoch = etf_lengthRSI==8 ? 5 : etf_lengthRSI==13 ? 8 : etf_lengthRSI==21 ? 13 : 21
    etf_smoothK = etf_lengthRSI==8 ? 3 : etf_lengthRSI==13 ? 5 : etf_lengthRSI==21 ? 8 : 13
    etf_smoothD = etf_lengthRSI==8 ? 3 : etf_lengthRSI==13 ? 5 : etf_lengthRSI==21 ? 8 : 13
    [etf_lengthStoch, etf_smoothK, etf_smoothD]

overbought = input(75, minval=1)
oversold = input(25, minval=1)

//StochRSI Calculation

[htf_lengthStoch, htf_smoothK, htf_smoothD] = htf_srsi_settings(htf_lengthRSI)
[mtf_lengthStoch, mtf_smoothK, mtf_smoothD] = mtf_srsi_settings(mtf_lengthRSI)
[etf_lengthStoch, etf_smoothK, etf_smoothD] = etf_srsi_settings(etf_lengthRSI)

src = input(close, title="RSI Source")
mtf_src = security(syminfo.tickerid, mtf, close)
htf_src = security(syminfo.tickerid, htf, close)
etf_rsi1 = rsi(src, etf_lengthRSI)
htf_rsi1 = rsi(src, htf_lengthRSI)
mtf_rsi1 = rsi(src, mtf_lengthRSI)
etf_k_sma = sma(stoch(etf_rsi1, etf_rsi1, etf_rsi1, etf_lengthStoch), etf_smoothK)
etf_d_sma = sma(etf_k_sma, etf_smoothD)
htf_k_sma = sma(stoch(htf_rsi1, htf_rsi1, htf_rsi1, htf_lengthStoch), htf_smoothK)
htf_d_sma = sma(htf_k_sma, htf_smoothD)
mtf_k_sma = sma(stoch(mtf_rsi1,mtf_rsi1, mtf_rsi1, mtf_lengthStoch), mtf_smoothK)
mtf_d_sma = sma(mtf_k_sma, mtf_smoothD)

// Function to securely and simply call `security()` so that it never repaints and never looks ahead.
//f_secureSecurity(_symbol, _res, _src) => security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)
htf_security(_sym, _res, _src, _rep) => security(_sym, _res, _src[not _rep and barstate.isrealtime ? 1 : 0])[_rep or barstate.isrealtime ? 0 : 1]
mtf_security(_sym, _res, _src, _rep) => security(_sym, _res, _src[not _rep and barstate.isrealtime ? 1 : 0])[_rep or barstate.isrealtime ? 0 : 1]
etf_security(_sym, _res, _src, _rep) => security(_sym, _res, _src[not _rep and barstate.isrealtime ? 1 : 0])[_rep or barstate.isrealtime ? 0 : 1]

// Get all timeframe srsi data

htf_k = htf_security(syminfo.tickerid, htf, htf_k_sma, not prevRep)
htf_d = htf_security(syminfo.tickerid, htf, htf_d_sma, not prevRep)
mtf_k = mtf_security(syminfo.tickerid, mtf, mtf_k_sma, not prevRep)
mtf_d = mtf_security(syminfo.tickerid, mtf, mtf_d_sma, not prevRep)
etf_k = etf_security(syminfo.tickerid, etf, etf_k_sma, not prevRep)
etf_d = mtf_security(syminfo.tickerid, etf, etf_d_sma, not prevRep)

//Plot SRSI
plot(htf_k,"HTF Fast SRSI", color.blue, linewidth=4)
plot(htf_d,"HTF Slow SRSI", color.red, linewidth=4)
plot(mtf_k,"HTF Fast SRSI", color.blue, linewidth=2)
plot(mtf_d,"HTF Slow SRSI", color.red, linewidth=2)
plot(etf_k,"HTF Fast SRSI", color.blue, linewidth=1)
plot(etf_d,"HTF Slow SRSI", color.red, linewidth=1)
hline(overbought, color=color.black, linestyle=hline.style_dashed)
hline(oversold, color=color.black, linestyle=hline.style_dashed)


//StochRSI Positions
htfBull = htf_k > htf_d
htfBear = htf_k < htf_d
mtfBull = mtf_k > mtf_d
mtfBear = mtf_k < mtf_d
etfBullCO = etf_k[1] < etf_d[1] and etf_k > etf_d and etf_k[1] <= oversold and etf_d[1] <= oversold
etfBearCO = etf_d[1] < etf_k[1] and etf_d > etf_k and etf_k[1] >= overbought and etf_d[1] >= overbought

htfOB = htf_k >= overbought and htf_d >= overbought
htfOS = htf_k <= oversold and htf_d <= oversold
mtfOB = mtf_k >= overbought and mtf_d >= overbought
mtfOS = mtf_k <= oversold and mtf_d <= oversold

//StochRsi Bands
htf_srsiLong = (htfBull and not htfOB) or (htfBear and htfOS)
htf_srsiShort = (htfBear and not htfOS) or (htfBull and htfOB)
mtf_srsiLong = (mtfBull and not mtfOB) or (mtfBear and mtfOS)
mtf_srsiShort = (mtfBear and not mtfOS) or (mtfBull and mtfOB)

//bgcolor(htf_srsiLong and showhtf ? color.green : htf_srsiShort and showhtf ? color.red : na, transp=60)
//bgcolor(mtf_srsiLong and showmtf ? color.green : mtf_srsiShort and showmtf ? color.red : na, transp=80)

//Entry Signals based on SRSI Crossovers
//plotshape(etfBullCO and showetf and htf_srsiLong, "Bullish CO Signal", shape.triangleup, location.belowbar, color=color.green, size=size.normal, offset=-1)
//plotshape(etfBearCO and showetf and htf_srsiShort, "Bearish CO Signal", shape.triangledown, location.abovebar, color=color.red, size=size.normal, offset=-1)

//Strategy Orders

//Long Entry

float LongHighEntry = na
float LongLowStop = na
float LongProfit = na

goLong = etfBullCO and htf_srsiLong and strategy.position_size == 0   

if goLong

    LongHighEntry := high[1] + syminfo.mintick * 50
    LongLowStop := low[1] - syminfo.mintick * 100
    LongProfit := (LongHighEntry - LongLowStop) + LongHighEntry
    strategy.entry("Open Long", strategy.long, stop=LongHighEntry)
    strategy.exit("Long StopLoss", "Open Long",loss=LongLowStop, profit=LongProfit )

plot(LongHighEntry, "LongEntry", color.blue, 3, plot.style_linebr)
plot(LongLowStop, "LongStopLoss", color.red, 3, plot.style_linebr)
plot(LongProfit, "LongProfit", color.green, 3, plot.style_linebr)

//Long Cancel Trade if Hits StopLoss or Enters Red Zone before Entry
    
strategy.cancel("Open Long", when = strategy.position_size == 0  and low < LongLowStop )
   
//Short Entry 

float ShortLowEntry = na
float ShortHighStop = na
float ShortProfit = na

goShort = etfBearCO and htf_srsiShort and strategy.position_size == 0   

if goShort
    ShortLowEntry := low[1] - syminfo.mintick * 50
    ShortHighStop := high[1] + syminfo.mintick * 100
    ShortProfit := (ShortLowEntry - ShortHighStop) + ShortLowEntry
    strategy.entry("Open Short", strategy.short, stop=ShortLowEntry)
    strategy.exit("Short StopLoss", "Open Short",loss=ShortHighStop, profit=ShortProfit)
    
plot(ShortLowEntry, "ShortEntry", color.blue, 3, plot.style_linebr)
plot(ShortHighStop, "ShortStopLoss", color.red, 3, plot.style_linebr)
plot(ShortProfit, "ShortProfit", color.green, 3, plot.style_linebr)

strategy.cancel("Open Short", when = strategy.position_size == 0 and high > ShortHighStop )


Solution 1:[1]

I suppose the bug is in wrong parameter name that you use in this line:

strategy.exit("Long StopLoss", "Open Long",loss=LongLowStop, profit=LongProfit )

Try this:

strategy.exit("Long StopLoss", "Open Long", stop=LongLowStop, limit=LongProfit )

Same fix for exit from short ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrey D