Back to Community
Unable to acess the dictionary with time key values.

Hello,
I am newbie to Quantopian. I am just writting a very simple code to try with. The code looks like below.

def initialize(context) :  
    context.dat = { }  
def handle_data(context,data) :  
    full = data[symbol('SPY')]  
    date = str(full.datetime)[:10]  
    context.dat[date] = [full.open_price,full.high,full.low,full.close_price]  
    log.info(context.dat)  
    #log.info(context.dat['2015-06-04'])  

I am just trying to get the data every minute and append it to a dictionary which is indexed by time so that I can use the data to compute my indicators. I ran it for 3 days with daily data settings. End of the backtest the dictionary looks like this

{'2015-06-08': [209.64, 209.83, 208.39, 208.47], '2015-06-04': [211.07, 211.78, 209.75, 210.22], '2015-06-05': [209.95, 210.58, 208.98, 209.79]}

The problem now is that I am not able to access the dictionaries. If i say context.dat['2015-06-04'] It's giving me key error 2015-06-04 why is it giving the key error even if there is a key? I just created same kind of dictionary in my own computer's python command prompt and I am able to access the dictionary using keys But its not woking as expected in quantopian. I am unable to figure out the problem here. Please assist me to solve this. Thanks for any help.

6 responses

Hi,

We run a set of unit tests on each algorithm before running the simulation. The key error is happening in one of those tests, which is using mock data to exercise handle data. As a result, the date key really is not in your dictionary in the test, and hence the exception. The telltale sign is if setting a breakpoint before the exception does not trigger the debugger (i.e. breakpoint appears to be ignored).

Attached is a backtest that just logs context.dat[date] instead.

There is also the history() function, which provides just this kind of lookback data. It will run faster and handle more corner cases than writing new code to store up events. https://www.quantopian.com/help#api-history

happy coding,
fawce

Clone Algorithm
2
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 557967cee9939a10bf5ec998
There was a runtime error.
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.

John Fawcett, Thanks for the reply.
I am just planning to code one of my stratagies which in live mode collects data from beginning of the day and computes indicator. In some point of time I may have to get the data of some random minutes. In which case I need a time indexed dictionries like I specified earlier in my code. If I can't access the dictionary like that how else can I perform this?

You can access the dictionary with a key, but you can't assume the key is in the dictionary at all times during the test. If you must hardcode a key, you have to check for it in the dictionary before accessing. That's demonstrated in this test.

You should use the history function.

Clone Algorithm
2
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 55796f1c215a8c10c0e68cda
There was a runtime error.

Hi Suraj,
If you are looking to get a trailing window of prices you should check out the history function. Here is an example, I also showed how to truncate it to keep only data from the current trading day.

David

Clone Algorithm
0
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 55799e187ea2e910bebc6992
There was a runtime error.

Hey guys what if your trying to get a trailing window for a custom variable? I don't have much coding experience but I've been trying to create an array or panda dataframe that records a custom variable each time handle_data is called and then uses that for a 5 minute moving average. Would a dictionary work for that or do you guys know of a simple way I could achieve that?

Thanks in advance, I've been reading/learning a lot through the quantopian and panda documentation but I don't have any prior experience with python.

This is my code if you want to see what I've come up with so far. It's easy to access although I'm not sure how to really build the dataframe or add to it every time handle_data is called. I'm guessing I'd be better off using a series and a rolling window than using the dataframe I'm just not quite sure how to build it yet.

def hstry(rt):  
    rtrn_df = pd.DataFrame([rt])  
    return rtrn_df.head(4).mean()  
def handle_data(context,data):  
    rt = (context.account.total_positions_value + context.portfolio.cash)/1000000  
    rt_mva = hstry(rt)  
    print rt_mva