Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Gap Up/Down Strategy

Hi Community! Looking for some guidance on how to best code my strategy. Unfortunately, i haven't been able to piece everything together in order to share my backtest (errors) but I've copied/pasted what i have so far. Can anyone help me implement this type of system into the new Optimize format?

Strategy:
1. Get a list of QTradable stocks.
2. Find the gap up or down by percentage from the gap from the day before yesterday's close to today's open (minimum gap up/down = 5% or -5%).
3. Sort the stocks in order by gap up or down percentage
4. For the top N gap up stocks (largest gainers): go long at current day open and sell at the current close of the trading day
5. For the bottom N stocks (largest losers): go short at current day open and cover at the close of the trading day

class ValueDaybeforeYesterday(CustomFactor):
window_length = 2
def compute(self, today, assets, out, values):
out[:] = values[0]

class ChangeAverage(CustomFactor):
def compute(self, today, assets, out, values):
mean = pd.DataFrame(values).pct_change().mean()
out[:] = mean.values

class ChangeAverageLog(CustomFactor):
def compute(self, today, assets, out, values):
df = pd.DataFrame(values)
mean = (df.shift(1) / df).mean().apply(np.log)
out[:] = mean.values

class calc_gap(context, data):
def compute(self,today,assets,out,values):
df = pd.DataFrame(values)
open_price = data.current(sids,'price').rename("open")
df = pd.concat([context.output, open_price], axis = 1)
df['gap'] = df['open'] / df['close_day_before_yeseterday'] - 1
out[:] = df[(df['gap'] > 0.05) ]

"*************************************Make_Pipeline*************************************" def make_pipeline():

pipe = Pipeline()  
base_universe = QTradableStocksUS()  
dollar_volume = AverageDollarVolume(window_length=30)  
high_dollar_volume = dollar_volume.percentile_between(95, 100)  

close_day_before_yeseterday = ValueDaybeforeYesterday(inputs = [USEquityPricing.close])  
volume_day_before_yeseterday = ValueDaybeforeYesterday(inputs = [USEquityPricing.volume])  
pipe.add(close_day_before_yeseterday, "close_day_before_yeseterday")  

return_change_mean = ChangeAverage(inputs = [USEquityPricing.close], window_length = 5)  
volume_change_mean = ChangeAverage(inputs = [USEquityPricing.volume], window_length = 5)  

open_price = data.current(context.security_list,price,1,"1m")  

longs = open_price > close_day_before_yeseterday  
shorts = open_price < close_day_before_yeseterday  



my_screen = base_universe  & high_dollar_volume  
pipe.set_screen(my_screen)  

return pipe  
1 response

Hi Daniel

I am actually working (slowly due to my job duties) on the same or similar trading gaps' idea. I easily can implement your idea. In fact it is already kinda embedded in my code which is doing much more (I was probably overdoing it with analyzing 3 first 5-min candle pattern and implementing trailing stop with offset etc - more complicated and maybe overkill).

I also see the tag "collaboration request". What if we collaborate together on coding and back-testing? The idea on itself is not worth that much and there is a lot to be done before it starts bringing money if ever.

Do you want to send me PM at dmitry dot kishkinev at gmail dot com? We could go from there.