Detection of price support
Last updated
Last updated
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.
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.
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.
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.
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.
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.
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/
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
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
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
Finally, we can plot the result.
Finally, we can plot everything again.