Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
stddev of mavg & console

Is it possible to get stddev of a mavg? In the code below, I get the following error: Runtime exception: AttributeError: 'float' object has no attribute 'stddev'

Also, is there a way to bring up a console in order to print the contents of variables or dataframes?

Thanks! Looking forward to contributing but still learning. Saw a few meetups in SF so I plan on going to those too.

def initialize(context):

    context.spy = sid(8554)

def handle_data(context, data):  
    price = data[context.spy].price  
    ma = data[context.spy].mavg(50)  
    if ma != None:  
        ma = data[context.spy].mavg(50)  
    std = ma.stddev(20)  
    if std != None:  
        std = ma.stddev(20)/ma  
2 responses

Hello Victor,

I though a simple way would be a deque of moving averages.

P.

Hello Victor (and Peter),

Here's an approach using pandas:

import pandas as pd

set_commission(commission.PerShare(cost=0))  
set_slippage(slippage.FixedSlippage(spread=0.00))

def initialize(context):  
    context.secs =   [ sid(19662),  # XLY Consumer Discrectionary SPDR Fund  
                       sid(19656),  # XLF Financial SPDR Fund  
                       sid(19658),  # XLK Technology SPDR Fund  
                       sid(19655),  # XLE Energy SPDR Fund  
                       sid(19661),  # XLV Health Care SPRD Fund  
                       sid(19657),  # XLI Industrial SPDR Fund  
                       sid(19659),  # XLP Consumer Staples SPDR Fund  
                       sid(19654),  # XLB Materials SPDR Fund  
                       sid(19660) ] # XLU Utilities SPRD Fund  
def handle_data(context, data):  
    # get trailing window of daily closes as pandas dataframe  
    prices = history(51,'1d','price')[0:-1] # drop last minute bar in frame  
    window = 6  
    rolling_mean = pd.rolling_mean(prices,6)[window-1:] # drop leading NaNs  
    std = rolling_mean.std(axis=0) # pandas series (sorted ascending by sid)  
    print get_datetime()  
    print std  
    print type(std)  

If you clone the algo and run it, see the log output.

No guarantee I haven't missed something here...I recommend doing some checks.

Grant