Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Iterative Batch Transforms

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

6 responses

Forgot to mention (and do), since its iterative the window_length on the iterative batch_transforms only needs to be 2, which will help speed up the Panel creation in BatchTransform. This the the backtest from the past year that did that. ;)

Happy Hacking!

7-11

Hi Jason,

That's a really neat idea, thanks for sharing! We're currently tweaking performance of the batch_transform a bit and this would be a nice enhancement. Would you mind if we used that code in zipline which will also make it available to the rest of Quantopians?

Thomas

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

No problem, I had it on my todo list to write tests and was going to do a PR, but haven't had time yet. If ya'll get to it prior than I do that would be awesome.

Cool, that'd be well appreciated!

Very cool indeed. Added an issue to the Zipline repo, https://github.com/quantopian/zipline/issues/115

(Jason, did I make the correct assumption that your GitHub username is @jkoelker?)

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Eddie, yep it is.