Back to Community
Current price with history. Quantopian Bug?

I think I might have found a bug in Quantopian's history function. If the problem is me misunderstanding something then please forgive me.

From the help docs on history:

data.history(context.assets, "price", 1, "1d") returns the current price.
data.history(context.assets, "price", 1, "1m") returns the current price.

And so you would expect these two calls to return the same value. But they don't always do so. Quite often there is a very small difference, which is fine. Sometimes the difference is quite large. The attached backtest logs the differences so you can see them.

So what is happening? Is it something I don't understand? Is this a 'feature'?

Please explain the current behavior as I have an algo that is performing very well, but I need to understand this in order to understand precisely why it is performing so well in some configurations. It is almost too good to be true, so watch out for bugs that allow data snooping with the history function related to this behavior.

Clone Algorithm
5
Loading...
Backtest from to with initial capital
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
# Backtest ID: 5a43236de58171417a6f9011
There was a runtime error.
11 responses

Just when '1d' bar_count is set to 1 and data.current(s, 'close') is nan. If this never surfaced before it would be because people are using bar_counts higher than 1 with data.history() or simply data.current(). The tiny differences that were being seen are a Python thing with floats, ignoring those.

This filters for differences over 1.00.
The c_price header for example means current price or data.current(s, 'price')

In the [second one] difference of 10.68 for example, it would be interesting to know where the 492.82 value comes from.
Edit: The answer is right there and I missed it before, at market open it is tomorrow's forward-filled closing price. Sweet.

1969-12-31 16:00 initialize:20 INFO Diff   '1d'     '1m'   c_close  c_price    Symbol  
2017-11-28 06:31 trade:34 INFO     3.27   156.63   153.36      nan   153.36      URI  
2017-11-28 06:31 trade:34 INFO    10.68   492.82   482.14      nan   482.14      BLK  
2017-11-28 06:31 trade:34 INFO     1.06    28.03    26.97      nan    26.97      DKS  
2017-11-29 06:31 trade:34 INFO     2.65   158.74   156.09      nan   156.09      SPG  
2017-11-29 06:31 trade:34 INFO     8.00   500.82   492.82      nan   492.82      BLK  
2017-11-29 06:31 trade:34 INFO     3.87   119.06   115.19      nan   115.19      UPS  
2017-11-30 06:31 trade:34 INFO     4.49   221.28   216.79      nan   216.79      GWW  
Clone Algorithm
6
Loading...
Backtest from to with initial capital
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
# Backtest ID: 5a4359eaef5c9f400053ba4d
There was a runtime error.

It looks like the values in the 1d(was day) column are pretty near to the close at the end of the day. If this is the case then it should be noted that the history function should not have access to these figures in the morning or during the day, that clearly leads to misleading results.

[edited Day column above to '1d' for clarity]

Try a breakpoint on line 41 (in my version of the code) and then these examples and others.

data.history(s, 'price', 1, '1d')
data.history(s, 'price', 2, '1d')
data.history(s, 'price', 10, '1m').values

I believe you have found a bug. Thank you for bringing it to our attention! We really appreciate the report.

I've made an initial assessment of the issue and handed it to an engineer for follow up. My initial assessment: This bug appears to be happening when the 1d history call is only for one bar, and the opening minute(s) of the day have zero volume. What should be happening is that the previous close price should be forward-filled. For the 1m history call, the close price is being correctly forward filled. For the 1d history call, it's using the daily close for the current day - a look-ahead bias problem, for sure.

As a workaround for now, I suggest that you pull more than one day of history at a time, like prc_day = data.history(s, 'price', 2, '1d'). When you pull that extra day the close price is correctly forward-filled from yesterday.

It's also worth noting that using history is a somewhat unusual way to get the current price. data.current() is going to be more common than data.history

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.

I was playing around with an algo that used the 1d history feature and stumbled across this while playing with the settings. So even though data.current would typically be used people might use this feature when the code is already laid out. I did a great job of optimizing my algo to use this, thought I was a real genius, lol.

The fix for the bug is written, but not yet released. You can track the progress in the Zipline pull request. Once it gets merged into master it will probably take a few days for testing and such before the fix makes it to production.

The fix for this problem has shipped and is available.

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.

