Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
What is wrong?
def initialize(context):  
    context.stock=sid(8554)

def handle_data(context, data):  
    closev = history(7, "1d", "close_price")  
    if (closev.iloc[-2] > closev.iloc[-1] and closev.iloc[-3] > closev.iloc[-4] and closev.iloc[-1] > closev.iloc[-3] and closev.iloc[-5] > closev.iloc[-6]) and closev.iloc[-4] > closev.iloc[-5] :  
    order_target_percent(context.stock, 1)  

This does not build. I get " SyntaxError: expected an indented block" for the last line

7 responses

You have a colon in the second to last line, so the code is expecting an indent on the next line. Since you want that order target percent under the if statement, you have to indent it one more time.

Thanks john. I fixed that and know I get the following:

"ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). There was a runtime error on line 17."

Then on line 17 somewhere in your code, you are calling a list where the program is expecting a boolean value; that is, your code is written in a way so the computer expects a True or False, but you're calling a list[] or a list[value] instead.

if (closev.iloc[-2] > closev.iloc[-1] and closev.iloc[-3] > closev.iloc[-4] and closev.iloc[-1] > closev.iloc[-3] and closev.iloc[-5] > closev.iloc[-6] and closev.iloc[-4] > closev.iloc[-5]):  

It's telling that in the above:

"ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). There was a runtime error on line 6."

Also you might want to initialize your closev as a series with something like:

def initialize(context):  
    context.stock=sid(8554)  



def handle_data(context, data):  
    closev = {}  
    closev = history(7, "1d", "close_price")  
    if closev[-2] > closev[-1] and closev[-3] > closev[-4] and closev[-1] > closev[-3] and closev[-5] > closev[-6] and closev[-4] > closev[-5] :  
         order_target_percent(context.stock, 1)  

Preface: I'm not a Python pro and fumbled through something similar in one of my algos.

I believe you're getting the Series error because you're trying to put a price series into a variable and you have to define closev as a data frame since it's holding multiple frames of data.

Thanks Tony but I still get the same message:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
There was a runtime error on line 7.

closev is a Dataframe(in this case, essentially a 2D spreadsheet, with the row index being date-time objects, and the columns being the stocks.
Indexing into that requires a bit of work. (See below)
Also, you need to use the 'debugger' to get at this problem...clik on a line number to stop on, use "Build Algorithm", and print out the values of the variables you are interested in to see their structures, so that you can access them.
https://www.quantopian.com/help#debugger

Here is the modified code :

def initialize(context):  
    context.stock=sid(8554)  

def handle_data(context, data):  
    closev = {}  
    closev = history(7, "1d", "close_price")  
    stk_pr = closev[context.stock]  

    if stk_pr[-2] > stk_pr[-1] and stk_pr[-3] > stk_pr[-4] and stk_pr[-1] > stk_pr[-3] and stk_pr[-5] > stk_pr[-6] and stk_pr[-4] > stk_pr[-5] :  
         order_target_percent(context.stock, 1)  

Good luck!
alan