Hi Tom,
The doc you pointed has a great example on the subject that looks back a set number of days but needs to be modified. If you were to change the history line to this:
hist = data.history(context.securities, fields=["price", "volume"], bar_count=640, frequency="1m")
the history variable now has the data for at the very least 1 futures trading day (which is approximately 640 minutes). The next step is figuring out how many bars are in this trading day so far. I would write a line of code at the beginning of the handle_data function (that I can only assume you are calling every minute using schedule_function()) that looks like this:
rn = pd.to_datetime(get_datetime(), utc=True).time()
This will get you the current time in the backtest. Now you only have to figure out how many minutes are between the start of the trading day and right now. You can compute this very easily, first by getting a time variable for 6:30 AM (start of futures trading day). To set this up and only run it once, write this line in your initialize code:
context.trading_day=time(6, 10, 30)
I believe datetime as a module is fully imported in the back tester, but if not import it at the top. Now to get the time difference you can subtract one time object from the other, get the number of seconds and divide by 60:
duration = datetime.combine(date.min, context.trading_day) - datetime.combine(date.min, rn)
minutes=int(duration.seconds/60)
Now you have the number of minutes between the start of the trading day and the time right now. To get the VWAP of that period of time, just call it in the line defining the VWAP in handle_data():
vwap= vwap(hist["price"][-minutes:], hist["volume"][-minutes:])
Let me know if this was confusing, here is a brief summary to be clear: (1) initialize a program wide variable of sorts in initialize that defines the start of the trading day, (2) modify the hist variable so it gets minute pricing for the last trading day, (3) get the current time, called rn in my example, (4) define the time between the start of the trading day and the time right now in minutes and (5) define your VWAP for that period of time!
Comment again if you need assistance. Keep in mind our futures data is in an experimental stage, I know for a fact that for some contracts we are missing data. Also, futures contracts are traded differently on different exchanges, so if you want to be sure that 6:30 is right for your contract look on cme.com under "Contract Specs".
Thanks,
Tanay Trivedi