Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Using history to determine if yesterdays close is greater than current

I'm trying to figure out how to compare current minute open to yesterdays close.

So far I have this:

close_prices = data.history(symbols,  'close', 2, '1d')  
previous_day_close = close_prices.iloc[-2]

def uptrendcheck():  
    if 'previous_day_close' < 'currentprice':  
        return True  
    else:  
        return False  

I'm getting 'NameError: name 'data' is not defined'.

Of course, price is defined elsewhere and working fine.

Please can someone tell me what I'm doing wrong - I'm quite new to this :)

Thanks in advance

3 responses

Try this:

def initialize(context):  
    schedule_function(uptrend_check, date_rules.every_day(), time_rules.market_open())

def uptrend_check(context, data):  
    prices = data.history(symbol('SPY'), 'price', 2, '1d')  
    current_price = prices[-1]  
    previous_day_close = prices[-2]     

    log.info("Current: %s Previous Close:%s" %(current_price, previous_day_close))    

    if previous_day_close < current_price:  
        print "Uptrend"  
    else:  
        print "Downtrend"  

That works but it doesn't output the right format; I need to be able to do this:

def trade(context, data):  
        open_rules= 'open > yesterday_close'  

using

context.assets = [sid(51231)]  
    yesterday_close = data.history(context.assets,'close', 2, '1d')  

I think I'm putting things in the wrong places? Sorry to be wasting everyone's time because I'm a total newbie!

Maybe this will help you:

# ------------------------------------------  
assets, uptrend = symbols('ROKU','TLT'), {}  
# ------------------------------------------  
def initialize(context):  
    schedule_function(uptrend_check, date_rules.every_day(), time_rules.market_open(minutes = 65))

def uptrend_check(context, data):  
    for sec in assets:  
        todays_open = data.history(sec, 'open', 2, '1d')[-1]  
        previous_day_close = data.history(sec, 'close', 2, '1d')[-2]  
        uptrend[sec] = True if previous_day_close <= todays_open else False  
    print uptrend