Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
NameError: name 'open_positions' is not defined

Hello,

I try to run this simple code:

def initialize(context):  
    context.aapl = sid(24)

schedule_function(open_positions,date_rules.week_start(),time_rules.market_open())  
schedule_function(close_positions,date_rules.week_end(),time_rules.market_close(minutes=30))  
def open_positions(context,data):  
    order_target_percent(context.aapl,0.10)  
def close_positions(context,data):  
    order_target_percent(context.aapl,0)  

but getting error notification:

5 Warning Undefined name 'open_positions'
5 Error Runtime exception: NameError: name 'open_positions' is not defined
6 Warning Undefined name 'close_positions'

What's wrong with my code?

Thanks in advance

3 responses

OK, I got it. The schedule should be in: def initialize(context). So just tab it. It should look like this:

def initialize(context):  
    context.aapl = sid(24)

    schedule_function(open_positions,date_rules.week_start(),time_rules.market_open())  
    schedule_function(close_positions,date_rules.week_end(),time_rules.market_close(minutes=30))  
def open_positions(context,data):  
    order_target_percent(context.aapl,0.10)  
def close_positions(context,data):  
    order_target_percent(context.aapl,0)  

Try this way:

def initialize(context):  
    context.aapl = sid(24)

    schedule_function(open_positions, date_rules.week_start(), time_rules.market_open())  
    schedule_function(close_positions, date_rules.week_end(), time_rules.market_close(minutes = 30))  

def open_positions(context,data):  
    order_target_percent(context.aapl, 0.10)  

def close_positions(context,data):  
    order_target_percent(context.aapl, 0)  

Space Matters, in Lukasz code there is no space and it worked. Thanks