I am trying to collect statistics for days of the week similarly to what is described in Larry Williams book and I am having some trouble collecting them properly in TradingView. I am using blocks like the one below for each day of the week however when looking at forex pairs that have no data for Saturday/Sunday I will have no data for Friday/Saturday.
indicator("My script", overlay = true)
fridayTotal = 0.0
if timeframe.isdaily and barstate.islast
for i = bar_index to 1
if dayofweek.friday == dayofweek(time[i])
fridayTotal += close[i]
label.new(bar_index, close, str.tostring(fridayTotal))
I would expect the script above to collect all the closing prices for Fridays and return the total however it returns 0. Change the day to any of Sun-Thurs and you get the expected result.
I suspect the issue is that bars open on one day but close on the next day. How would I get the bar labeled as Friday to be the bar I am working with when doing dayofweek.friday == dayofweek(time[I])
?
I suspect the issue is that bars open on one day but close on the next day.
Yes, because dayofweek
extracts the day of week from the timestamp of the bar's open, and the bar's open on Forex is one day before the bar close.
To work around this, you can use dayofweek(time_close("1D"))
instead -- the function will calculate day of week based on the timestamp of the daily bar's close.