Detection of Price Support and Resistance Levels in Python
Detection of Price Support and Resistance Levels in Python
towardsdatascience.com/detection-of-price-support-and-resistance-levels-in-python-baedc44c34c9
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.
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.
Support and resistance example
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.
Levels or areas?
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.
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.
Fractals
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.
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 import some useful libraries and initialize the plotting environment.
import pandas as pd
import numpy as np
import yfinance
from mpl_finance import candlestick_ohlc
import matplotlib.dates as mpl_dates
import matplotlib.pyplot as pltplt.rcParams['figure.figsize'] = [12, 7]
plt.rc('font', size=14)
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()
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))
Conclusions
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).