Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why won't this algo run?

Hi,
I'm trying to grasp some of the more basic features of the IDE. I want to put a single stock into my universe, get a couple mavg and then do a comparision every day. I went and looked at the simple algorithm in the docs and I came up with this

def initialize(context):  
    context.security = symbols('SPY')

def handle_data(context, data):  
    ma1 = data[context.security].mavg(33)  
    ma2 = data[context.security].mavg(205)  
    if ma1 > ma2:  
        order_target_percent(context.security, 1.0)

    elif ma1 < ma2:  
        order_target_percent(context.security, 0.00)  

The error I get says TypeError: unhashable type: 'list' at line 8
but this is the same way the algo in the docs here https://www.quantopian.com/help#sample-basic assigns mavg to a variable. What am I missing?
Thanks,
Aaron

4 responses

I went looking around at some of the algos in the community forums and figured out that I should be doing what I was trying like this

def initialize(context):  
    context.spy = sid(8554)  
def handle_data(context, data):

    ma1 = data[context.spy].mavg(33)  
    ma2 = data[context.spy].mavg(200)  
    if ma1 > ma2:  
        order_target_percent(context.spy, 1.0)

    elif ma1 < ma2:  
        order_target_percent(context.spy, 0.00)  

I'm not sure why the first algo is breaking, but this is how it is supposed to be done.

symbol (as in the Quantopian example you referenced) and sid return an Equity; symbols returns a list of Equity objects. The data dictionary is indexed by Equity, not by list.

@ Aaron Olson

It will run if you change:

context.security = symbols('SPY')  

to
context.security = symbol('SPY')

Thanks @ Vladimir Yevtushenko
I appreciate your help!