Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Resample of monthly data

Hi There,

I am trying to resample the monthly data of the stock SPY by using this code, however the resample is not working properly, when comparing with other sources like yahoo. What I'm doing wrong?

Thanks

    schedule_function(monthly_data,  
                      date_rules.month_end(days_offset=0),  
                      time_rules.market_close())

def monthly_data(context, data):  
    daily_ohlc = data.history(symbol('SPY'), ['open', 'high', 'low', 'close'], 90,'1d')  
    monthly_ohlc = daily_ohlc.resample('1M').agg({'open': 'first',  
                                 'high': 'max',  
                                 'low': 'min',  
                                 'close': 'last'})  

Results
high close open low
2016-11-30 00:00:00+00:00 220.520 219.039 211.682 207.159
2016-12-31 00:00:00+00:00 227.002 223.550 219.437 217.866
2017-01-31 00:00:00+00:00 229.710 227.430 225.040 223.884

Yahoo

Date Open High Low Close Adj Close* Volume
Feb 01, 2017 227.53 237.31 226.82 236.47 236.47 1,365,136,600
Jan 01, 2017 225.04 229.71 223.88 227.53 227.53 1,481,734,100
Dec 01, 2016 220.73 228.34 219.15 223.53 223.53 1,821,910,200

2 responses

I think you're getting the data that you are looking for, but I think what you're running into is confusion about how prices are adjusted for dividends and splits.

When you run your code and print monthly_ohlc, you're getting the adjusted prices, as of the day of simulation. So, on the simulated date of 2/28, you're seeing the as-adjusted close for 1/31 and 12/31. That is close to the adjusted close prices you're seeing for those months in Yahoo.

It might be easier for you to look a the unadjusted prices at the end of each month and compare those. Though the months with the dividend in the middle of the month are going to have adjusted opening prices, so even that is going to be annoying to unwrap.

The prices are often different by a few cents because of the difference in how Quantopian and Yahoo relate prices. Yahoo is using the close as quoted by the exchange, which includes out-of-hours trades and things like that. Quantopian is looking at the actual reported trades on all exchanges, not just the official listed exchange.

(As a side note - it's always easier if you attach a backtest rather than a code snippet. The backtest includes all the code, and the meta-information like when the start and end date are).

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 Dan,

How can you retrieve the unadjusted prices in Quantopian?

Regards,
John