Search code examples
pine-scriptpine-script-v5pine-script-v4

Variable in function won't be reassigned


i'm new to Pinescript. Can sb tell me, why the float var tempHigh in the function "highestClose" won't be reassigned? I already read https://www.tradingview.com/pine-script-docs/en/v5/language/Variable_declarations.html#id4 however this does not help me. I also checked the doku on the if statment.

Code:

//@version=5
strategy("Upper Lower Sup", overlay=true)
highestClose(num) =>
    float tempHigh = 0.0
    for i = (last_bar_index - num) to last_bar_index
        if(close[i] > tempHigh)
            tempHigh := close[i]                    // <-- Here it should reassign if condition is true
    tempHigh

num = input(50, "Bars in the past")
float highpoint = highestClose(num)
var higestPriceLine= line.new((last_bar_index - num), highpoint, last_bar_index + 20, highpoint, xloc = xloc.bar_index, extend=extend.none)

Thanks^^


Solution

  • The reassignment logic is correct (although I'm not too sure on what you're trying to accomplish there, so there might be other issues), its your method of outputting the result (line.new()) is wrong.

    You create the line with the var keyword, which means that it is created on the first bar (bar_index == 0) and left untouched after that. On the first bar, the highpoint is always 0.0 with the way you wrote the script. Let's say your chart has 2000 bars, and so the last_bar_index is 1999 -- close[i] in your loop will refer to values in range from close[1949] to close[1999]. On the first bar, none of these values exist (na will be returned), so the if close[i] > tempHigh condition will always be false. Thus, the line created on bar 0 will be created with y = 0 and will not be changed after that. Even after highpoint is reassigned, the line will not reflect it, because there is no directive from the script to change the line.

    The easier (in this case) way to visualise the value of the line is to add the plot(highpoint) to the end of the script. plot() takes the value of the variable on each bar and outputs it on the chart. If you do so, you'll see that on the last 50 bars, highpoint actually changes from 0 to a different value.

    Another thing to note is that, from what I can gather, you approach the task incorrectly. If your goals is to output the high of the last 50 bars on the chart, the for i = (last_bar_index - num) to last_bar_index is not the way to go. In this implementation, you access bars waaaay back in the past, because close[1950] is not the close of the bar 1950, but the close of the bar 1950 bars back from the one where the script is calculated.

    A more fitting way would be to only execute the logic on the last bar, cycle through the last 50 bars there, reassign the values and modify the line:

    //@version=5
    indicator("My script", overlay=true)
    barsBack = input(50)
    var highestClose = 0.0
    var line1 = line.new(na, na, na, na) // a dummy line created on the first bar, we'll move it to proper coordinates later
    if barstate.islast
        for i = barsBack - 1 to 0
            highestClose := math.max(close[i], highestClose)
        line.set_xy1(line1, bar_index[barsBack], highestClose)
        line.set_xy2(line1, bar_index, highestClose)
    

    I hope this helps.