Thanks. Current, excerpts with modified code for anyone who wishes to verify the fix or work with it.
I'm surprised to see nan under '1d' with ffilled 'price'. Any other differences are beyond my pay grade.

1969-12-31 16:00 initialize:33 INFO Diff    '1d'     '1m'   c_close  c_price    Symbol  
2017-08-07 06:31 trade:47 INFO      nan      nan   536.53      nan   536.53      AZO  
2017-08-07 06:31 trade:47 INFO      nan      nan    54.00      nan    54.00      BK  
2017-08-07 06:31 trade:47 INFO      nan      nan   112.02      nan   112.02      UPS  
2017-08-07 06:31 trade:47 INFO      nan      nan    18.39      nan    18.39      UAA  
2017-08-07 06:31 trade:47 INFO      nan      nan    19.71      nan    19.71      WU  
2017-08-07 06:31 trade:47 INFO      nan      nan    62.12      nan    62.12      ZTS  
2017-08-11 06:31 trade:47 INFO    -1.26   162.28   163.54      nan   163.54      GWW  
2017-08-11 06:31 trade:47 INFO    -0.50   221.64   222.14      nan   222.14      ADS  
2017-08-14 06:31 trade:47 INFO      nan      nan   144.60      nan   144.60      APD  
2017-08-14 06:31 trade:47 INFO      nan      nan    37.80      nan    37.80      YUMC  
2017-08-17 06:31 trade:47 INFO    -0.67   137.44   138.11      nan   138.11      HON  
2017-08-21 06:31 trade:47 INFO      nan      nan    85.38      nan    85.38      AXP  
2017-08-21 06:31 trade:47 INFO      nan      nan    37.04      nan    37.04      YUMC  
2017-08-22 06:31 trade:47 INFO    -0.00   517.56   517.56      nan   517.56      AZO  
2017-08-25 06:31 trade:47 INFO    -0.41   151.05   151.46      nan   151.46      SPGI  
2017-08-28 06:31 trade:47 INFO      nan      nan    52.65      nan    52.65      BK  
2017-08-28 06:31 trade:47 INFO      nan      nan   308.70      nan   308.70      CMG  
2017-09-01 06:31 trade:47 INFO    -2.52   416.44   418.96      nan   418.96      BLK  
2017-09-05 06:31 trade:47 INFO      nan      nan   158.20      nan   158.20      AET  
2017-09-05 06:31 trade:47 INFO      nan      nan    64.36      nan    64.36      A  
2017-09-08 06:31 trade:47 INFO    -0.33    40.31    40.64      nan    40.64      TPR  
2017-09-11 06:31 trade:47 INFO      nan      nan    87.47      nan    87.47      IR  
2017-09-11 06:31 trade:47 INFO      nan      nan    49.24      nan    49.24      EVHC  
2017-09-15 06:31 trade:47 INFO    -0.09    65.17    65.26      nan    65.26      AME  
2017-09-15 06:31 trade:47 INFO    -0.15   191.63   191.78      nan   191.78      TMO  
2017-09-15 06:31 trade:47 INFO    -0.51    84.55    85.06      nan    85.06      EMN  
2017-09-15 06:31 trade:47 INFO    -0.02    18.76    18.78      nan    18.78      RRC  
2017-09-15 06:31 trade:47 INFO    -0.29    91.84    92.13      nan    92.13      FIS  
2017-09-15 06:31 trade:47 INFO    -0.92   120.00   120.92      nan   120.92      DLR  
2017-09-18 06:31 trade:47 INFO      nan      nan    98.55      nan    98.55      DIS  
2017-10-02 06:31 trade:47 INFO      nan      nan    63.75      nan    63.75      ZTS  
2017-10-04 06:31 trade:47 INFO    -0.23    28.73    28.96      nan    28.96      GPS  
2017-10-09 06:31 trade:47 INFO      nan      nan    83.20      nan    83.20      TAP  
2017-10-30 06:31 trade:47 INFO      nan      nan    91.95      nan    91.95      ABBV  
2017-10-31 06:31 trade:47 INFO    -0.24    51.39    51.63      nan    51.63      BK  
2017-11-06 06:31 trade:47 INFO      nan      nan    28.23      nan    28.23      COG  
2017-11-16 06:31 trade:47 INFO    -0.39    61.12    61.51      nan    61.51      MPC  
2017-11-20 06:31 trade:47 INFO      nan      nan   103.44      nan   103.44      DIS  
2017-11-20 06:31 trade:47 INFO      nan      nan    20.29      nan    20.29      SM  
2017-11-20 06:31 trade:47 INFO      nan      nan   229.36      nan   229.36      MMM  
2017-11-20 06:31 trade:47 INFO      nan      nan    28.56      nan    28.56      NWL  
2017-11-20 06:31 trade:47 INFO      nan      nan   105.35      nan   105.35      ANDV  
2017-11-20 06:31 trade:47 INFO      nan      nan   474.00      nan   474.00      BLK  
2017-11-20 06:31 trade:47 INFO      nan      nan   146.48      nan   146.48      HON  
2017-11-20 06:31 trade:47 INFO      nan      nan    98.71      nan    98.71      APTV  
2017-11-24 06:31 trade:47 INFO      nan      nan   217.85      nan   217.85      FDX  
2017-11-24 06:31 trade:47 INFO      nan      nan    23.35      nan    23.35      GGP  
2017-11-24 06:31 trade:47 INFO      nan      nan   478.94      nan   478.94      BLK  
2017-11-24 06:31 trade:47 INFO      nan      nan   113.75      nan   113.75      UPS  
2017-11-24 06:31 trade:47 INFO      nan      nan    87.20      nan    87.20      DG  
2017-11-27 06:31 trade:47 INFO      nan      nan    29.26      nan    29.26      COG  
2017-11-27 06:31 trade:47 INFO      nan      nan    57.21      nan    57.21      STI  
2017-11-27 06:31 trade:47 INFO      nan      nan   206.00      nan   206.00      MLM  
2017-11-27 06:31 trade:47 INFO      nan      nan    29.04      nan    29.04      NRG  
Clone Algorithm
4
Loading...
Backtest from to with initial capital
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
# Backtest ID: 5a6248cd3633ad442deb5bd6
There was a runtime error.

