Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Returning pipeline with previous trading day's close to today's open gap

Dear all

I am relatively new on Quantopian and looking for a custom factor / pipeline way to make a pipeline which would calculate and select out only equities with gaps (previous trading day's close to today's open) with specific threshold (e.g. selecting only gaps between 1% and 3% as of gap size/last day's close to normalize gaps and select out "biggish but not too much" gaps).

I read the pipeline tutorial and found a couple of examples. I am looking in something like that (below) but not sure it works. Obviously, one could achieve that without using pipeline but I believe that pipeline is more powerful tool.

Any help is very appreciated.

Snippet of a code

from quantopian.research import run_pipeline  
from quantopian.pipeline.data import morningstar  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline import CustomFactor, Pipeline  
import numpy as np

class Gap(CustomFactor):  
    """  
    Computes the difference between close price of a previous trading date and  
    open price today.  
    """  
    inputs = [USEquityPricing.close, USEquityPricing.open]

    def compute(self, today, assets, out, close, open):  
        out[:] = open[0] - close[-1] # here I foresee some issues. Not sure it should be done like that


def make_pipeline():

    yesterday_close = USEquityPricing.close  
    tradable_gaps = ((gap_pct > 1) & (gap_pct < 3)) # defining what range of gaps I am interested in (1-3% i.e. gap size normalized to close price)

    return Pipeline(  
        columns={  
            'gap_pct': gap_pct,  
            },  
        screen=tradable_gaps  
    )

result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05')  
result.head()  
2 responses

Dmitry, please keep in mind that Pipeline runs every day after market close (precisely before market open of the next day) before having the open price for the next day. So a factor can compute the gap between yesterday open and two day ago close. This is not what you probably expect.

Happened across this locally and remembered seeing your message about the gap.

# the biggest absolute overnight gap in the previous 90 sessions  
class MaxGap(CustomFactor):  
    inputs = [USEquityPricing.close]  
    window_length = 90  
    def compute(self, today, assets, out, close):  
        abs_log_rets = np.abs(np.diff(np.log(close),axis=0))  
        max_gap = np.max(abs_log_rets, axis=0)  
        out[:] = max_gap  

Some of the surroundings in the file in case might help in a search:

# Implementation of the Stocks On The Move system by Andreas Clenow  
# http://www.followingthetrend.com/stocks-on-the-move/


def initialize(context):  
    context.spy = sid(8554)  
    set_benchmark(context.spy)  
    momentum = MomentumOfTopN()  
    mkt_cap = MarketCap()  
    max_gap = MaxGap()  
    atr = ATR()  
    latest = Latest(inputs=[USEquityPricing.close])  
    mkt_cap_rank = mkt_cap.rank(ascending=False)  
    universe = (mkt_cap_rank <= UniverseSize)  
    momentum_rank = momentum.rank(mask=universe, ascending=False)  
    sma100 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=100)


    pipe = Pipeline()  
    pipe.add(momentum, 'momentum')  
    pipe.add(max_gap, 'max_gap')  
    pipe.add(mkt_cap, 'mkt_cap')  
    pipe.add(mkt_cap_rank, 'mkt_cap_rank')  
    pipe.add(sma100, 'sma100')  
    pipe.add(latest, 'latest')  
    pipe.add(atr, 'atr')  
    pipe.add(momentum_rank, 'momentum_rank')  
    # pre-screen all the NaN stuff, and crop down to our pseudo-S&P 500 universe  
    pipe.set_screen(universe &  
                    (momentum.eq(momentum)) & # these are just to drop NaN  
                    (sma100.eq(sma100)) &  
                    (mkt_cap.eq(mkt_cap))  
                   )  

Possibly from https://www.quantopian.com/posts/stocks-on-the-move-by-andreas-clenow