Reputation: 5
The following Pine script does not always reset VWAP at midnight. This is especially a problem when looking at bonds in the sub 1 minute scale.
I see it most on ZF in the 30 second scale. Seems to work fine most times on the 1 minute and higher bars.
================================ ` //@version=5 indicator('VWAP Midnight', overlay=true)
var float vwapsum = na
var float volumesum = na
//------------------------------------------------
Midnight = timestamp("UTC-5:00",year, month, dayofmonth, 00, 00, 00)
newSession = Midnight == time ? 1 : 0
//------------------------------------------------
vwapsum := newSession ? hl2 * volume : hl2 * volume + vwapsum[1]
volumesum := newSession ? volume : volume + volumesum[1]
myvwap = vwapsum / volumesum
coloring = color.aqua
av = myvwap
A = plot(av, style=plot.style_circles, color=color.new(coloring, 0))`
=======================================
Also, if I change the "UTC-5:00" to "UTC+3:00" it works fine. Even "UTC-4:00" does not work. I am located in San Diego (PST), but my graph is set to New York Time (EST). I posted some screenshots of the graph and the code and how it was showing VWAP on the graph. On one of the screenshots, you can clearly see a break in the VWAP line at the coded time based on UTC.
I tried to reach out to TradingView support, but they said it was working fine.
Let me know if you can not replicate the issue.
I tried to have VWAP reset at midnight NYC time, instead of 6pm NYC time as is the VWAP default.
Upvotes: 0
Views: 373
Reputation: 2171
TV answer makes complete sense. Since you are looking for a bar that it's open time is exactly midnight, there are some cases where this will return false even if it's the first bar of the day.
This will happen in cases where there is no bar with open time that is exactly midnight (for example if there are no trades in this bar time). You can use one of the following solutions:
newSession = ta.change(time("D"))
or
newSession = dayofweek != dayofweek[1]
or
midnight = hour == 0 and minute == 0
newSession = midnight and not midnight[1]
Upvotes: 0