Getting unexpected values in the Pine script

Viewed 32

When displaying the "greatest" values to the console I found that it changes if I uncomment the last line. Moreover, it changes to a value (1709.99) that was neither in longStop nor in longStop before. Where could it come from?

Screenshot without the last line:

screenshot without the last line

Screenshot with last line:

screenshot with last line

Here is my small script:

//@version=4

strategy("CE", shorttitle="CE", overlay=true)

length = input(title="ATR P", type=input.integer, defval=22)
mult = input(title="ATR M", type=input.float, step=0.1, defval=3.0)

atr = mult * atr(length)

// debug = label.new(x = bar_index, y = close, style = label.style_label_left, text = stri)
// label.delete(debug[1])

// plot(atr, color=color.yellow, linewidth=1)
// plotchar(bar_index, "Bar Index", "", location = location.top)

longStop = highest(close, length) - atr
// /plot(close[1] - mult * atr(length)[1], title = "longStopPrev", color=color.blue, linewidth=1)
longStopPrev = longStop[1]
plotchar(longStop, "longStop", "", location = location.top)
plotchar(longStopPrev, "longStopPrev", "", location = location.top)
greatest =  max(longStop, longStopPrev)
// plot(greatest, title = "greatest", color=color.blue, linewidth=1)
plotchar(greatest, "greatest", "", location = location.top)
longStop := close[1] > longStopPrev ? greatest : longStop
1 Answers

longStop is of type series. That means, it will have historical data for each bar.

With your last line, you change the value of longStop depending on your condition.

Then on the next execution, you have this statement: longStopPrev = longStop[1]. Here you access longStop's previous value.

So, what's happening here is, with your last line, you change longStop's value and therefore longStop[1] returns a different value on the next execution.

Related