Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Delay between price by code vs. backtest graph

Why does the value of

data[stock].price  

for a specific day always appear in the backtest graph as the value or the previous day?

For example, the code
print 'price = ' + str(data[stock].price)

prints
2011-03-03 PRINT price = 51.367

but on the graph 51.37 is the price on 2011-03-02.

Also, when I get history by days, what is the difference between price and close_price?

1 response

Hi Lee,

data[stock].price will always return the last known price, which is the price of the last bar. In daily mode backtesting, this is yesterday's close price. In minute mode backtesting, this is the price 1 minute ago.

When you're backtesting (or live trading) it's not possible to know the price exactly right now. It's constantly changing! So we offer the closest thing available, which is the most recently seen price.

In history, there is a slight difference between price and close_price. The field close_price will return the raw prices. If a stock didn't trade in a specific minute (or day) it will have a NaN for the value. The field 'price' will return the raw prices and will forward-fill the last known value if a data point is missing.

For example, let's say stock ABC has pricing that looks like this:

9:31AM: 100  
9:32AM: 99.6  
9:33AM:  
9:34AM:  
9:35AM: 99  

Then, `history(5, '1m', 'close_price') will return that identical data:

9:31AM: 100  
9:32AM: 99.6  
9:33AM: NaN  
9:34AM: NaN  
9:35AM: 99  

Whereas `history(5, '1m', 'price') will forward-fill the value from 9:32AM:

9:31AM: 100  
9:32AM: 99.6  
9:33AM: 99.6  
9:34AM: 99.6  
9:35AM: 99  

Cheers,
Alisa

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.