Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Confused by @batch_transform

I'm trying to better understand the @batch_transform decorator and I'm having some trouble.

For example this first test works...

@batch_transform(window_length=20, refresh_period=1)  
def myTest(datapanel):  
    return datapanel['price']

def initialize(context):  
    context.stock = sid(23911)  
    pass

def handle_data(context, data):  
    log.debug(myTest(data))

However, this next example returns: "Runtime exception: KeyError: 'price'"

def initialize(context):  
    context.stock = sid(23911)  
    pass

def handle_data(context, data):  
    log.debug(data['price'])  

I would expect that one should return a 20 day list of the price while the other would just yield a single day.

4 responses

So I think I'm starting to understand...

The decorator changes the "data" object I pass into the function from a class: zipline.protocol.BarData object into a class: pandas.core.panel.Panel object.

Is the code showing the decorator declaration available somewhere? Just so we know what is under the hood?

Hi Pumplerod,

That's correct. Here is the code: https://github.com/quantopian/zipline/blob/master/zipline/transforms/batch_transform.py

But note that we are currently in the process of refactoring batchtransform:
https://quantopian.com/posts/draft-proposal-of-new-data-history-api

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.

That will take me a while to wrap my head around. ;)

it seems that while getting price or volume data is something batch_transform handles, getting results of functions such as results() and mavg() are not. Is that correct? Forgive me if I've missed that in the references you site. I haven't completely read through or digested all of it yet.

This is a little more clear in zipline, but the data you access inside of handle data is a bar object that is essentially a dictionary holding the current information for that particular day. while to access the historical data, I believe you call

SomeBatchTransform.handle_data(data)  

So as the quantopian ide is somewhat of a wrapper to the zipline library, I imagine when you pass data to your batch_transform it is smart enough to call the handle_data method of the transform and thus return the pandas object, with Multindex: level - SIDS, level - Price, Volume, etc.
Therefore you can index by price, however the bar object inside def handle_data(self, data) has to first be indexed by the SID to get access to the current data.
So data.sid.price is valid