Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Dealing with time

Hi all
I am new here
I have two questions remained unsolved.
I understand that there is an mavg function
How do I calculate weekly moving average?
How do I look up for the close price few days ago
(Well if I can get the closing price retrospectively I can calculate weekly average myself) Momo

3 responses

Hello Momo,

Welcome. The built-in moving average transform is computed on a rolling basis:

mavg(days) calculates the moving average of the security's price over
the given number of trading days.

So, for days = 5, you should get a "weekly" moving average (if you run the backtest on daily data).

You can also access a trailing window of data using the batch transform. With daily data, it is straightforward to obtain the close price N days ago. With minute data, it is also possible, but requires some additional filtering logic.

I've attached an example of the batch transform. If you look at the log output, hopefully you can get a feel for what's going on.

I recommend that you get a simple algorithm running and then post the result to this forum, with remaining questions. You just need to do a "Run Full Backtest" and the version of the algorithm and its results will be saved. Then, you can either do a "Share Results" or "Add Backtest" (which are buttons on the backtest result screen and the community forum editor toolbar, respectively). If you can't get the backtest to run, just post the code directly into editor, like this:

import numpy as np

# globals for get_data batch transform decorator  
R_P = 1  # refresh period in days  
W_L = 3  # window length in days

def initialize(context):  
    context.stocks = [sid(21519),sid(8554)]  
def handle_data(context, data):  
    # get data  
    d = get_data(data,context.stocks)  
    if d == None:  
       return  
    prices = d[0]  
    volumes = d[1]  
    log.debug(prices)  
    log.debug(volumes)  
@batch_transform(refresh_period=R_P, window_length=W_L) # set globals R_P & W_L above  
def get_data(datapanel,sids):  
    p = datapanel['price'].as_matrix(sids)  
    v = datapanel['volume'].as_matrix(sids)  
    return [p,v]  

Grant

Actually the function I want to do is something else.
I just want to do a moving average just to test out how to reference data retrospectively.

Batch transform is precisely what I need

@batch_transform(window_length=3)
def get_averages(datapanel):
# get the dataframe of prices
prices = datapanel['price']
# return a dataframe with one row showing the averages for each stock.
mean = prices.mean()

Thank you!

Momo,

You're welcome.

Here's another approach:

@batch_transform(refresh_period=R_P, window_length=W_L) # set globals R_P & W_L above  
def get_average(datapanel,sids):  
    p = datapanel['price'].as_matrix(sids)  
    p_avg = np.average(p,axis=0)  
    return p_avg  

I prefer, as much as possible, to use numpy/scipy for any number crunching.

Grant