Back to Community
help w/ batch transform & multiple sids

Can give me a tip or two on how to use the batch transform with multiple sids. I'm trying to write my own moving average (see the code). It appears to be calculating the moving average of only one of the sids based on the log output.

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 50c25a127899860a3ae96b82
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
5 responses

Hi Grant,

I was trying to write an Exponential Moving Average and had the same scratch-head moment but I think the below code works. I've not tested extensively

# calculate Extended Moving Average for a given SID  
@batch_transform(refresh_period=1, window_length=34)  
def batchEMA34(data,context,activeSID):  
    c = context  
    # closing-price array, where [0] is in the past and [N-1] is most current  
    vclose = data['close'][activeSID].values  
    sma = np.average(vclose)    # simple moving average here  
    if c.lastEMA is None:  
        c.lastEMA = sma  
    if c.lastSMA is None:  
        c.lastSMA = sma  
    multiplier = 2.0 / (vclose.size +1)  
    thisEMA = ((c.lastSMA - c.lastEMA) * multiplier) + c.lastEMA  
    c.lastEMA = thisEMA  
    c.lastSMA = sma  
    return thisEMA  

Sorry I think I misunderstood in my first reply. I think you could return multiple averages by doing something like...

@batch_transform(refresh_period=1, window_length=34)  
def batchEMA34(data,sidArray):  
    smaList = []  
    for s in sidArray:  
        vclose = data['close'][s].values  
    return smaList  

hope it helps?

import numpy as np

R_P = 1  
W_L = 3

def initialize(context):  
    context.stocks = [sid(16841),sid(24)]

def handle_data(context, data):  
    avgs = get_avg(data, context.stocks)

    for i, stock in enumerate(context.stocks):  
        print "index i: {0}, stock {1}".format(i, stock)  
        event_time = data[stock].datetime  
        if avgs is not None:  
            avg = avgs[i]  
            avg = None  
@batch_transform(refresh_period=R_P, window_length=W_L)  
def get_avg(datapanel, sids):  
    avgs = []  
    for s in sids:  
        avgs.append( np.average(datapanel['price'][s].values) )  
    return avgs  

Must stop drinking coffee ;)

Thanks James,

I'll give these a try at some point in the next day or two and let you know how things work out.