from pytz import timezone
import matplotlib.pyplot as plt
import pandas as pd
# get minute bar volume data for SPY
# assumption is that empty bars can be filled with zeros, corresponding to no trading volume
data = get_pricing(['SPY'],start_date='2002-01-01',end_date='2015-09-18',fields='volume',frequency='minute').fillna(0)
data.tail(3)
# add time & date columns to the data, for pivot table
data['time'] = data.index.tz_convert(timezone('US/Eastern')).time
data['date'] = data.index.date
data.tail(3)
# on a rolling basis, compute volume z-scores using a trailing window of width w minutes
# credit to Quantopian user Michael Van Kleeck for this efficient code
# ref.: https://www.quantopian.com/posts/modified-heatmap-based-on-grant-kiehnes-example
w = 390
volumes = data.iloc[:, 0:1]
means = volumes.apply(lambda x: pd.rolling_mean(x, window=w))
sds = volumes.apply(lambda x: pd.rolling_std(x, window=w))
zs = ((volumes - means) / sds)
data['z-score'] = zs
data.tail(3)
# apply a pivot table, with trading day along the vertical & trading time along the horizontal
# fill empty minutes with zeros
ht_map = pd.pivot_table(data,'z-score',index=data['date'], columns=data['time'],fill_value=0)
ht_map.tail(3)
# plot heat map of z-scores
# see http://matplotlib.org/examples/color/colormaps_reference.html to switch colormap
plt.imshow(ht_map,cmap='jet',aspect=0.1)
plt.grid(False)
plt.colorbar()
plt.clim(ht_map.min().min(),ht_map.max().max())
plt.title('SPY volume z-score, 390-minute rolling window, 2002-present')
plt.xlabel('trading minute')
plt.ylabel('trading day')