Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How would I check for a gap up or gap down to previous day's close

Algorithm is running minutely data.

How would I capture the previous day's close to test for a gap on the subsequent day's open?

3 responses

Chuck,

Attached, you'll find an example (hopefully bug-free). It'll only handle one security as-is. The trick is to store the most recent previous price at the end of each iteration:

context.previous_price = current_price  

Then, when a new day is detected, the opening price can be compared to the closing price from the prior day.

You might also be interested in https://www.quantopian.com/posts/trading-earnings-surprises-with-estimize-data, which inspired me to write the attached algorithm.

Grant

A trader named Scott Andrews (with whom I have no affiliation - he maintains a decent web site: "masterthegap" ) makes a living trading opening gaps in index products. I think he trades manually though he backtests in TS and Excel.

It's extremely easy and short in Excel and TS to code. This seems like absurdly long and complicated code in Quantopian for such a super simple concept like opening gaps. Anyone have any opinions on this?

Hello Francis,

The code I posted above could be cleaned up and clarified, undoubtedly...I just cobbled together an algorithm. Specifically, the collection of flags and counters is kinda ugly:

    context.previous_price = 1  
    context.initialize = True  
    context.event_day = 0  
    context.new_day = False  
    context.day_counter = 0  
    context.bought = False  
    context.day_submitted = 0  
    context.num_shares = 0  
    context.hold_period = 0  

I'll think about how to improve the code, and I'm open to suggestions.

Grant