the arrays DZarr and SZarr do not get filled when using array.push().
I check the array size of DZarr on each bar by using label as a kind of printf and it always shows 0. Its weird since the boxes get displayed on screen but somehow cant just get into the arrays.
//@version=5
//This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
indicator('simple OBs', overlay=true, max_boxes_count=500, max_lines_count=500)
// functions
DZ()=>
r = (close[2] < open[2]) and (close[1] > open[1]) and (close[1] > high[2]) ? 1 : 0
SZ()=>
r = ( close[2] > open[2]) and (close[1] < open[1]) and (close[1] < low[2]) ? 1 : 0
limit = 10
box[] DZarr = array.new_box()
box[] SZarr = array.new_box()
if DZ()
DZ = box.new(left=bar_index-2, right=bar_index, top=high[2] ,bottom=math.min(low[2],low[1]), border_color = color.black, bgcolor = color.new(color.lime, 75))
array.push(DZarr,DZ)
if SZ()
SZ = box.new(left=bar_index-2, right=bar_index, top=math.max(high[2],high[1]) ,bottom=low[2], border_color = color.black, bgcolor = color.new(color.red, 75))
array.push(SZarr,SZ)
l=label.new(x=bar_index,y=close, yloc=yloc.belowbar, color=color.white,textcolor=color.black, text=str.tostring(array.size(DZarr)), size=size.large)
>Solution :
You need to use the var keyword while declaring your arrays. Otherwise your arrays will be initialized on each bar.
var is the keyword used for assigning and one-time initializing of the
variable.
var box[] DZarr = array.new_box()
var box[] SZarr = array.new_box()