Detection of price support and resistance levels in Python | by Gianluca Malato | Towards Data Science

Algorithmic trading is a fascinating field of trading and statistics and one of the most useful trading techniques that quantitative traders often would like to automate is price action, which is the analysis of price movements without using derivated indicators such as oscillators or moving average.
In this article, I’ll cover an algorithm to automatically detect two important tools of price action, which are supports and resistances.
Supports and resistances are often called “key levels”. They are price levels at which the stock price has inverted its trend. If the price rises and then inverts its trend moving down, the highest point it has reached is called resistance. If the price has gone down and then starts rising, the lowest price value is called support.
These price levels identify supply and demand zones, at which traders have increased their operation volume and have shown some interest. That’s why, as soon as the price gets closer to a key level, traders must keep their eyes open and see what happens.
Key levels are very important because many interesting events may occur near these inversion levels. For example, the market can bounce again allowing mean-reversion strategies to win or it can break the key level, making things better for breakout traders.
There’s a rule of thumb that says that the more times a key level has been tested (i.e. the market has bounced near it many times), the higher the importance of the level.
Another rule of thumb is that, once a resistance level is broken, it automatically becomes a support level. Viceversa, a broken support level becomes a resistance level.
In this article, I’ll refer to key levels as price levels, but there are some traders saying that key levels are areas, not levels. That happens because volatility makes everything noisier, so supply and demand zones are never that clear. That’s why, in real life trading, you should always consider some kind of envelope around a key level, roughly as wide as volatility (e.g. Average True Range, Standard Deviation). For the seek of simplicity, in this article I’ll consider key levels as fixed price levels and not wider zones.
Key levels are rejection points, so we must check if the market has reached a certain level and then has rejected moving in the opposite direction.
A good idea is to use the candlestick chart and check the high and low prices of every candle. If a candle low is lower than the previous and the next candle’s low, that’s a support. This particular price action pattern is called swing. Unfortunately, this pattern often shows some flaws due do market volatility and noise, that’s why we can use a better pattern called fractal.
A fractal is a candlestick pattern made by 5 candles. The third candle has the lowest low price, the previous candles have decreasing lows and the next candles have increasing lows. By this pattern, the low of the third candle is the support level. The same concept applies to resistance levels, where the third candle has the highest high of the five ones.
Fractals are very useful because they remove some of the noise shown by the swings and identify key levels with higher accuracy. That’s why I’m going to use them in my algorithm.
Let’s see an example in Python using S&P 500 data. My notebook can be found on GitHub here: https://github.com/gianlucamalato/machinelearning/blob/master/Support_and_resistance.ipynb
For all the candlestick charts code I’ve used some of the code found here: https://saralgyaan.com/posts/python-candlestick-chart-matplotlib-tutorial-chapter-11/
Let’s first install yfinance and mpl_finance libraries.
!pip install yfinance!pip install mpl_finance
Let’s import some useful libraries and initialize the plotting environment.
import pandas as pdimport numpy as npimport yfinancefrom mpl_finance import candlestick_ohlcimport matplotlib.dates as mpl_datesimport matplotlib.pyplot as pltplt.rcParams['figure.figsize'] = [12, 7]plt.rc('font', size=14)
Now we can download S&P 500 daily data.
name = 'SPY'ticker = yfinance.Ticker(name)df = ticker.history(interval="1d",start="2020-03-15", end="2020-07-15")df['Date'] = pd.to_datetime(df.index)df['Date'] = df['Date'].apply(mpl_dates.date2num)df = df.loc[:,['Date', 'Open', 'High', 'Low', 'Close']]
Let’s not create two functions that identify the 4-candles fractals.
def isSupport(df,i):  support = df['Low'][i] < df['Low'][i-1]  and df['Low'][i] < df['Low'][i+1] and df['Low'][i+1] < df['Low'][i+2] and df['Low'][i-1] < df['Low'][i-2]  return supportdef isResistance(df,i):  resistance = df['High'][i] > df['High'][i-1]  and df['High'][i] > df['High'][i+1] and df['High'][i+1] > df['High'][i+2] and df['High'][i-1] > df['High'][i-2]  return resistance
Finally, let’s create a list that will contain the levels we find. Each level is a tuple whose first element is the index of the signal candle and the second element is the price value.
levels = []for i in range(2,df.shape[0]-2):  if isSupport(df,i):    levels.append((i,df['Low'][i]))  elif isResistance(df,i):    levels.append((i,df['High'][i]))
We can now define a function that plots price and key levels together.
def plot_all():  fig, ax = plt.subplots()  candlestick_ohlc(ax,df.values,width=0.6, \                   colorup='green', colordown='red', alpha=0.8)  date_format = mpl_dates.DateFormatter('%d %b %Y')  ax.xaxis.set_major_formatter(date_format)  fig.autofmt_xdate()  fig.tight_layout()  for level in levels:    plt.hlines(level[1],xmin=df['Date'][level[0]],\               xmax=max(df['Date']),colors='blue')  fig.show()
Finally, we can plot the result.
As you can see, we have been able to detect the major rejection levels, but there’s still some noise. Some levels are over others, but they are essentially the same level.
We can clean this noise modifying the function that detects key levels. If a level is near another one, it will be discarded. We must decide what “near” means, then. We can say that a level is near another one if their distance is less than the average candle size in our chart (i.e. the average difference between high and low prices in a candle). This will give us a rough estimate of volatility.
s =  np.mean(df['High'] - df['Low'])
Let’s define a function that, given a price value, returns False if it is near some previously discovered key level.
def isFarFromLevel(l):   return np.sum([abs(l-x) < s  for x in levels]) == 0
Now we can scan the price history looking for key levels using this function as a filter.
levels = []for i in range(2,df.shape[0]-2):  if isSupport(df,i):    l = df['Low'][i]    if isFarFromLevel(l):      levels.append((i,l))  elif isResistance(df,i):    l = df['High'][i]    if isFarFromLevel(l):      levels.append((i,l))
Finally, we can plot everything again.
Now the levels are clearer, they do not overlay with each other and we can easily see that sometimes price jumps upon each level more than once.
Automating price levels can be very useful for a quantitative trader and can remove some of the market noise making the chart clearer. Key levels can be used for mean reversion strategies (i.e. buy when the price bounces away from a support level) or for breakout strategies (i.e. buy when price breaks a resistance level).
Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.