Back to Community
output of one batch transform as the input of another?

Maybe my dataframe is rusty or maybe I just don't understand what the output of a batch transform is, but I am having trouble figuring this out.

Use case is simple:

  • data has say three stocks
  • I want to calculate the historical 10-day volatility (using std() for instance) of each stock
  • then I want to calculate the 5 day average of those volatilities

There must be an elegant way to do this that doesn't involve loops?


3 responses

Hi Simon,

I suppose you mean the 10-day rolling volatility?
I haven't run this but hopefully it will get the idea across:

@batch_transform(window_length=numpy.inf, compute_only_full=False) # will never clear out old events and update from the beginning  
def avg_volatility(datapanel):  
    vol = pd.rolling_std(datapanel.prices, 10)  
    return pd.rolling_mean(vol, 5)  



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.

Yeah I found the same thing, but if I am going to use the rolling functions, what is the point of having them in a batch_transform in the first place? Or is that just because I wouldn't normally have a datapanel...

Or is that just because I wouldn't normally have a datapanel...

Yes, exactly. The central predicament is that we can not rely on an array of data being present. It was a central design choice of zipline for it to be streaming based. This has many benefits like being closer to reality, out-of-the-box support for storing the historical data in a db and streaming it (like we do on quantopian; it'd require way too much ram otherwise) and making it easy to make the jump to paper and eventually live trading. The down-side is that the style people normally process data (which is array based) doesn't work. Hence the batch_transform.

The name is actually not the best. It's really just an event accumulator. Once this PR is in: the batch_transform will also be much much faster. Then things like TAlib integration start making more sense too.