chartspine-scripttradingtechnical-indicator

Tradingview Pinescript alert not working as expected


I am using below pinescript and i am not getting alerts as expected. I am aware that it is a repainting script. It is working fine with the back testing. Long alert is set to "Once per bar close" and short alert is set to "once per bar".

The unexpected behavior is 1) Few times, i am getting short alert though there is no corresponding long alert (though i have taken care in my script, short alert will be sent only when there is a long). 2) Multiple consecutive Short alerts per bar. I am aware that in the realtime bar, the short condition may be true multiple number of times. But since i have set alert to "once per bar", the alert should come only for the first time short condition becomes true.

Please can you let me know if i am doing anything wrong ?

Thanks in advance.

//@version=4
study("My Script",overlay = true)

ST = input(true, title = "Activate Strategy Tester")
T_SY = input(2020, title = "Strategy Start Year")
T_SM = input(5, title = "Start Month")
T_SD = input(1, title = "Strategy Start Day")
T_EY = input(2025, title = "Strategy End Year")
T_EM = input(1, title = "End Month")
T_ED = input(1, title = "Strategy End Day")
T_S = timestamp(T_SY, T_SM, T_SD,00,00)
T_E = timestamp(T_EY, T_EM, T_ED,00,00)
T= ST and time >= T_S and time <= T_E 

firstrun = true
bought = false
longcondition = false
shortcondition = false

//Just to track first run

firstrun := firstrun[1]


if (firstrun == false)
    bought := bought[1]

//once condition is met, send a buy alert and make "bought" equal to true  //to enable selling

if (close <= 8600 and bought==false and T)
    bought := true
    longcondition :=true

alertcondition(longcondition,  "Long",  "Long")  

plotshape(longcondition,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.abovebar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)


if (longcondition)
    longcondition :=false

//once condition is met, sent a sell alert.

if (bought and close>=9000 and T)  
    shortcondition := true
    bought := false

alertcondition(shortcondition,  "short",  "short") 
plotshape(shortcondition,  title = "Sell",  text = 'Sell',  style = shape.labelup,   location = location.belowbar, color= color.red, textcolor = color.white, transp = 0, size = size.tiny)


if (shortcondition)
    shortcondition :=false

plotchar(bought, "bought", "", location = location.top)   

firstrun := false


Solution

  • You bought variable now automatically saves its value bar to bar and I have removed the late reinitializations of vars in your script. Seems to be working fine here:

    //@version=4
    study("My Script",overlay = true)
    
    ST = input(true, title = "Activate Strategy Tester")
    T_SY = input(2000, title = "Strategy Start Year")
    T_SM = input(5, title = "Start Month")
    T_SD = input(1, title = "Strategy Start Day")
    T_EY = input(2025, title = "Strategy End Year")
    T_EM = input(1, title = "End Month")
    T_ED = input(1, title = "Strategy End Day")
    T_S = timestamp(T_SY, T_SM, T_SD,00,00)
    T_E = timestamp(T_EY, T_EM, T_ED,00,00)
    T= ST and time >= T_S and time <= T_E 
    
    var bought = false
    longcondition = false
    shortcondition = false
    
    //once condition is met, send a buy alert and make "bought" equal to true  //to enable selling
    if (close <= 8600 and bought==false and T)
        bought := true
        longcondition :=true
    
    //once condition is met, sent a sell alert.
    if (bought and close>=9000 and T)  
        shortcondition := true
        bought := false
    
    alertcondition(longcondition,  "Long",  "Long")  
    alertcondition(shortcondition,  "short",  "short") 
    plotshape(longcondition,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.abovebar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)
    plotshape(shortcondition,  title = "Sell",  text = 'Sell',  style = shape.labelup,   location = location.belowbar, color= color.red, textcolor = color.white, transp = 0, size = size.tiny)
    // For debugging.
    plotchar(bought, "bought", "•", location = location.top)
    

    enter image description here