Detect Technical Breakouts Using Python

Joe Ng
DataDrivenInvestor

--

The detection of technical chart patterns automatically is an interesting topic especially for traders and investors. Price action trading typically has a higher win rate than technical-indicator related strategies such as MACD, RSI, Bollinger Bands and Ichimoku Cloud. Nevertheless, algorithmically detecting supports and resistances may be a difficult as there are no known libraries or one-size-fit-all techniques to do so. In this article, I am going to share an approach to detect technical chart patterns using a simple algorithm in Python.

First of all, let’s import all the required libraries :

We need the data, which includes the Open, High, Low, Close(OHLC) price of an asset. AlphaVantage is a free platform to obtain this data, though many other market data vendors such as Alpaca and marketstack also provide market data for free, with, of course, limitations to the number and frequency of API calls.

In the code above, price_high, prices_low and prices_close contains the daily high, low and close price of the selected asset. The datetime module is utilized to generate the dates to access the data contained in the API response. If the date generated does not correspond to any data( for example, on weekends and public holidays in which there is no trading) it will just skip and go on to retrieve the data for the next day.

https://www.forextrading200.com/

In trending markets, local lows and highs form reference points for supports and resistances. Similarly in code, we are able to identify the local minimum and maximum points using argrelextrema from the SciPy library.

Now, the most important part of this approach is to plot the trendlines and build a screener to identify the assets which has completed a breakout. The main idea of this is expressed in the code below:

By looping through all local max points, np.polyfit is used to fit a straight line with the equation y=mx+c through every 3 consecutive local max points. r is the residue of the straight line, which reflects how closely the original points are fitted to the line. Lines with gradients that are too large or too small as well as lines which are not fitted very well are screened out.

As shown in the graph above, A point (a,b) is above the straight line y=mx+c if and only if b is larger than ma+c. Therefore, a breakout is said to have happened it the closing price is above the trendline, which is when prices_close[k]>(k*m+c).

One of the breakouts detected by the algorithm

Further Developments

With appropriate risk/reward ratio and stop loss, a simple yet valid strategy may be developed through this algorithm to make automated trades of financial assets. There are still lots of effort to be put in to determine the most suitable ranges for the gradient and the residue, which varies among different assets and derivatives. The Machine Learning approach is an interesting way to solve this problem. Besides, the detection of more specific chart patterns, for instance, the ‘Cup and Handle’ pattern is also a future direction of my work.

If you liked this article, please give some claps or share it to those who might be interested in it so that more people can see it. Thank you!

--

--