Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
pd.rolling_max() throw attribute error
import pandas as pd;  
.
def handle_data(context, data):  
    high_prices = data[context.security].high;  
    period9_high = pd.rolling_max(high_prices, window=9);

I am getting this error...
Runtime exception: AttributeError: 'float' object has no attribute 'dtype'

Could this be a platform issue?

5 responses

Hi Patrick,

the data object only has the current day (for daily data) or the current minute (for minutely data). To get historical prices use the history function.

e.g.

import pandas as pd  

def initialize(context):  
    context.Sid = symbol('AAPL') # just an example

def handle_data(context, data):  
    high_prices = history(10, '1d', 'high')  
    period9_high = pd.rolling_max(high_prices, window=9)  

and period9_high will be something like:

Equity(24 [AAPL])  
2010-12-21 00:00:00+00:00                NaN  
2010-12-22 00:00:00+00:00                NaN  
2010-12-23 00:00:00+00:00                NaN  
2010-12-27 00:00:00+00:00                NaN  
2010-12-28 00:00:00+00:00                NaN  
2010-12-29 00:00:00+00:00                NaN  
2010-12-30 00:00:00+00:00                NaN  
2010-12-31 00:00:00+00:00                NaN  
2011-01-03 00:00:00+00:00             47.181  
2011-01-04 00:00:00+00:00             47.501  

Hope that helps :)

James

Thanks James! Yes, your sample codes help me.

Why 10 and not 9 in this function call?

high_prices = history(10, '1d', 'high')  

To pull 47.501 out of period9_high, is it correct to do this?
period9_high = float(period9_high.ix[-1])

Patrick,

Be sure to have a look at the help page to understand the difference between history() when running your backtest on daily bars versus minute bars:

Returned Data

The returned data for daily history, for each day, is:

open_price: the open of the first minute bar of the given day.  
high: the maximum of the minute highs for the given day.  
low: the minimum of the minute bar lows for the given day.  
close_price: the close of the last minute bar in the specified period. This value is not forward-filled. If there is no price in the bar, the returned price is NaN.  
price: the close of the last minute bar as of the specified period. This value is forward-filled. If there is no price in the bar, the returned price is the previous price, until there is new trade data available.  
volume: the sum of all minute volumes. For the current day, the sum of volume thus far.

So, for minute units, you'll get high prices for the trailing days plus the high for the current day (between market open and the current minute). If you only want the highs for the trailing days, you'll need to ignore the last row in the dataframe returned by history().

Grant

@Patrick,

No reason - 9 is just fine!

period9_high = float(period9_high.ix[-1]) works if the algo is only using one stock.

If you had more than one stock then its an error because pd.rolling_max returns a dataframe, so with multiple stocks, it can't convert multiple values to just one float. See this:

import pandas as pd

def initialize(context):  
    context.ListOfSids = symbols('AAPL', 'C')

def handle_data(context, data):  
    high_prices = history(9, '1d', 'high') # this is a dataframe  
    period9_high = pd.rolling_max(high_prices, window=9) # this returns a dataframe  
    print period9_high  
    period9_high = period9_high.ix[-1] # this returns a series (most recent 9 period high for all stocks)  
    print period9_high  
    AAPL_high = period9_high[symbol('AAPL')]  
    C_high = period9_high[symbol('C')]  
    print AAPL_high  
    print C_high  

gives

                                       Equity(24 [AAPL])  Equity(1335 [C])  
2010-12-22 00:00:00+00:00                NaN               NaN  
2010-12-23 00:00:00+00:00                NaN               NaN  
2010-12-27 00:00:00+00:00                NaN               NaN  
2010-12-28 00:00:00+00:00                NaN               NaN  
2010-12-29 00:00:00+00:00                NaN               NaN  
2010-12-30 00:00:00+00:00                NaN               NaN  
2010-12-31 00:00:00+00:00                NaN               NaN  
2011-01-03 00:00:00+00:00                NaN               NaN  
2011-01-04 00:00:00+00:00             47.501              49.4  
Equity(24 [AAPL])    47.501  
Equity(1335 [C])     49.400  
Name: 2011-01-04 00:00:00+00:00, dtype: float64  
47.501  
49.4  

As Grant suggests, do read the help. There's loads of example algos too

James

Thanks guys for your responses. I have been referring to the API doc but stuck with basic knowledge. For example, I read that pd.rolling_max() returns dataframe. But, I don't know how to get values out of dataframe object.

Through example like the ones you have given, I get it.