Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Local variable within CustomFactor class

Hi community,

I'm very new to finance algorithms in general and quantopian specifically. As a first exercise I'm trying to implement Heikin Ashi candles which seem to work fine outside of pipelines. If I want to put this code into a pipeline I guess I need to create a CustomFactor. The challenge I have with this is: how do I store data locally to the CustomFactor class instance?

The reason for this is: when caclulating HeikinAshi candles you always need data from the last days. And to make the calculations somewhat correct, you need to go back at least 10 days. So I could solve this by simple providing a window_length of 10 to my CustomFactor but then everyday for every stock I have to recalculate candles for the last 10 days. If I were to calculate the initial 10 days, store the candle data and then when the pipeline runs only use a window-length of 1 and use the stored candle history within the CustomFactor it should be much more efficient.

I already tried to put in a variable into the class (I tried both with the "self" and without the "self" reference) but that didn't work.

class HeikinAshi(CustomFactor):  
    window_length=10  
    ha_candles = {}

    def compute(self, today, assets, out, prices_open, prices_high, prices_low, prices_close):  
        print(ha_candles)  

I also tried to pass the "context" variable into my compute method so I can reference the previously calculated candles that way but that is also not allowed.

Thanks
Kurt

2 responses

One cannot store data between iterations of a pipeline. The reason is pipelines are run asynchronously to the algo. Also be VERY careful with storing any price or volume data (or factors which use these). The stored values will not be adjusted for splits or dividends.

Don't worry about efficiency. Pipelines are designed to optimize the data fetches and calculations. The best way is, as was alluded to, use a custom factor with a window_length=10. This is necessary so prices will be re-adjusted each day. The prices passed to the compute function are all adjusted as of the current simulation day and can therefore be compared properly.

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.

Thank you for the clarification and insight! I have my CustomFactor working now, using the method you suggested. It re-calculates the most recent X Heikin Ashi candles each day.