Asstoped
Asstoped

Reputation: 15

Pinescript variable doesn't change its value

To make it simple, I'll give another piece of code.

currentRSI = ta.rsi(close,14)
var tradeExists = 0
if (currentRSI > 50 and tradeExists == 0)
    tradeExists := 1
    alert("Long trade")

In my case, if currentRSI crosses over 50, so it gets to 51, AND in the same candle of the timeframe it gets to 49.5, the tradeExists value will remain 0 but the alert has been sent. How could I fix to detect that and close the trade, any idea if I can do this?

I want to specify that I also tried using varip tradeExists = 0 but the variable still gets rollback at the close of the candle.

Upvotes: 1

Views: 432

Answers (1)

Mahdi
Mahdi

Reputation: 11

I just had that problem and I fixed it: use the barstate.isconfirmed in your conditions, it's the only way to change a var in pinescript.

if (barstate.isconfirmed and first_time_signal == false)
    if nT3Average >= ok_to_buy and nT3Average_2 <= oversold 
        last_signal := 1
        label.new(bar_index,close,'Long',color=color.rgb(49, 161, 64),style = label.style_label_up,xloc=xloc.bar_index,yloc=yloc.belowbar)
        first_time_signal := true

    if nT3Average <= ok_to_sell and nT3Average_2 >= overbought 
        last_signal := -1
        label.new(bar_index,close,'Short',color=color.rgb(175, 55, 95),style = label.style_label_down,xloc=xloc.bar_index,yloc=yloc.abovebar,textcolor=color.rgb(10,10,10))
        first_time_signal := true 

Upvotes: 1

Related Questions