Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
New to Python: Data Structure Help

Can some one please tell me what am I doing wrong here:

import numpy as np
import pandas as pd
def initialize(context):
context.stock = sid(21508)
def handle_data(context, data):
prices = history(200, '1d', 'price')[context.stock]

I am attempting to get a time series of iShares Core S&P Small-Cap ETF.
history(200, '1d', 'price') returns a dataframe as a dictionary.

history(200, '1d', 'price'): DataFrame
dict{'2010-05-06 00:00:00+00:00': 59.64, '2010-12-16 00:00:00+00:00': 68.31, '2010-12-06 00:00:00+00:00': 66.79, '2010-09-10 00:00:00+00:00': 56.16, '2010-05-18 00:00:00+00:00': 60.68, ...}
2010-05-06 00:00:00+00:00: 59.64
2010-12-16 00:00:00+00:00: 68.31
2010-12-06 00:00:00+00:00: 66.79
2010-09-10 00:00:00+00:00: 56.16
2010-05-18 00:00:00+00:00: 60.68
2010-09-15 00:00:00+00:00: 57.59
2010-10-19 00:00:00+00:00: 61.11
2010-09-28 00:00:00+00:00: 59.2

Above dictionary object values seem correct.

BUT when I attempt to convert it to a timeseries structure by:
prices = history(200, '1d', 'price')[context.stock]

I get a timeseries with prices repeated periodically like this:
0_2010-03-23 00:00:00+00:00: 60.96
1_2010-03-24 00:00:00+00:00: 60.15
2_2010-03-25 00:00:00+00:00: 59.68
3_2010-03-26 00:00:00+00:00: 60.15
4_2010-03-29 00:00:00+00:00: 60.96
5_2010-03-30 00:00:00+00:00: 60.15
6_2010-03-31 00:00:00+00:00: 59.68
7_2010-04-01 00:00:00+00:00: 60.15
8_2010-04-05 00:00:00+00:00: 60.96
9_2010-04-06 00:00:00+00:00: 60.15
10_2010-04-07 00:00:00+00:00: 59.68
11_2010-04-08 00:00:00+00:00: 60.15
12_2010-04-09 00:00:00+00:00: 60.96
13_2010-04-12 00:00:00+00:00: 60.15
14_2010-04-13 00:00:00+00:00: 59.68

What am I doing wrong here?
Thanks!

4 responses

Someone from Quantopian can confirm but this is probably the bug in the debugger.

See previous thread https://www.quantopian.com/posts/strange-price-history-in-debugger

You can sometimes get around this by a variety of different printing strategies which you can run inside the debugger. e.g.

print ",".join([str(x) for x in prices])  

Thanks James! This is disappointing. When the debugger itself is buggy, it defeats the purpose of having one (debugger). Being able to debug confidently is invaluable when learning a new environment and language.

Hi Sam and James,
Thanks for helping find this bug. We have a fix for this getting reviewed and it should be on production in a day or two. I'll let you know when it's there.

Karen

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.

Great! Thanks Karen.