Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Market Timing before Trading begins

If I schedule my rebalance to occur daily at 10:30am EST this leaves me with approx 1 hour between the market open vs when the algo's function is scheduled. I've been thinking of ways to analyze each security in my pipeline for that day in that buffer between 9:30am and 10:30am. The reason being is I want to see if there are any significant movements that occured before my function runs at 10:30am.

For example, say I have Stock A in my pipeline with a buy(long) signal. However, between market open and 10:30 am the stock fell 15% due to some unforeseen news event.

I am looking for different ways I can "hold" or "freeze" buying Stock A at 10:31am based on the opening results since I don't want to go long a stock which just fell 10% as I would want to digest what caused the drastic fall in price. Maybe something along the lines of a log output that consists of a running list of those specific stocks within my pipeline whos pre-rebalance time changed +- 15% with an opposite buy/sell signal.

2 responses

A start:

import pandas as pd  
TradingMinute = 60

def trade(context, data):  
    pipe = context.pipeline_data

    # TradingMinute-1 for back to market open  
    hst = data.history(pipe.index, 'price', TradingMinute-1, '1m').ffill()

    ratios = pd.DataFrame(hst.index, columns=['now', 'then', 'ratio'])  
    ratios['now']   = hst.ix[-1]  
    ratios['then']  = hst.ix[0]  
    ratios['ratio'] = ratios['now'] / ratios['then']

    # https://quantopian.com/posts/overview-of-pipeline-content-easy-to-add-to-your-backtest  
    log_data(context, ratios, 4)        # show data  
    '''  
        2011-01-04 06:58 log_data:264 INFO _ _ _   ratio   _ _ _  
            ... ratio highs  
                   now    then     ratio  
          VECO   45.13   43.66  1.033669  
          TIVO    9.01    8.76  1.028539  
           WLT  134.45  130.82  1.027748  
           CZZ   14.21   13.84  1.026734  
            ... ratio lows  
                  now   then     ratio  
           ANV  25.49  26.33  0.968097  
          FINL  16.82  17.39  0.967223  
           ESI  63.03  65.30  0.965237  
          MPWR  15.80  16.88  0.936019  
    '''

    to_drop = ratios[ratios.ratio < .97]

    print to_drop.sort_values('ratio').tail()  
    print to_drop.sort_values('ratio').head()

    print 'to be excluded: {}'.format( to_drop.index.tolist() )  
    assert 0  # a deliberate halt while testing, see Logs tab for output

    # another route  
    #to_examine = ratios.sort_values('ratio').tail()  # highs  
    #to_examine = ratios.sort_values('ratio').head()  # lows

... delivering a list of security id's to look into:

2011-01-04 06:58 PRINT to be excluded: [Equity(2845 [FINL]), Equity(24831 [ESI]), Equity(26811 [MPWR]), Equity(33832 [ANV])]

Awesome, thanks Blue. This is a great start and I will work on implementing it into an algo. Once I get everything to work I can share my results.