Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
example code for daily VWAP?

Does anyone have example code to share that computes the daily VWAP using minute bars, across a universe of securities?

Presently, I'm using the code below (which isn't daily VWAP), but I'd like to try using daily VWAP values instead.

prices = data.history(context.stocks, 'price', 390*context.N, '1m').dropna(axis=1)  
context.stocks = list(prices.columns.values)  
prices = prices.ewm(com=390).mean().as_matrix(context.stocks)  

The computation looks pretty straightforward (e.g. see http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:vwap_intraday ), but I'm lazy and perhaps someone has some efficient code to share (and it is a little messy, since one has to deal with the pandas datetime stamps, and chunk the data by trading day).

3 responses

I was just looking into this actually. I'm trying to compose an algo based on vwap.

This seems to work.

def vwap(data,context,s):  
    hist = data.history(s, fields=["price", "volume"], bar_count=390, frequency="1m")  
    if hist["volume"].sum() > 0:  
        return (hist["price"] * hist["volume"]).sum() / hist["volume"].sum()  
    else:  
        hist["price"][-1]  

Anybody found this? I am trying to write an algorithm on basis of percentage change in vwap. Is there a way I can capture percentage change in vwap?