Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Dictionary problem - what am I doing wrong?

Hi,

I'm doing something stupid, obviously, but can't see it so appreciate a little help.

I'm trying to add a dictionary item to the variable that stores the history output using the following code:

        y = get_datetime().year  
        m = get_datetime().month  
        d = get_datetime().day  
        h = get_datetime().hour  
        mi = get_datetime().minute  
        s = get_datetime().second  
        ms = get_datetime().microsecond  
        date = datetime.datetime(y,m,d, h, mi, s, ms)  
        daily_prices = history(bar_count=252, frequency='1d', field='price')

        if ( isTodayMonday ): # Just a logical check which is working  
            dct = { str(date) : currentClose }  
            daily_prices.update(dct)  

but I'm getting this runtime error:

KeyError: u'no item named 2014-01-03 00:00:00'
There was a runtime error on line 138.

Where line 138 is daily_prices.update(dct)

What am I doing wrong? How do I add an element to the daily_prices variable?

Thanks in advance for your help.
Andrew

4 responses

Hello Andrew,

Why do you want to add an element to daily_prices? If I read your code correctly, you are trying to insert the current closing price, but it should already be there, per the help page. Have a look, but you should be getting 251 daily closing prices, plus the closing price for the current minute (assuming you are running on minute bars).

Or are you trying to do something else?

Grant

If you could strip out any code that you don't want to share post the portion of your algo that's throwing an error it would be very helpful. I'm not sure where you are going wrong, but as Grant stated, the history function should return the data that you are looking to store. If you prefer to work with dictionaries, dataframe.to_dict() should work for you.

Thanks Grant and David.

Grant you were right about what I'm trying to do and hadn't realised the subtles of the history command which meant that my algo had a fundermental flaw so thanks for pointing it out.

Andrew

just a note about the get_datetime function: get_datetime returns a copy of the algorithm's date, you don't need to construct a new datetime based on the values inside of it. You can simplify your example to:

date = get_datetime()  
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.