The Gap Fill Trading Strategy.
Creating a Strategy Based on Filling the Gaps.
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”. From that we can evaluate the fill ratio of the gap.
I have just released a new book after the success of my previous one “The Book of Trading Strategies”. It features advanced trend-following indicators and strategies with a GitHub page dedicated to the continuously updated code. Also, this book features the original colors after having optimized for printing costs. If you feel that this interests you, feel free to visit the below Amazon link, or if you prefer to buy the PDF version, you could contact me on LinkedIn.
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.
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.
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.
Creating the Strategy
Just as we detect common gaps with our eyes, we can code a recursive algorithm that does the same thing for us. 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:
width = 0.0005 # 5 pips Gap example for Hourly OHLC data
def signal(Data, opening, close, width, buy, sell): for i in range(len(Data)): if Data[i, opening] < Data[i - 1, close] and abs(Data[i, opening] - Data[i - 1, close]) >= width: Data[i, buy] = 1 if Data[i, opening] > Data[i - 1, close] and abs(Data[i, opening] - Data[i - 1, close]) >= width: Data[i, sell] = -1 return Data
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 variable is the column that holds the closing price (The C in OHLC).
The opening variable is the column that holds the opening price (The O in OHLC).
The 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.
It is time to back-test the strategy for informational purposes and to see where does pure gap trading stands with regards to predictability. The trading rules are therefore:
A long (Buy) signal is generated whenever the scanner identifies a bullish gap configuration. A bullish gap is a gap down looking to be filled upwards when the market’s high reaches the low of the candle before the gap. Therefore, the signal is triggered at the open price.
Go short (Sell) whenever the scanner identifies a bearish gap configuration. A bearish gap is a gap up looking to be filled downwards when the market’s low reaches the high of the candle before the gap. Therefore, the signal is triggered at the open price.
The trade is opened and closed either at fill or stopped at the next signal.
The above illustration shows an example of a gap that needs to be filled through a short sell trade, therefore, we will sell at the opening above the previous closing price and target its high.
The above illustration shows an example of a gap that needs to be filled through a long trade, therefore, we will buy at the opening below the previous closing price and target its low.
def quality(Data, buy, sell, where): for i in range(len(Data)): try: if Data[i, buy] == 1: for a in range(i, len(Data)): if Data[a, 1] >= Data[i - 1, 2]: Data[a, where] = Data[i - 1, 2] - Data[i, 0] break elif Data[a, sell] == -1: Data[a, where] = Data[a, 0] - Data[i, 0] break elif Data[i, sell] == -1: for a in range(i, len(Data)): if Data[a, 2] <= Data[i - 1, 1]: Data[a, where] = Data[i, 0] - Data[i - 1, 1] break elif Data[a, buy] == 1: Data[a, where] = Data[i, 0] - Data[a, 0] break except IndexError: pass return Data
my_data = quality(my_data, 6, 7, 8)
total_net_profits = my_data[my_data[:, 8] > 0, 8] total_net_losses = my_data[my_data[:, 8] < 0, 8] total_net_losses = abs(total_net_losses)
fill_ratio = len(total_net_profits) / (len(total_net_losses) + len(total_net_profits)) fill_ratio = round(fill_ratio, 2) * 100
The fill ratio measures the percentage of gaps that have been filled before the market encounters another gap. The rationale is very simple:
If we encounter a down gap which gives us a bullish signal, we will wait until it is filled or another gap is encountered which in case we consider the first gap unfilled.
If we encounter an up gap which gives us a bearish signal, we will wait until it is filled or another gap is encountered which in case we consider the first gap unfilled.
The below are a sample of the results on hourly data:
# EURUSD Fill Ratio: 78.00% # EURGBP Fill Ratio: 81.00%
# The fill ratio is saying that whenever we encounter any type of gap on the EURUSD and EURGBP, we have a probability of 78% and 81% of seeing it filled.
If you are also interested by more technical indicators and strategies, then my book might interest you:
Remember to always do your back-tests. You should always believe that other people are wrong. My indicators and style of trading may work for me but maybe not for you.
I am a firm believer of not spoon-feeding. I have learnt by doing and not by copying. You should get the idea, the function, the intuition, the conditions of the strategy, and then elaborate (an even better) one yourself so that you back-test and improve it before deciding to take it live or to eliminate it. My choice of not providing specific Back-testing results should lead the reader to explore more herself the strategy and work on it more.
To sum up, are the strategies I provide realistic? Yes, but only by optimizing the environment (robust algorithm, low costs, honest broker, proper risk management, and order management). Are the strategies provided only for the sole use of trading? No, it is to stimulate brainstorming and getting more trading ideas as we are all sick of hearing about an oversold RSI as a reason to go short or a resistance being surpassed as a reason to go long. I am trying to introduce a new field called Objective Technical Analysis where we use hard data to judge our techniques rather than rely on outdated classical methods.