Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Iterating over price data?

. I'm having some difficulty working with the price data as its represented. For example

prices_close = history(10, '1d', 'close_price')

If I print this, I get:

2015-07-09PRINT Equity(24 [AAPL])
2015-06-25 00:00:00+00:00 127.51
2015-06-26 00:00:00+00:00 126.78
2015-06-29 00:00:00+00:00 124.58
2015-06-30 00:00:00+00:00 125.42
2015-07-01 00:00:00+00:00 126.58
2015-07-02 00:00:00+00:00 126.40
2015-07-06 00:00:00+00:00 125.99
2015-07-07 00:00:00+00:00 125.69
2015-07-08 00:00:00+00:00 122.59
2015-07-09 00:00:00+00:00 120.03

I would like to iterate over this somehow and get a list that I can process further, for example:

[127.51,126.78,124.58,125.42,126.58,126.40,125.99,125.69,122.59,120.03]

I'm currently treating it as a string, parsing it out and then reconverting to a float however this is terribly inefficient and prone to error. Is there another method that is recommended?

thanks!

2 responses

History() will return a pandas dataframe - the columns are all of the stocks in your algo and the rows are the dates.

To get the history specifically for AAPL, you can index into the dataframe. This will return a series:

prices_close = history(10, '1d', 'close_price')  
aapl_prices_close = prices_close[sid(24)]  

Does this work for your use case? Here is a good tutorial to learn more about history dataframes: https://www.quantopian.com/posts/working-with-history-dataframes

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.

perfect! thank you:)