Gap Trading. An Introduction & Back-test in Python.

Introducing Gaps and How to Identify and Trade Them.

Sofien Kaabar
Dec 29, 2020 · 7 min read

Gaps form an important part of price action. They vary in rareness from market to market. For instance, in the currencies market, they usually happen on opening following the weekend or whenever there is a big announcement while in stocks, gaps are fairly common from one day to another. In this article, we will see the different types of gaps and then code a scanner in Python that finds them and applies the well-known practice of “filling the gaps” as an algorithmic trading strategy. Intuitively, the strategy should underperform as no technique this simple can provide alpha but it is interesting to know how to code this system in Python.

If you would like to see more on patterns, feel free to have a look at the article below I recently published on Medium:

Introduction to Gaps

A gap is a discontinuation or a hole between prices. When a market is trading at $100 and suddenly trades at $102 without ever quoting at $101, it has formed a gap. This can be seen in the charts where it appears to have a missing piece between candlesticks. Take a look at the below chart on the GBPUSD and notice the empty space in grey between candlesticks.

Two filled gaps on the GBPUSD 3H data. (Image by Author)

Gaps can occur due to fundamental and technical reasons, but we are mostly interested in identifying and trading them. In the currencies market, the visible gaps are the ones that occur during the weekend. Since it is traded all day long for 5 days a week, the presumed gaps would probably look like giant candles, but since we cannot know for sure, we will stick to the common definition of gaps.

“We call the act of trading based on gaps: Playing the gap.”

There are different types of gaps and distinguishing them can be quite tricky:

  • A common gap: It generally occurs in a sideways markets. It is likely to be filled because of the market’s mean-reversion dynamic.
  • A breakaway gap: It generally resembles a common gap but the gap occurs above a graphical resistance or below a graphical support. It signals acceleration in the new trend.
  • A runaway gap: It generally occurs within the trend but it confirms it more, therefore, it is a continuation pattern.
  • An exhaustion gap: It generally occurs at the end of a trend and close to a support or resistance level. It is a reversal pattern.

Note that most of the above specificities come from personal experience as some sources state that common gaps are least likely to be filled. Also, the runaway and exhaustion gaps are so similar that it is almost impossible to know which is which at the moment they appear, therefore, they suffer from hindsight bias.

Breakaway gap on the GBPUSD 3H data after surpassing resistance. (Image by Author)

The above chart shows a chart on GBPUSD with a breakaway gap after a triple bottom bullish reversal pattern following the surpass of its resistance. This gives credit that the move will likely continue higher.

In this article, we will be considering that all gaps should be filled and therefore, all gaps encountered by the algorithm coded below are common gaps. Many explanations can be used to explain why gaps are filled:

  • An overly optimistic or pessimistic gap reaction which makes participants go in the other direction to fade this gap. This is also called Irrational Exuberance, a term first coined by Alan Greenspan.
  • The gap occurs around a support or resistance level forcing it to reverse course.
Exhaustion gap at the end of a downtrend and close to a Harmonic support. (Image by Author)

To learn more about Harmonic Patterns and how to find key market reactions, you can read more about them here:

Creating a Gap Scanner

Just as we detect common gaps with our eyes, we can code a recursive algorithm that does the same thing for us. Let us consider that we will work on Hourly data since 2010. What variables can we use to populate the scanner function? I would say the width variable is the most important one.

  • The width: This is your threshold where you would say for certain that there is a gap. On hourly FX data, we cannot really say that 1 or 2 pips are worthy of a gap. Therefore, we can consider that at least a 5 pips gap is worthy of being traded. Taking into account a 0.2 spread, an example of maximum profit on EURUSD would be 5–0.2 = 4.8 pips which is $4.8 on a mini lot account with 1:100 leverage. I say maximum profit because our target on the gap trade is to fill it. This means that if the gap is 5 pips wide, and we trade directly based on it, we can expect 4.8 pips gain after accounting for the spread.

Let us take a look at the scanner function below:

def gap_scanner(Data, closing_price, opening_price, gap_width, buy, sell):

for i in range(len(Data)):

if Data[i, opening_price] < Data[i - 1, closing_price] and abs(Data[i, opening_price] - Data[i - 1, closing_price]) >= gap_width:
Data[i, buy] = 1

if Data[i, opening] > Data[i - 1, closing_price] and abs(Data[i, opening] - Data[i - 1, closing_price]) >= gap_width:
Data[i, sell] = -1

The signal function takes into account 4 variables:

  • The Data variable is the OHLC time series in the form of an array, preferably a numpy array.
  • The closing_price variable is the column that holds the closing price (The C in OHLC).
  • The opening_price variable is the column that holds the opening price (The O in OHLC).
  • The gap_width is the distance between the closing price and the new opening price that has just opened. This measures the minimum distance for the gap to be considered worthy for a trade.
  • The buy and sell variables are the columns where the buy and sell orders are put. A value of 1 refers to a buying trigger while a value of -1 refers to a selling trigger.
Signal chart on the EURUSD hourly data following the signals by the Gap scanner. (Image by Author)

Back-testing the Gap Strategy

It is time to back-test the strategy for informational purposes and to see where does pure simplistic gap trading stands with regards to profitability. The trading rules are therefore:

  • Go long (Buy) whenever the scanner identifies a bullish gap configuration. A bullish gap is of course a gap down looking to be filled upwards.
  • Go short (Sell) whenever the scanner identifies a bearish gap configuration. A bearish gap is of course a gap up looking to be filled downwards.
  • The trade is opened and closed either at fill or stopped at a 50 pips loss. I believe that a 5 to 50 reward-risk ratio is extremely suboptimal, but it is to see whether the gaps are filled or not.

Let us take a look at the results:

Performance Summary Table. (Image by Author)
Equity curves following the gap strategy. (Image by Author)

Note that the results are only of the past and should not really be used as an expected return in the future. In finance, returns are never guaranteed. You have to make your own back-tests that fit your profile, do not forget that we have put a very low risk-reward ratio which explains the high hit ratio. In reality, you must have better risk management than the above.

If you are also interested by more technical indicators and using Python to create strategies, then my latest book may interest you:


Why was this article written? It is certainly not a spoon-feeding method or the way to a profitable strategy. If you follow my articles, you will notice that I place more emphasize on how to do it instead of here it is and that I also provide functions not full replicable code. In the financial industry, you should combine the pieces yourself from other exogenous information and data, only then, will you master the art of research and trading.

I always advise you to do the proper back-tests and understand any risks relating to trading. For example, the above results are not very indicative as the spread we have used is very competitive and may be considered hard to constantly obtain in the retail trading world (but not impossible). However, with institutional bid/ask spreads, it may be possible to lower the costs such as that a systematic medium-frequency strategy starts being very profitable.

The Startup

Get smarter at building your thing. Join The Startup’s +789K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Sofien Kaabar

Written by

Institutional FOREX Strategist & Trader. Author of “The Book of Back-tests”

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +789K followers.

Sofien Kaabar

Written by

Institutional FOREX Strategist & Trader. Author of “The Book of Back-tests”

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +789K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store