Thanks for the verification! Indeed there was a bug with the original fix that we didn't catch. The additional fix is in progress and should appear in production soon as well.

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.

That additional fix for the NaNs was shipped.

@Blue I'm glad you pointed out those other differences - the fix for that should be shipped soon as well.

Clean now using the same backtest above. That was sort of deja-vu from some of my testing fun during Microsoft days, happy to help.

1969-12-31 16:00 initialize:33 INFO Diff    '1d'     '1m'   c_close  c_price    Symbol  
2017-08-11 06:31 trade:47 INFO    -1.26   162.28   163.54      nan   163.54      GWW  
2017-08-11 06:31 trade:47 INFO    -0.50   221.64   222.14      nan   222.14      ADS  
2017-08-17 06:31 trade:47 INFO    -0.67   137.44   138.11      nan   138.11      HON  
2017-08-25 06:31 trade:47 INFO    -0.41   151.05   151.46      nan   151.46      SPGI  
2017-09-01 06:31 trade:47 INFO    -2.52   416.44   418.96      nan   418.96      BLK  
2017-09-08 06:31 trade:47 INFO    -0.33    40.31    40.64      nan    40.64      TPR  
2017-09-15 06:31 trade:47 INFO    -0.09    65.17    65.26      nan    65.26      AME  
2017-09-15 06:31 trade:47 INFO    -0.15   191.63   191.78      nan   191.78      TMO  
2017-09-15 06:31 trade:47 INFO    -0.51    84.55    85.06      nan    85.06      EMN  
2017-09-15 06:31 trade:47 INFO    -0.02    18.76    18.78      nan    18.78      RRC  
2017-09-15 06:31 trade:47 INFO    -0.29    91.84    92.13      nan    92.13      FIS  
2017-09-15 06:31 trade:47 INFO    -0.92   120.00   120.92      nan   120.92      DLR  
2017-10-04 06:31 trade:47 INFO    -0.23    28.73    28.96      nan    28.96      GPS  
2017-10-31 06:31 trade:47 INFO    -0.24    51.39    51.63      nan    51.63      BK  
2017-11-16 06:31 trade:47 INFO    -0.39    61.12    61.51      nan    61.51      MPC