0% found this document useful (0 votes)
195 views

Gap Trading. An Introduction & Back-Test in Python

Gap Trading. An Introduction & Back-test in Python

Uploaded by

FabioSantos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
195 views

Gap Trading. An Introduction & Back-Test in Python

Gap Trading. An Introduction & Back-test in Python

Uploaded by

FabioSantos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 15
You have 1 free member-only story left this month. Sign up for Medium and get an extra one Gap Trading. An Introduction & Back-test in Python. Introducing Gaps and How to Identify and Trade Them. Ay seofenkaabar ee 29,2000: minted saz s by Stan Kaatr [ha Sip O66, 2028| Maes — — Gaps form an important part of price action. They vary in rareness from market to market. For instance, in the currencies market, they usually swanaeet cag An tn 8 an yt ty a 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 itis 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: “Technical Pattern Recognition for Trading in Python. Back-testing some Exotic Technical Patterns and evaluating t Performance, ‘omarssdstacconeszom Introduction to Gaps MoxtnedinanivahgisrireniniaicontvcintaitenTi ap Tag An tthe Banat yt y Steet Th Sing |, 209 | A gap isa 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, cep Down ae Gap Up Fed “Two filed gaps onthe GBPUSD 34 data, mage by Author) Moxinedinamioahgiratepaninia.cton incinerator TH ws MoxtnedinanivahgisrireniniaicontvcintaitenTi ip Taha An rn & cnt ny |b te IMesintip be 209 | earn 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 S 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: + Acommon gap: It generally occurs in a sideways markets. Its 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. + Arunaway gap: It generally occurs within the trend but it confirms it more, therefore, it is a continuation pattern. cp arg tin 8 aes ey Stan atari 228M + 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. ‘noone ap Tag An ste Bat nan ty Sten kata | Stig De, 200 Ma aera TR aE SO EA ST {Breakaway gp onthe GEPUSD 3H data ater surpassing resistance, (mage 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. ror TU wo smooae ap Yak ste Ban yan |b Sten kata Stip |e, 200 Mar I wi Exhaustion Gop ‘Sppot Area ABCD Pate ha Exhaustion gap tthe end ofa downtrend and close ta Harmonic suppart. mage by Author) To learn more about Harmonic Patterns and how to find key market reactions, you can read more about them here: Advanced Price Action Techniques to Detect Market Reactions Presenting Advanced Technical Analysis Techniques to Trade the Markets, ‘moot ip Taha An rn & cnt ny |b te 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 ona 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: MoxtnedinanivahgisrireniniaicontvcintaitenTi ‘moot ip Taha An rn & cnt ny |b te scanner (Data, closing price, opening price, gap width, buy, for i in range (en (Data) ): £ Datali, opening price] < pata[i - 1, closing price] and abs (Datali, opening price) ~ Datali ~ 1, closing pricel) >= gap_width Batali, buy] = 1 £ Data[i, opening) > Datali - 1, closing price) and abs (Datali, opening] = Datali ~ 1, closing price]) >= gap_wicth: ‘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. MoxtnedinanivahgisrireniniaicontvcintaitenTi soca cp arg tin 8 aes ey Stan atari 228M + 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. ‘Signa ena’ on tre EURUSD hourly data following the signals by the Gap scanner mage by Author) Moxinedinamioahgiratepaninia.cton incinerator TH ‘moot ip Taha An rn & cnt ny |b te Back-testing the Gap Strategy Itis time to backtest 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: [ao [rte | Priactor | Theoeieal Rak Reward Realized Risk Reward ‘Brealseven Hit Ratio Number of Trades oxinedim nbagieratepaninia.ctmtnantoroytor TSB ‘moot tp Ya. An atc & BaP by Sten Kabat he Ship Do, 209| Mary fEUROSO| —__ssootif- «Bae oad oa oa oi 395 USDCHF| pastel 383 os 5 015 ‘6.05% 407 cures earl TBE on 035 215 86.96% as iAUDUSD] —__o6zfe)-__—Bave 0.30] 05 035] 86.96% 338] NaUsD} Sasa TBE 0.3 09 oul Brr20H an USDCAD Teast Bone 8 oa on 877308 338] -BURCAD al 4008 or 05 oul B72 st FURGEP| awe 20 023 oul B72 355 EURCHE| oa3e 085 = ons 36.1% 30| -AUDCAD| 3876 os4 023 0.6 86.21% 500] Performance Summary Table. (Image by Author) Gap Strategy 1 ‘moot ip Tag An ren & cnt ny by tn 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. Conclusion 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. MoxtnedinanivahgisrireniniaicontvcintaitenTi tg nti A acs yt ty ten Lalways 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. DataScience MachneLesing Artiicilintligence Trading Finance Make Mediu 1S. are your think

You might also like