I’m new to pine script.
I want to detect the imbalance of bar 2
I wrote this script:
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © myself
//@version=5
indicator("My first try", overlay=true)
find_imbalance(h1, h3, l1, l3) =>
var int rv = 0
if h3 < l1
rv := 1
if l3 > h1
rv := -1
rv
var label myLabel = label.new(bar_index, high + ta.tr, textcolor=color.white, color=color.blue)
if barstate.islast
log.info("{0} - cur: h={1}, l={2}, bar[1]: h={3}, l={4} bar[3]: h={5}, l={6}", bar_index, high, low, high[1], low[1], high[3], low[3])
label.set_x(myLabel, bar_index)
label.set_y(myLabel, high + ta.tr)
var int ib = find_imbalance(high[1], high[3], low[1], low[3])
if ib > 0
label.set_text(myLabel, text="Imbalance Long")
else if ib < 0
label.set_text(myLabel, text="Imbalance short")
else
label.set_text(myLabel, text="Bars onnthe chart:n" + str.tostring(bar_index + 1))
which produced this logs:
[2024-05-30T18:00:00.212+02:00]: 11,860 - cur: h=5,261.5, l=5,261.5, bar[1]: h=5,263, l=5,261.25 bar[3]: h=5,264, l=5,263.25
[2024-05-30T18:00:00.212+02:00]: 11,860 - cur: h=5,261.5, l=5,261.25, bar[1]: h=5,263, l=5,261.25 bar[3]: h=5,264, l=5,263.25
Values from log indicate, that imbalance could be detected, but label shows text from else condition (no imbalance found)
What am I doing wrong?