As we’ve seen in a trend line explanation, recognizing a trend and rallying patterns in a band is vital for technical traders. Support and resistence lines play a key role to understand a band that is created by higher prices and lower prices. If we conform to the principles,When the price hits the ceiling or the floor in the channel, behaivors of the price follow either bouncing back away from the support or resistance level, or violating the price level and continue in its direction—until it hits the next support or resistance level if it confirms to the principle.
Here’s what we drew in the previous article a trend line for both higher data points and lower data points those work as resistence line and support line.
In this article, I’d like to draw a static barrier that is a single straight horizontal line in a chart that represent support and resistence lines in Python. This is not either ascending or descending line following a trend like I introduced in the previous writing. These support and resistence lines are more simplified ones and aren’t influenced by upward and downward trend but it’s useful yet to predict a probable range of prices in a certain period of time.
There might be some approaches to compute support and resistence points from DataFrame data, I’ve just found this code and the approach leveraged scipy.signal.savgol_filter method. This algorithm looks fine to me.
I use this function and drew support and resistence lines from DataFrame data in the following section. Firstly, here’s my understanding how this code works about the computation.
- Apply savgol_filter with a specific window, it computes smoothed data
- A substraction of matrices computes delta of adjacent smoothed data
- A loop detects both local maxima value and local minima value
Now we’ve got variables support and resistence that are Python lists of values.
We’re ready to draw a graph with DataFrame and returned support & resistence values in matplotlib. I wrapped these tasks in a function and it takes df which is given DataFrame in OHLCV format and lists of support and resistence values in float.
supres_plot(df, support, resistence)
This is the result of Apple, Inc stock data. This price data seems unfit a little for the given Apple, Inc stock prices. We can try different data set and different time intervals.
I published the previous article how to aggregate OHLCV data in different time interval.
This can be a reference if you want to modify the time interval of your OHLCV data, for example between daily, weekly, monthly, or for intra-day data.
Here’s the Jupyter notebook for all samples above for your reference.