Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
problem w/ history API & pandas rolling mean

When I try to run the code below, I get the error:

Runtime exception: TypeError: issubclass() arg 1 must be a class

Seems it should work, right? The rolling_mean should accept the price_history dataframe after the warm-up period, right? What am I missing?

Grant

# http://pandas.pydata.org/pandas-docs/dev/computation.html

import pandas as pd

window_roll = 30

def initialize(context):  
    context.stocks = [sid(8554),sid(32268)]  
def handle_data(context, data):  
    price_history = history(180, '1d', 'price')  
    price_history = price_history.fillna(method='ffill')  
    prices_rolling = pd.rolling_mean(price_history,window_roll)  
8 responses

Hello Grant,

I've been unable to get 'history' to work with TA-LIb 'natively' in Quantopian or via importing TA-Lib from zipline. 'History' involves a new 'MagicMock' data structure which returns a DataFrame of objects rather than floats. I think it may be too early in the development of 'history' for both of us to do what we want. Perhaps Eddie can advise?

P.

Hi Guys,

I know that Eddie has the docs just about ready to post. Since I was the one who let the history cat out of the bag early, let me give a couple quick answers.

One cool thing about history is that there is no more warmup - you have the full dataframe backfilled and available at the start date. Also, the object returned is a dataframe of prices, so the prior rolling transforms can be replaced with their pandas counterparts - which is also great for flexibility. As an example, to compute and plot a 20 day moving average of AAPL's close prices you can just do:

price_history = history(bar_count=20, frequency='1d', field='price')
sma = price_history.mean()

record(AAPL_20day_moving_average=sma[sid(24)])

I'll let Eddie speak to the TA-Lib functionality. --jess

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.

Hello Peter and Jess,

It seems a few technical implementation details were missing from the history API spec. that we reviewed. I don't recall hearing that the dataframe would somehow be non-standard (never heard of 'MagicMock').

Yes, Peter, I noticed the weirdness with respect to the dataframe of objects. Even after converting to a numpy ndarray, I had to apply x.astype(float) to use the data in a scipy function. Perhaps my problem with the pandas rolling mean is related.

Jess, thanks for the code example, but I actually need the rolling mean, since it effectively smooths the data set for subsequent analysis. I'm looking forward to the full release of the history API, although if I recall, we'll only have access to 5 trading days of minute-level data...bummer.

Best regards,

Grant

Tried this, and still no worky:

# http://pandas.pydata.org/pandas-docs/dev/computation.html

import pandas as pd

window_roll = 2

def initialize(context):  
    context.stocks = [sid(8554),sid(32268)]  
def handle_data(context, data):  
    price_history = history(10, '1d', 'price')  
    price_history = price_history.fillna(method='ffill')  
    prices = price_history.as_matrix(context.stocks).astype(float)  
    prices_rolling = pd.rolling_mean(prices,window_roll)  

Hi Jess,

Is the relevant history API code in github? Perhaps you could point me to it; maybe I can sort out what's going on.

Grant

Peter, Grant:

The problem with using TA-Lib or pd.rolling_mean with the history API is in a step where we do some sanity checking on the code.
When the sanity checking passes, a real DataFrame is used during the run of a backtest or live trading session.
We are working on a fix to the sanity checking step so that it is not creating that false error.

Grant:

The impending official first release will only support:
- frequency of '1d', i.e. '1m' will not be supported.
- The OHLCV pricing data, i.e. fetcher data will not be supported.
The help documentation will be updated accordingly when the history API is officially released.
Then, as we add more features to the history API, we will update the documentation, and keep everyone updated with forum posts.
Please treat the help documentation, not the draft proposal, as the canonical source of what we support with the history API.
We appreciate the valuable feedback you and others have provided about the proposal, and have incorporated some of that feedback, but that document is more of a goalpost than something that is set in stone.

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.

Thanks Eddie. --Grant

The problem appears to be fixed--thanks! I've attached some code, in case someone wants to play around. --Grant