Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to get benchmark and algorithm output in sync after warmup period?

Hi,
I have sma of 200. So after 200 days algorithm starts trading. However, the benchmark is already started. How to sync those after warmup so that results are comparable?
J.

9 responses

Hello J.,

If you run your algo on minute bars and use history, you can compute the 200-day SMA at the start of the algo, rather than accumulating the data (since history provides the trailing data at the start). Your backtest will run more slowly (even if you limit trading to once per day), since there is overhead in calling handle_data every minute. However, by default, the benchmark and your algo will be in synch.

Grant

Dear Grant,
I scanned through the doc. but how is the initialization data aware? I use fetcher in init and data is not defined yet.
J.

I'm not sure about fetcher, since I haven't used it yet. However, history will load the trailing data window at the backtest start. If you are trying to do computations on data within initialize, maybe this is problem? Perhaps you could strip out your confidential details, and post the algorithm? --Grant

No confidential details in a community please :-). https://www.quantopian.com/posts/trading-on-the-rsi-of-the-vix-and-spy

You can use Fetcher's universe_func to create a custom universe in your algorithm. If you then call history(), it will return trailing data prior to your backtest start date so your algorithm and benchmark will be in sync from the start.

Take a look here at the custom universe documentation and an example from the forum.

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. I was trying to use the post_func of fetcher, but no success.

fetch_csv(url, symbol='VIX', date_column='Date', pre_func=preview, post_func=warmup)  

Most likely the data format is wrong...but no debugging options to find out easily what is wrong. I will go your way.
def warmup(df): hist = _history(df) log.info(' %s ' % df.head()) log.info(' %s ' % hist.head()) return df

I do not get the universe_func to run. I only have one SID in it and I have the idea that the List Comprehensions expects more than one.. I tried next code with history indead very slow. Therefore I tried to limited it to be used only once. However, my fetched sid named 'VIX' is not present for some reason..I get error on vix = price_history['VIX']

    hist = _history(data)  
    if hist is None:  
        # Return if there is not enough data yet  
        # Return  
        # try to warm-up based on passed data (otherwise algo will not start before 200 days...)  
        close_history = history(bar_count=200, frequency='1d', field='close_price')  
        price_history = history(bar_count=200, frequency='1d', field='price')  
        vix = price_history['VIX']  
        prices = price_history[spy]  
    else:  
        vix = hist['close_price']['VIX'].values  
        prices = hist['price'][spy].values  

VIX is not an equity, right? Per the help page, only equities are available.

Regarding running on minute bars, if you can restrict trading to once per day (e.g. a specific time of day), you might be able to speed up execution a bit. However, keep in mind that the net overhead will be ~390 times greater regardless so backtests will seem relatively sluggish compared to running on daily bars.

--Grant

Grant is right that we don't have VIX data, so you can't query for its price history. Instead, you could use VIX prices as a trading signal.

@J
There could be a couple things at play here that are throwing the error. Could you share you share your algorithm with me? If you press the 'Collaborate' button in the IDE I'll get invited to see your code and help debug. My email is: [email protected]

Thanks!