Andrey Kanava
Andrey Kanava

Reputation: 57

Incorrect value of ta.lowest(12) pine script

I need to know the lowest price for 12 bars so i use ta.lowest(12) but i got incorrect value. My code:

order_price = ta.lowest(12)
lbl = label.new(bar_index, na)
label.set_text( lbl, "order="+str.tostring(order_price))
label.set_color(lbl, color.green)
label.set_yloc( lbl, yloc.belowbar)
label.set_style(lbl, label.style_label_down)

and how it looks on chart: enter image description here

Upvotes: 0

Views: 563

Answers (1)

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6865

I'm guessing you're using this inside an if statement. Something like this:

if myCondition
    order_price = ta.lowest(12)
    lbl = label.new(bar_index, na)
    label.set_text( lbl, "order="+str.tostring(order_price))
    label.set_color(lbl, color.green)
    label.set_yloc( lbl, yloc.belowbar)
    label.set_style(lbl, label.style_label_down)

In that case, you'll probably have seen this warning:

The function 'ta.lowest' should be called on each calculation for consistency. It is recommended to extract the call from this scope.

You should call ta.lowest(12) in the main code so that it can execute on every bar. If not, you're only calling it when your if statement is true, and so it'll only check the low of the bars on which you called it.

You could mitigate by something like this:

//@version=5
indicator("My script")

myLowest = ta.lowest(12)  // Call the function on every bar, in the main program

if myCondition
    order_price = myLowest  // Use the value in your if statement
    lbl = label.new(bar_index, na)
    label.set_text( lbl, "order="+str.tostring(order_price))
    label.set_color(lbl, color.green)
    label.set_yloc( lbl, yloc.belowbar)
    label.set_style(lbl, label.style_label_down)

Upvotes: 1

Related Questions