Back to Community
Wrong Stock Price??

Hey,

I was backtesting an algo that I built. I was looking at the prices of some of the positions that it took. I noticed that some of the prices reported were horrendously off. AAPL on 2011-01-11 was $341.6. How could that happen?

4 responses

Why do you think the prices are 'horrendously' off (though actually I have an idea)? Yes indeed, one would have had to cough up $341.60 to buy a single share of AAPL on 2011-01-11. That was the actual close price on that date. That is the 'un-adjusted' price (or maybe more correctly the 'adjusted as of 2011-01-11' price).

Most online sources, such as Yahoo, only display prices adjusted 'as of' the current day. Adjusted prices reflect stock splits and dividends and aren't necessarily the actual trading price on a particular day. Quantopian is much more flexible and gives users the ability to display prices adjusted 'as of ' a user defined day.

So, how to set the 'as of' date which Quantopian uses to adjust prices? Inside a backtest prices and volumes are adjusted as of the simulation date. These would have been the data actually traded on that date. To avoid lookahead bias this cannot be changed.

Within the research environment there is more flexibility. The get_pricing method uses the 'end_date' parameter as the 'as of' date. Specify an 'end-date' and the resulting price and volume data will take into account all stock splits and dividends up though that date. As an example, the following would fetch the close prices for AAPL and adjust them for all splits and dividends through 2011-01-01.

get_pricing_2011_01_01 = get_pricing('AAPL',  
                                 start_date='2011-01-01',  
                                 end_date='2011-01-11',  
                                 frequency='daily',  
                                 fields='close_price')

One can choose any end_date as the 'as_of' date to adjust prices. To fetch data as one would have actually traded on a given day, set the end_date to that day. To fetch data adjusted to match most online data, set the end_date to the current day. So, to replicate what one may see on a site like Yahoo do something like this

import pandas as pd  
current_date = pd.to_datetime('today')  
get_pricing_current = get_pricing('AAPL',  
                                 start_date='2011-01-01',  
                                 end_date=current_date,  
                                 frequency='daily',  
                                 fields='close_price')

The Quantopian prices may be a bit off from other online sources but they are still correct and will be adjusted 'as of' the current day. See the post "Why is your close price different from other data sources?" in the FAQs

Hope that helps.

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.

Hi Dan,

Thanks for your response. I hear what you’re saying. Although, how do you fix that? My issue isn’t in the research notebook. It’s in the IDE. When I run my backtests, it’s acting as if the price is $341. When it sells the position, does it take adjustments into effect to fix that or does the issue persist?

There is no issue. The backtest acts as if the AAPL price was $341.60 on 2011-01-11 because it was $341.60. If one wanted to buy a share of AAPL on that date they would have had to pay $341.60. If later one were to sell the stock it would sell at that dates current price. The backtest faithfully represents what the prices were on each day.

The Quantopian backtest engine automagically takes care of dividends and stock splits. That may be the 'missing link'? Say one were to buy 1 share of company XYZ at $100 per share. If XYZ declared a 2:1 stock split the following day, one would now see 2 shares in their account. The price would typically be proportionately lower at $50. This is exactly what one would see in a real broker account. On the day of a stock spit one would see the quantity of shares in their account adjusted.

Similar adjustments are made for dividends. When a company pays a dividend, the backtest engine adds that amount as cash into ones portfolio. This again is exactly what one would see in a real broker account. The Quantopian backtester attempts to faithfully re-create corporate events and trades as they actually would have happened.

As a side note, there are backtest frameworks which don't use 'actual' prices but instead use 'adjusted' prices and volumes. This avoids the complexity of handling stock splits and dividends. However, this approach won't handle commissions correctly (eg if commissions are based upon shares the quantity of shares purchased would be the adjusted amount and not the actual amount). This approach also typically won't get the shares ordered correct either (because of rounding since orders can only be made in whole shares). The Quantopian backtest engine replicates the actual trade process and faithfully calculates share quantity, commissions, and dividends.

So, in a way, yes the effect gets 'fixed'

I've found some bad prices myself.

import matplotlib.pyplot as plt

BAD_SIDS = [  
    10583,  
    16934,  
    26560,  
]

for sid in BAD_SIDS:  
    fig, ax = plt.subplots(nrows=1, ncols=1)  
    symbol = symbols(sid)  
    quantopian.research.prices(  
        assets      = symbol,  
        start       = symbol.security_start_date,  
        end         = symbol.security_end_date,  
        frequency   = 'daily',  
        price_field = 'price').plot()