Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
shillers PE - need access to CPI information - Any suggestions

I am looking to calculate the shiller's PE for a group of stocks.

Does anyone know an easy to use source to integrate CPI(Consumer Price Index) data into an algorithm

Thanks,
Sarvi

3 responses

Quandl has CPI - https://www.quandl.com/FRED/CPIAUCSL-Consumer-Price-Index-for-All-Urban-Consumers-All-Items-USA-Inflation
You can bring this into your algorithm using fetcher.

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 John,
Thanks for the pointer.
I have a toy example where I am puling the data.
But am seeing a Runtime Error
"Runtime exception: KeyError: 'Value'"

I have read the documentation and some of the examples in the mailing list, and looks like the code is right.
But can't seem to figure out what i might be doing wrong.

Any pointers on what I am doing wrong?

def rename_col(df):  
    df = df.rename(columns={'Value': 'value'})  
    return df  

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    fetch_csv('https://www.quandl.com/api/v1/datasets/FRED/CPIAUCSL.csv',  
         symbol='USCPI',  
#        post_func=rename_col,  
#        date_format='%d-%m-%Y')  
         date_column='Date',  
         date_format='%Y-%m-%d')  
#    )  
    pass

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    # Implement your algorithm logic here.

    # data[sid(X)] holds the trade event data for that security.  
    # context.portfolio holds the current portfolio state.

    # Place orders with the order(SID, amount) method.

    # TODO: implement your own logic here.  
    # order(sid(24), 50)  
    record(USCPI=data['USCPI']['Value'])

Hi Saravanan,

So there were a few things that you needed to do in order to get this working:

1) The 'date_format' parameter needs to match up with what you have in your CSV exactly, so in this case the correct format would be '%m/%d/%y'
2) Because you're backtest might not line up exactly with the dates in your CSV, you need a 'if 'value' in data['uscpi']' check before trying to access your data

We cover Fetcher extensively in this tutorial so I would highly recommend checking that out if you'd like to learn more.

The attached backtest will solve your errors

Thanks!

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.