Search code examples
pine-scriptpine-script-v5

Pine Script code converted from v.1 to v.5 not working as intended


I've tried to convert this Pine Script v.1 code from the first version to the v.5 and it seems to not work as it would do with the v.1 version. I've tried to apply the most frequent changes needed top convert the v.1 code in the v.5 code so far but it's still not prompting the right signals.

V.1 CODE

study("Volatility Stop", shorttitle="VStop1", overlay=true)
length = input(20)
mult = input(2)
atr_ = atr(length)
max1 = max(nz(max_[1]), close)
min1 = min(nz(min_[1]), close)
is_uptrend_prev = nz(is_uptrend[1], true)
stop = is_uptrend_prev ? max1 - mult * atr_ : min1 + mult * atr_
vstop_prev = nz(vstop[1])
vstop1 = is_uptrend_prev ? max(vstop_prev, stop) : min(vstop_prev, stop)
is_uptrend = close - vstop1 >= 0
is_trend_changed = is_uptrend != is_uptrend_prev
max_ = is_trend_changed ? close : max1
min_ = is_trend_changed ? close : min1
vstop = is_trend_changed ? is_uptrend ? max_ - mult * atr_ : min_ + mult * atr_ : vstop1
plot(vstop, color = is_uptrend ? green : red, style=line, linewidth=2)
buysignal = (is_trend_changed == true and close - vstop1 >= 0)
sellsignal = (is_trend_changed == true and close - vstop1 < 0)//and close -vstop1<0)
plotshape(buysignal, "Signal", style=shape.circle, location = location.belowbar, color=green, text="BUY", textcolor=white,  size = size.normal) 
plotshape(sellsignal, "Signal", style=shape.circle, location = location.abovebar, color=red, text="SELL", textcolor=white, size = size.normal) 

V.5 CODE

//@version=5
indicator(title='Volatility Stop', shorttitle='VStop1', overlay=true)

length = input(20, title='Length')
mult = input(2, title='Multiplier')

atr_ = ta.atr(length)

var float max1 = 0.0
var float min1 = 0.0
var bool is_uptrend_prev = 0
var float stop = 0.0
var float vstop_prev = 0.0
var float vstop1 = 0.0
var bool is_uptrend = 0
var bool is_trend_changed = 0
var float max_ = 0.0
var float min_ = 0.0
var float vstop = 0.0
max1 := math.max(nz(max1[1]), close)
min1 := math.min(nz(min1[1]), close)
is_uptrend_prev := nz(is_uptrend_prev[1], true)
stop := is_uptrend_prev ? max1 - mult * atr_ : min1 + mult * atr_
vstop_prev := nz(vstop_prev[1])
vstop1 := is_uptrend_prev ? math.max(vstop_prev, stop) : math.min(vstop_prev, stop)
is_uptrend := (close - vstop1 >= 0)
is_trend_changed := (is_uptrend != is_uptrend_prev)
max_ := is_trend_changed ? close : max1
min_ := is_trend_changed ? close : min1
vstop := is_trend_changed ? is_uptrend ? max_ - mult * atr_ : min_ + mult * atr_ : vstop1
//plot(vstop, color=is_uptrend ? color.green : color.red, style=plot.style_line, linewidth=2)

buysignal = is_trend_changed == true and close - vstop1 > 0
sellsignal = is_trend_changed == true and close - vstop1 <= 0

plotshape(buysignal, 'Signal', style=shape.circle, location=location.belowbar, color=color.new(color.green, 0), text='BUY', textcolor=color.new(color.white, 0), size=size.normal)
plotshape(sellsignal, 'Signal', style=shape.circle, location=location.abovebar, color=color.new(color.red, 0), text='SELL', textcolor=color.new(color.white, 0), size=size.normal)

I've tried converting the code by manually change the self referencing variables and I've also checked if there was any other major change to be needed to convert the code but it seems ok and when I've used the Pine Script convert function the code was running without prompting an error. Anyways the signals prompted are incorrect if compared to the signals triggered by the v.1 code.


Solution

  • You have copy&paste mistakes and also you don't need to make those variables as var.

    This will give you the same results:

    //@version=5
    indicator(title='v5', overlay=true)
    
    length = input(20, title='Length')
    mult = input(2, title='Multiplier')
    
    atr_ = ta.atr(length)
    
    float max1 = 0.0
    float min1 = 0.0
    bool is_uptrend_prev = false
    float stop = 0.0
    float vstop_prev = 0.0
    float vstop1 = 0.0
    bool is_uptrend = 0
    bool is_trend_changed = 0
    float max_ = 0.0
    float min_ = 0.0
    float vstop = 0.0
    max1 := math.max(nz(max_[1]), close)
    min1 := math.min(nz(min_[1]), close)
    is_uptrend_prev := nz(is_uptrend[1], true)
    stop := is_uptrend_prev ? max1 - mult * atr_ : min1 + mult * atr_
    vstop_prev := nz(vstop[1])
    vstop1 := is_uptrend_prev ? math.max(vstop_prev, stop) : math.min(vstop_prev, stop)
    is_uptrend := (close - vstop1 >= 0)
    is_trend_changed := (is_uptrend != is_uptrend_prev)
    max_ := is_trend_changed ? close : max1
    min_ := is_trend_changed ? close : min1
    vstop := is_trend_changed ? is_uptrend ? max_ - mult * atr_ : min_ + mult * atr_ : vstop1
    plot(vstop, color=is_uptrend ? color.green : color.red, style=plot.style_line, linewidth=2)
    
    buysignal = is_trend_changed == true and close - vstop1 > 0
    sellsignal = is_trend_changed == true and close - vstop1 <= 0
    
    plotshape(buysignal, 'Signal', style=shape.circle, location=location.belowbar, color=color.new(color.green, 0), text='BUY', textcolor=color.new(color.white, 0), size=size.normal)
    plotshape(sellsignal, 'Signal', style=shape.circle, location=location.abovebar, color=color.new(color.red, 0), text='SELL', textcolor=color.new(color.white, 0), size=size.normal)