Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Finding stocks that opened with a gap of X%

How can I find stocks that opened with a certain gap from previous day's closing? Let's say I want to scan all (or at least a very large number, like 4000) stocks for this. Is it possible to do this in Quantopian? How about the new Pipeline API? Thanks.

10 responses

You can do this scan at 9:31am in handle_data, but not before, and you cannot do it in Pipeline since it runs at midnight or something like that. This means you are limited to looking for gaps amongst the 500 stocks in your universe, and you will miss the first minute of their post-gap trading.

OK, so that means I am still limited to the 500 stocks universe in this case :| I am currently doing what you mentioned (or rather, was doing it a few months ago when I was using Quantopian to write an algorithm on gap). Just read about Pipeline this morning, and although it seemed like it can't do this, I wanted to check if my understanding was right.

Is there a way to work around this? Say I have a list of 2000 stocks, is it possible to create a universe with 500 of these at a time, scan for the ones that meet my criteria (opened with X% gap), save the result, and then repeat for the next 500, etc.?

Thanks.

Nope, once the universe is decided at midnight, it's locked in, as far as I know.

Pinaki,

There is a kind of ridiculous hack to get more than 500 stocks into your universe. You would need to buy one share each of the first 500 the first day, one share each of the second 500 the next day, and so on. So long as you hold positions, then the stocks will be in data. Once you get up to 2000, then you could start analyzing which ones to buy at higher levels, based on your X% gap at the open criterion. And you could sell the rest.

Another hack would be to create orders that would never be filled, 500 new stocks per day. This will force the stocks to remain in data and you can work your magic. The problem, I think, is that it should work for backtesting, but for live trading at IB, all open orders are cancelled at the market close, so I think the next day, the stocks would be flushed from data when you call update_universe again.

Grant

Thanks Grant. Not sure I will try buying all 2000 stocks, but a pretty interesting idea nonetheless.

Does this help? code is still a hodgepodge that needs to be cleaned up and believe still have some issues that I never got around to clearing. Implementing in pipeline I think would be ideal if anyone if up for it (and sharing :))

Note the sharp drop in July 2015 is a pricing issue that Q's been made aware of:
https://www.quantopian.com/posts/backtest-issue-appears-as-though-buying-one-position-but-selling-another

Also this is not my code - something I picked up from here - wish I could remember where to give credit to who made it (think it may have been Grant K's actually but not sure)

Could you please explain the rationale for this algo? It looks like it's fading opening gaps, perhaps with more stringent criteria for gaps down, to account for the fact that we can't get dividend-adjusted prices?

Not to detract from Simon's question, just that I'm seeing a different result when I run that, with a leverage high of 4.78. All I did is add this:

    context.max_lvrg = 0  
def handle_data(context, data):  
    c = context  
    if c.account.leverage > c.max_lvrg:  
        c.max_lvrg = c.account.leverage  
        record(MaxLvrg = c.max_lvrg)  

Umar, Doesn't look like my code. --Grant

Once again I got fooled by the curve as it doesn't say this is without commission/slippage anywhere. Please, please attach commisson/slippage disclaimer to the returns, I checked the initialize and was happy that it wasn't there (it was before initialize.. duh.), I was already converting this to Q2...

Q, please add this request to your request list:
https://www.quantopian.com/posts/request-add-disclaimer-to-backtest-graphs-without-commissions

I would add the backtest curve with commission/slippage but for some reason attaching backtests doesn't work right now (it's stuck on loading..)