Back to Community
Different data from Notebook vs Algorithm?

Hi all,

I was trying to implement my Notebook strategy into the Algorithm.
However, I found that the results varies a lot.
I found out that the prices I get from Notebook and Algorithm differs.
Is there a reason why?

I had a look at
https://www.quantopian.com/posts/help-notebook-vs-algorithm
The better answer I found

This means that the 'current' price of an equity on any given day in the backtester will be the price that it traded that day. However, if you ask for a historical window on day N in the backtester, the returned price series will be adjusted for all the dividends and splits that occurred prior to day N.

However, I can't see how historical can give me price for a time when algorithm can't.

Check out the two simple code below.

When I start from 2017-01-01, I see that
- Notebook's history starts at 2017-01-03 @ 23:01
- Algorithm's tick starts at 2017-01-04 @ 11:31

I'm sure it's not an issue with timezone as the Algorithm's tick starts at 30 minutes after the hour.
Please help. Thanks.

Notebook - Code

from quantopian.research.experimental import history, continuous_future

prd_1 = continuous_future('EC')

Data = history(  
  prd_1,  
  fields=['open_price', 'high', 'low', 'close_price'],  
  frequency='minute',  
  start='2016-01-01',  
  end='2016-02-28'  
)

print(str(Data[0:100]))  

Notebook - Results

                           open_price    high     low  close_price  
2016-01-03 23:01:00+00:00         NaN     NaN     NaN          NaN  
2016-01-03 23:02:00+00:00      1.0884  1.0884  1.0881       1.0882  
2016-01-03 23:03:00+00:00      1.0883  1.0884  1.0882       1.0883  
2016-01-03 23:04:00+00:00      1.0884  1.0884  1.0879       1.0879  
2016-01-03 23:05:00+00:00      1.0878  1.0878  1.0873       1.0874  
2016-01-03 23:06:00+00:00      1.0874  1.0875  1.0874       1.0875  
2016-01-03 23:07:00+00:00      1.0875  1.0876  1.0874       1.0875  
2016-01-03 23:08:00+00:00      1.0875  1.0875  1.0875       1.0875  

Algorithm Code

def initialize(context):  
    print("Started");

def handle_data(context,data):  
    product = continuous_future('EC', offset=0, roll='volume', adjustment='mul');  
    openn = data.current(product,'open');  
    high = data.current(product,'high');  
    low = data.current(product,'low');  
    close = data.current(product, 'price');  
    now = data.current_dt;  
    print(str(now) + ", " + str(openn) + ", " + str(high)  
          + ", " + str(low) + ", " + str(close));  

Algorithm Results

1970-01-01 00:00 PRINT Started  
2016-01-04 11:31 PRINT 2016-01-04 11:31:00+00:00, 1.0929, 1.0931, 1.0929, 1.0931  
2016-01-04 11:32 PRINT 2016-01-04 11:32:00+00:00, 1.093, 1.093, 1.0925, 1.0925  
2016-01-04 11:33 PRINT 2016-01-04 11:33:00+00:00, 1.0925, 1.0926, 1.0924, 1.0924  
2016-01-04 11:34 PRINT 2016-01-04 11:34:00+00:00, 1.0925, 1.0928, 1.0924, 1.0927  
2016-01-04 11:35 PRINT 2016-01-04 11:35:00+00:00, 1.0926, 1.0927, 1.0926, 1.0927  
2016-01-04 11:36 PRINT 2016-01-04 11:36:00+00:00, 1.0927, 1.0927, 1.0926, 1.0927  
2016-01-04 11:37 PRINT 2016-01-04 11:37:00+00:00, 1.0926, 1.0928, 1.0926, 1.0928  
2016-01-04 11:38 PRINT 2016-01-04 11:38:00+00:00, 1.0927, 1.093, 1.0927, 1.093  
2016-01-04 11:39 PRINT 2016-01-04 11:39:00+00:00, 1.093, 1.0934, 1.093, 1.0933  
2016-01-04 11:40 PRINT 2016-01-04 11:40:00+00:00, 1.0932, 1.0933, 1.0931, 1.0932  
1 response

Hey Han,

My guess: the notebook shows you all data that quantopian has access to (which might include pre-market trading), whereas the algorithm shows you results for times that you could be running your backtest on (that is, market times).

This guess is consistent with the fact that if you ask the notebook for prices on trading times, those prices agree with the prices from the algorithm. You can see this by running on the notebook:

print(str(Data.loc["2016-01-04 11:30":].head()))

                           open_price    high     low  close_price  
2016-01-04 11:30:00+00:00      1.0928  1.0930  1.0928       1.0929  
2016-01-04 11:31:00+00:00      1.0929  1.0931  1.0929       1.0931  
2016-01-04 11:32:00+00:00      1.0930  1.0930  1.0925       1.0925  
2016-01-04 11:33:00+00:00      1.0925  1.0926  1.0924       1.0924  
2016-01-04 11:34:00+00:00      1.0925  1.0928  1.0924       1.0927