Well since this article is named Algorithmically drawing Trend Lines on a Stock Chart, you might already have guessed the answer ;- Now whether these lines are useful or not is a whole different topic.
On the other hand, people are using these lines so there should also be some kind of self-fulfilling prophecy component with them.
However, the secret ingredient is called Hough Transform. The idea of the Hough transformation is, that every edge point in the edge map is transformed into all possible lines that could pass through that point.
The areas where most Hough space lines intersect is interpreted as true lines. It is not that complicated as it sounds. You need to know that Hough transformation is mostly used in image processing i.
Therefore you need to pre-process images using an edge detector. Well, of course, we use a discrete amount of lines where the number is somewhat arbitrary.
You can obviously find lots of tutorials and explanations of the Hough transformation on the internet if you want to dive deeper. Or you just take a look at this guy who tries to explain it for us.
Implement a Hough Transform for a stock price chart The fully functional code and a Jupyter notebook can be found as part of a bigger library I am working on in my GitHub repository . And just for visibility we only look at the most recent days. Use the standard approach and rescale all prices to fall between 0 and 1 and round these numbers just to remove a bit of noise.
But since we have stock prices and not an image we use a slightly different approach. For the x-axis, we introduce a linear space from 0 to 1 and for the y-axis, we use the rescaled prices.
For our use case of finding trend lines we need to detect turning points. There we calculate the mean and check if the first and the latest data points are both above or below the mean.