0

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.

  • Here are the screen shots: https://www.dropbox.com/s/wtncg0sz3ezlzri/Screenshot%202022-11-20%2020.38.28.png?dl=0 https://www.dropbox.com/s/bc472xsxvmgec3n/Screenshot%202022-11-20%2020.38.42.png?dl=0 – Adrian Ortiz Nov 21 '22 at 16:43
  • Try change the `newSession` variable to `newSession = ta.change(time("D"))` – mr_statler Nov 21 '22 at 16:53
  • Thank mr_statler, but the sets VWAP to the end of the trading day, not to midnight. – Adrian Ortiz Nov 21 '22 at 17:49
  • I got this from TradingView support, but I am having trouble understanding how to implement their solutionL "The script you have provided is working as expected, it's using the strict comparison of any particular bar with a specific time, in case there is no candle with such a timestamp on the chart, newSession variable will return 0. The first candle on the ZF1! futures on November 18 starts at 00:00:30, hence doesn't satisfy the given criteria. Use the non-strict comparison >= (Greater Than or Equal To) or use the built-int timeframe.change() function to detect the change of the day." – Adrian Ortiz Nov 21 '22 at 17:53
  • Weird. You can also try `newSession = dayofweek != dayofweek[1]`. What's your ticker and timeframe? – mr_statler Nov 21 '22 at 17:55
  • Ticker is ZF, 30 second time frame – Adrian Ortiz Nov 21 '22 at 18:11

1 Answers1

0

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]
mr_statler
  • 1,913
  • 2
  • 3
  • 14
  • Unfortunatly I tried "newSession = ta.change(time("D"))" but this only reset it to the end of the trading day, for ZF, this was 6pm EST. I will try the others, the last one seems great, in case I want to reset it at a specific time other than midnight – Adrian Ortiz Nov 21 '22 at 18:09
  • midnight = hour == 0 and minute == 0 newSession = midnight and not midnight[1] This seems to have worked!!! Thank you very much every body! – Adrian Ortiz Nov 21 '22 at 18:14
  • BTW, could you explain why newSession = midnight and not midnight[1] works like it does. I dont understand what the AND NOT does in this logic – Adrian Ortiz Nov 22 '22 at 16:28