Back to Community
Backtest data ill-adjusted, is this a bug?

Running the attached backtest spanning over the 2014-06-09 AAPL split yields an odd result, the log shows:

2014-06-06  PRINT :  
2014-06-04 00:00:00+00:00   644.82  
2014-06-05 00:00:00+00:00   647.35  
Freq: C  
2014-06-09  PRINT :  
2014-06-05 00:00:00+00:00    92.48  
2014-06-06 00:00:00+00:00    92.23  
Freq: C  
2014-06-10  PRINT :  
2014-06-06 00:00:00+00:00   645.57  
2014-06-09 00:00:00+00:00    93.70  
Freq: C  
2014-06-11  PRINT :  
2014-06-09 00:00:00+00:00    93.70  
2014-06-10 00:00:00+00:00    94.25  

where on June 6th the split has yet to happen, on the 9th the split has happened and the previous data is adjusted backward, on the 10th only one of the values is back adjusted. This looks wrong to me.

I went through this Karen's post and in turn this other one, from what I understood the prices evaluated on day 10th for day 6th and 9th should both be adjusted since current day is past the split. Nonetheless the 9th is in the queried day so that should pull in back-adjusting for the whole Series.

Notice that I'm accessing history through the data object not via pipeline and I'm fetching it in before_trading_start so the last item of history belongs to the previous trading day.

Is this a bug? If not what's happening with those two prices printed on 2014-06-10?

Clone Algorithm
1
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
def initialize(context):
    schedule_function(my_handle,
                      date_rules.every_day())     
    context.sec = sid(24)

def before_trading_start(context, data):
    context.history = data.history(context.sec, 'close', 2, '1d')

def my_handle(context,data):
    print(":\n%s" % (context.history.to_string(float_format=lambda x: "%6.2f" % float(x))))
There was a runtime error.
3 responses

I compared the results of history accessed via data object, the source is in the attached backtest.

The two columns should display the same value but there's definitely something odd going on both on the split day and on the following one.
See how the log on 2014-06-11 is correctly adjusted instead, even if it goes as back as 2014-06-05:

2014-06-05  PRINT :  
                           before day  during day  
2014-05-30 00:00:00+00:00      633.00      633.00  
2014-06-02 00:00:00+00:00      628.50      628.50  
2014-06-03 00:00:00+00:00      637.54      637.54  
2014-06-04 00:00:00+00:00      644.82      644.82  
2014-06-05 00:00:00+00:00         nan      646.26  
.
2014-06-06  PRINT :  
                           before day  during day  
2014-06-02 00:00:00+00:00      628.50      628.50  
2014-06-03 00:00:00+00:00      637.54      637.54  
2014-06-04 00:00:00+00:00      644.82      644.82  
2014-06-05 00:00:00+00:00      647.35      647.35  
2014-06-06 00:00:00+00:00         nan      649.39  
.
2014-06-09  PRINT :  
                           before day  during day  
2014-06-03 00:00:00+00:00       91.08      637.54  
2014-06-04 00:00:00+00:00       92.12      644.82  
2014-06-05 00:00:00+00:00       92.48      647.35  
2014-06-06 00:00:00+00:00       92.23      645.57  
2014-06-09 00:00:00+00:00         nan       92.72  
.
2014-06-10  PRINT :  
                           before day  during day  
2014-06-04 00:00:00+00:00      644.82       92.12  
2014-06-05 00:00:00+00:00      647.35       92.48  
2014-06-06 00:00:00+00:00      645.57       92.23  
2014-06-09 00:00:00+00:00       93.70       93.70  
2014-06-10 00:00:00+00:00         nan       94.50  
.
2014-06-11  PRINT :  
                           before day  during day  
2014-06-05 00:00:00+00:00       92.48       92.48  
2014-06-06 00:00:00+00:00       92.23       92.23  
2014-06-09 00:00:00+00:00       93.70       93.70  
2014-06-10 00:00:00+00:00       94.25       94.25  
2014-06-11 00:00:00+00:00         nan       93.97  
.
End of logs.  
Clone Algorithm
1
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
from pandas import DataFrame

_length = 4

def initialize(context):
    schedule_function(an_handle, date_rules.every_day())     
    context.sec = sid(24)

def before_trading_start(context, data):
    context.prices = data.history(context.sec, 'close', _length, '1d')

def an_handle(context,data):
    prices = data.history(context.sec, 'close', _length+1, '1d')
    
    df = DataFrame({'before day': context.prices, 'during day': prices},
                  columns=('before day', 'during day'))

    print(":\n%s\n." % df.to_string(float_format=lambda x: "%6.2f" % float(x)))
There was a runtime error.

Hi Andrea,

Thanks for reporting this, it is definitely a bug. Our engineers are looking into it.

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.

Are you filing an issue on github for this?

I didn't as I'm not using zipline locally, just the web platform, and I wasn't sure the github repo acts as tracker for the platform as well., but if one gets filed please post its number here, I'm interested in following it.