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