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.

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.

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
smaList.append(np.average(vclose))
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]
else:
avg = None
log.debug(stock)
log.debug(event_time)
log.debug(avg)
@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.

Grant

Please sign in or join Quantopian to post a reply.