Back to Community
Best way to filter out stocks with a large gap up in their price history?

I want to put a filter into my pipeline to exclude any stocks that have gapped up by more than 15% in the last x days. I was thinking I'd build the following custom factor logic:

  1. Pull in daily returns for window_length = x
  2. Build a function pct_change that works out % change from previous open to current close
  3. Build the compute function with a for loop to run pct_change over the daily returns to create a list
  4. Output the maximum value from the list

I'd then build a filter in the pipeline.

I'm not the fastest of coders so wanted to get an idea of whether or not this is a feasible/good approach before I invest some time building it! Thanks in advance.

8 responses

You can use the builtin factor Returns for that:

from quantopian.pipeline.factors import Returns

x = 20

gap_filter = Returns(window_length=x) < .15

Ah. I meant “prices” in my first bullet clearly! Thanks for the heads up on Returns. However, wouldn’t the above give me the return over the total window length rather than a maximum single inter day move within the window length?

Yes you're right, I missunderstood the question. So you want the daily percent change not to exceed 15 % for any given day in a x-day window?

Yep although most big changes happen overnight so would be good to get the inter day rather than intra day movement. Basically I don't want the open to be more than Y% greater than the previous day's close in any of the last X days.

Ok. This one returns the maximum overnight gap percentage for the given window lenth:

class MaxOvernightGap(CustomFactor):  
    inputs = [USEquityPricing.open, USEquityPricing.close]  
    window_length = 20

    def compute(self, today, assets, out, o, c):  
        overnight_gap = o[1:] / c[:-1] - 1  
        max_gap = np.nanmax(overnight_gap, axis=0)  
        out[:] = max_gap


Awesome, thanks a million for helping out on this. Just worked through why that works - very clever the removal of the first and last prices :-) I also learnt a bit about numpy arrays - you can divide them by each other. Lots of birds, minimal stones. Cheers again.

Youre welcome.
If I remember correctly, I got that trick with removing last and first values from one of the codes here in the forum ;)
Numpy arrays are really useful, especially in custom factors. Of course you could do all that with for-loops but it would take much longer to compute. It really pays out do digg further into numpy.

Will do! Thanks again.