Detection of price support
Detection of price support and resistance levels in Python
An algorithm to find price support and resistance levels in Python
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.
What 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.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.Resistance becomes support
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.
How to identify key levels
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.
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.Support identification using fractals
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.
Automatic detection in Python
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.
Let’s import some useful libraries and initialize the plotting environment.
Now we can download S&P 500 daily data.
Let’s not create two functions that identify the 4-candles fractals.
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.
We can now define a function that plots price and key levels together.
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.
Let’s define a function that, given a price value, returns False if it is near some previously discovered key level.
Now we can scan the price history looking for key levels using this function as a filter.
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.
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).
Reference : https://towardsdatascience.com/
Last updated