I try to build an indicator to get the ranges of candles by the same day of week, hour and minute, then to compare the current candle range with the average.
I use last_bar_time to get the bar time of last candle displayed by the chart.
When the engine loops through bar_indexes from 0 till the end, if the dayofweek, hour and time are matched with last_bar_time details, store the values in an array.
At first iteration of script it works ok, but when i play Next candle in replay or is a new candle in realtime, data is not updated in array.
I attached code where i store candle details (day hour min) and is not working ok. Thank you.
//@version=5
indicator('Ca Range', "Ca Range", overlay=true, max_lines_count=500, max_boxes_count=500, max_labels_count=500 , max_bars_back = 4999 )
TIMEZONE = 'America/New_York'
int timenow_year = year( timenow, TIMEZONE )
int timenow_month = month( timenow, TIMEZONE )
int timenow_dayofmonth = dayofmonth( timenow, TIMEZONE ) // 1...31
int timenow_dayofweek = dayofweek( timenow, TIMEZONE )
int timenow_hour = hour( timenow, TIMEZONE )
int timenow_minute = minute( timenow, TIMEZONE )
int timenow_seconds = second( timenow, TIMEZONE )
int time_year = year( time, TIMEZONE )
int time_month = month( time, TIMEZONE )
int time_dayofmonth = dayofmonth( time, TIMEZONE ) // 1...31
int time_dayofweek = dayofweek( time, TIMEZONE )
int time_hour = hour( time, TIMEZONE )
int time_minute = minute( time, TIMEZONE )
int time_seconds = second( time, TIMEZONE )
string time_ymd = str.format_time(time, "yyy-M-d", TIMEZONE )
string time_day_name_long = str.format_time(time, "EEEE", TIMEZONE )
int last_bar_time_year = year( last_bar_time, TIMEZONE )
int last_bar_time_month = month( last_bar_time, TIMEZONE )
int last_bar_time_dayofmonth = dayofmonth( last_bar_time, TIMEZONE ) // 1...31
int last_bar_time_dayofweek = dayofweek( last_bar_time, TIMEZONE )
int last_bar_time_hour = hour( last_bar_time, TIMEZONE )
int last_bar_time_minute = minute( last_bar_time, TIMEZONE )
int last_bar_time_seconds = second( last_bar_time, TIMEZONE )
var arr_candles_by_dayofweek_hour_min = array.new_string()
if array.size(arr_candles_by_dayofweek_hour_min) > 2
array.shift(arr_candles_by_dayofweek_hour_min)
ca_0_range_id_day_name_cond =
time_hour == last_bar_time_hour
and time_minute == last_bar_time_minute
and time_dayofweek == last_bar_time_dayofweek
if ca_0_range_id_day_name_cond
array.push(arr_candles_by_dayofweek_hour_min,
""
+ str.tostring(time_ymd)
+ " "
+ str.tostring(time_day_name_long)
+ " "
+ str.tostring(time_hour)
+ ":"
+ str.tostring(time_minute)
)
var label lbl_3 = na
label.delete(lbl_3)
lbl_3 := label.new (barstate.islast ? bar_index : na,
y=close,
xloc=xloc.bar_index,
yloc=yloc.abovebar,
style = label.style_label_down,
size = size.large,
color = color.new(color.aqua, 60),
textcolor = color.new(color.black, 0),
textalign = text.align_left,
text = ""
+ str.tostring( arr_candles_by_dayofweek_hour_min )
// + "n" + str.tostring( timenow_hour )
,
tooltip = "" + str.tostring( bar_index )
)