Batch Transforms are awesome for computing a series of values, but with large changing datasets performance can be quite spotty. If the transform can be written in an iterative fashion with EventWindows, performance is more consistent, but you lose the batch nature and speed when iterating over the list of securities updating all the windows. I've written a little decorator that combines the two so my algorithm doesn't have to keep track in the context or wherever the previous batch result. Sharing in case others might find it useful.
Not included in this backtest is the iterative ema as shown here:
@iterative_batch_transform
@batch_transform(window_length=EMA_LONG_WINDOW)
def ema_long(d, prev):
return _iema(d, prev, span=EMA_LONG_WINDOW)
def _iema(d, prev=None, span=None):
if prev is None:
return d['close_price'].ix[-1]
alpha = 2.0 / (span + 1)
return prev + alpha * (d['close_price'].ix[-1] - prev)
Happy Hacking!
7-11