Back to Community
Futures Intraday VWAP calculation / All bars from session start

Hello Quantopian community!

I'm new to Q since I only just realised that there is now Futures data available and not just Equities any more.

I have been experimenting with the API and am now trying to write a first algo, but I'm struggling to figure out how to compute the correct values for intraday VWAP on the minute bars.
I've read the example at "Common usage VWAP", but having a moving window of 15 / 30 bars for VWAP calculation is not what I'm looking for.

Usually intraday vwap is calculated over all bars of the current day (or session).

How do I accomplish this here?
Is there a simple way to access intraday VWAP or to request history for "all_bars_of_current_session"?

Any input would be much appreciated!

2 responses

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 under "Contract Specs".
Tanay Trivedi


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hey Tanay!
Thanks a lot for the detailed reply, it is much appreciated!
This does look a bit more complicated than I expected, but I'll certainly give it a try.

I'll report back with any success or questions.

Have a great day & thanks again!