10

I've been performing some research, in order to find the best approach to identify break points (trend direction change) in a dataset (with pairs of x/y coordinates), that allow me to identify the trend lines behind my data collections.

However I had no luck, finding anything that brings me some light.

The yellow dots in the following image, represent the breakpoints I need to detect.

Any suggestion about an article, algorithm, or implementation example (typescript prefered) would be very helpful and appreciated.

breakpoints-and-trend-lines

Emma
  • 27,428
  • 11
  • 44
  • 69
colxi
  • 7,640
  • 2
  • 45
  • 43
  • 1
    You may be able to formulate this as a polynomial approximation problem (find curve to "fit" your data) https://stackoverflow.com/questions/28269021/how-do-i-create-a-best-fit-polynomial-curve-in-javascript. Then you can find local minima and maximas of that curve to determine the breakpoints. – cisco Jun 10 '20 at 07:08
  • 1
    there are many ways to do it like sign of sliding average derivation change, angle between last breakpoint and 2 consequent poitns change thresholding, intersection of regresed lines, ... – Spektre Jun 11 '20 at 05:03
  • It will always be a lagging indicator. Prices will need to move past the breakpoint in order to identify it as a breakpoint. If you want to implement a leading one, you will have to optimize it and deal with probabilities. – Youssef AbouEgla Jun 16 '20 at 19:39
  • Just search leading and lagging indicators. you'll find plenty – Youssef AbouEgla Jun 16 '20 at 19:39

3 Answers3

4

Usually, people tend to filter the data by looking only maximums (support) or only minimums (resistance). A trend line could be the average of those. The breakpoints are when the data crosses the trend, but this gives a lot of false breakpoints. Because images are better than words, you can look at page 2 of http://www.meacse.org/ijcar/archives/128.pdf.

There are a lot of scripts available look for "ZigZag" in

https://www.tradingview.com/

e.g. https://www.tradingview.com/script/lj8djt1n-ZigZag/ https://www.tradingview.com/script/prH14cfo-Trend-Direction-Helper-ZigZag-and-S-R-and-HH-LL-labels/

Also you can find an interesting blog post here (but code in in python):

https://towardsdatascience.com/programmatic-identification-of-support-resistance-trend-lines-with-python-d797a4a90530

with code available: https://pypi.org/project/trendln/

One Lyner
  • 1,964
  • 1
  • 6
  • 8
0

If you can identify trend lines then can't you just identify a breakpoint as when the slope changes? If you can't identify trend lines, then can you for example, take a 5-day moving average and see when that changes slope?

Colin Steidtmann
  • 417
  • 1
  • 4
  • 10
  • Trend lines are a consequence of the trend breakpoints. As soon as you have the breakpoints. The hard part here is to find the breakpoints. Also, your suggestion of taking the n-days based approach does not provide the "trend change event" detection I'm looking for (eg: a trend can last 2 ,7, 22 days...) – colxi Jun 16 '20 at 01:37
  • Maybe you should define the minimum number of days a trend can be in then once you get two defined trends with different directions, then you got a breakpoint at the location where one trend ended and the other begun. – Colin Steidtmann Jun 16 '20 at 01:46
0

This might sound strange, or even controversial, but -- there are no "breakpoints". Even looking at your image, the fourth breakpoint might as well be on the local maximum immediately before its current position. So, different people might call "breakpoints" different points on the same graph (and, indeed, they do).

What you have in the numbers are several possible moving averages (calculated on a varying interval, so you might consider MA5 for five-day average, or MA7 for a week average) and their first and maybe second derivatives (if you feel fancy you can experiment with third derivatives). If you plot all these lines, suitably smoothed, over your data, you will notice that the salient points of some of them will roughly intersect near the "breakpoints". Those are the parameters that your brain considers when you "see" the breakpoints; it is why you see there the breakpoints, and not somewhere else.

Another method that the human vision employs to recognize features is trimming outliers: you discard in the above calculations either any value outside a given tolerance, or a fixed percentage of all values starting from those farther from the average. You can also experiment not trimming those values that are outliers for longer-period moving averages but are not for shorter periods (this gives better responsivity but will find more "breakpoints"). Then you run the same calculations on the remaining data.

Finally you can attribute a "breakpoint score" based on weighing the distance from nearby salient points. Then, choose a desired breakpoint distance and call "breakpoint" the highest scoring point in that interval; repeat for all subsequent breakpoints. Again, you may want to experiment with different intervals. This allows for a conveniently paced breakpoint set.

And, finally, you will probably notice that different kinds of signal sources have different "best" breakpoint parameters, so there is no one "One-Size-Fits-All" parameter set.

If you're building an interface to display data, leaving the control of these parameters to the user might be a good idea.

LSerni
  • 55,617
  • 10
  • 65
  • 107