Noob - calculating VWAP from bars data

Im a pretty experienced day trader and have a system i want to implement in code, but im not much good at programming yet.
I am trying to work out how to use the data returned from:

api.get_barset(‘AAPL’,‘minute’, limit=30).df

to calculate VWAP, roll it up up into 2, 3 5 minute bars and generally use it for calculation.

What code needs to be added to, after or around the command above to get to using the returned values.

Thanks for your help.

If this question is way off, someone please say so… if there are any programmers looking for work, i’d like to get this coded.

I’ll work out the details and let you know how it works!

Thanks for responding :slight_smile: I wondered if either the forum was dead, or if i’d asked such a stupid or offensive question that it wouldnt get answered.

I have looked through other’s code and seen things like bars[-1] to pull the last bar data, but this doesnt work when i try it. for example, something like:

for symbol in watchlist:
bars = api.get_barset(symbol,‘minute’,limit=10).df
bar = bars[-1]

i can print bars, but not bar.

As far as the calculation goes, its straightforward to roll up into different time increments in excel, but notice that the data has holes in it through the API when pulled as ‘minute’ (i.e. some minutes missing).

for example (NaN for the missing data at 15:56)

open high low close volume
2019-12-06 15:56:00-05:00 NaN NaN NaN NaN NaN
2019-12-06 15:57:00-05:00 76.235 76.27 76.210 76.210 1832.0
2019-12-06 15:58:00-05:00 76.220 76.25 76.190 76.200 1179.0
2019-12-06 15:59:00-05:00 76.215 76.25 76.205 76.205 10587.0

This defeats my simple logic in excel. :slight_smile:

If there is some sort of append command in python for adding vwap to each bar data, that would permit the real goal of comparing each bar to the vwap value.

Im gradually coming up to speed and expect to eventually understand any technical response, but feel free to include some explanation for the noob.

Cheers, Steve.

Here is the indicator implemented with backtrader:

1 Like

You should look at the alpaca API docs. You are limiting the number of bars to 10. If you want to day trade there are more than 10x5min bars in a day. Also a good pandas tutorial will help allot! I know because I’ve been right where you are!! :grinning:

1 Like

FYI: alpaca API docs:

1 Like

Thanks. I see in the API docs that 5min bars are available. I’ll see if i can make progress with that for now.

REST.get_barset(symbols, 5Min, limit, start=None, end=None, after=None, until=None)

I’ll check out the other tutorial suggested too.

I worked out how to append the VWAP, and im happy to share it here as I hope to use this forum to get more help in the future. If anyone has any improvements please chime in:

This calculates the VWAP value and enters it to a new column called VWAP in bars.

bars[“VWAP”] = (bars.close*bars.volume).cumsum()/bars.volume.cumsum()

This is still scuppered by the missing data, but that seems to be a problem outside of the python code. If anyone knows how to prevent the missing data, i’m interested to hear how you did it.


I built a sample trade off the bactrader example. I can show you how i got the data if you need. Here is the link:

I was looking at the examples for a streaming connection. (
I found this:
come to find out polygon provides it in it’s stream data!!!

 // VWAP (Volume Weighted Average Price)

see here:

WHOLEY COW!!! Do you know how long I have been coding on this?!?!?!
Dammit JIM!!!

The code to resample is:

barset = api.get_barset(“AAPL”, ‘1Min’).df

columns_dict = {‘open’: ‘first’, ‘high’: ‘max’, ‘low’: ‘min’,
‘close’: ‘last’, ‘volume’: ‘sum’}

for symbol in watchlist:
barsx = barset[symbol]
bars = barsx.resample(‘2T’).agg(columns_dict)
print("bars " + symbol)

note the reset of the index is important here because of the dataframe returned by the API. If you dont reset the index it blows up into some 20 columns which if you search github is everyone’s problem, but there is no solution listed there. (so im feeling pretty pleased withmyself considering im such a noob :slight_smile: )

note that 2T means 2 minutes, replace with any time frames or dates appropriate to your need: S for seconds, T for minutes, D for days, W for weeks and so on…

1 Like