Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Waits: Simple per security waiting period any number of days

I use this quite a bit to wait a few days after closing a position before opening that stock again, usually with pretty good results. s is short for security

def initialize(context):  
    context.waits = {}

def before_trading_start(context, data):  
    wait(context)    # Increment any that are present

def trade(context, data):  
    for s in context.portfolio.positions:  
        order_target(s, 0)                        # close  
        wait(context, s, 3)                       # enter to begin the wait 3 days

    for s in context.pipe_out.index:  
        if s in context.waits.keys(): continue    # skip open if on the wait list  
        order_target_percent(s, weights[s])

def wait(c, s=None, action=None):  
    ''' Enter any security like after sell to wait a few days  
          before opening the position again.  
          They are removed automatically after number of days specified.  
    '''  
    if s and days:  
        if   days  > 0:  
            c.waits[s] = days             # start wait, number of days  
        elif days == 0: del c.waits[s]    # end wait manually  
    else:  
        for s in c.waits.copy():  
            c.waits[s] -= 1               # decrement, count down 1 day  
            if c.waits[s] <= 0:  
                del c.waits[s]            # end wait