@batch_transform(refresh_period=R_P, window_length=W_L) # set globals R_P & W_L above
p = np.flipud(datapanel['price'].as_matrix(sids))
p_cumsum = np.cumsum(p,axis=0)
n = np.cumsum(np.ones(p_cumsum.shape),axis=0)
avg = np.divide(p_cumsum,n)
The first line, p = np.flipud(datapanel['price'].as_matrix(sids)), creates a numpy ndarray of prices, and flips it about a horizontal axis ("up down") so that the most recent prices come first. Then, the next line, p_cumsum = np.cumsum(p,axis=0), performs a cumulative sum over the columns. The next line, n = np.cumsum(np.ones(p_cumsum.shape),axis=0), creates an ndarray of the denominators, required to compute the averages of each element of p_cumsum. Finally, an elementwise division is performed to compute the averages, with avg = np.divide(p_cumsum,n). So, what is returned is an ndarray of moving averages--columns correspond to sid, and rows correspond to the trailing window length of the average. So, by picking row and column, as I do in handle_data, the moving averages for each sid can be compared to those computed with mavg. In the example code, they agree, so I declare victory.
Just let me know if this doesn't make sense, and I'll work up a better example, with comments in the code.