Finding levels in an array of floats Before anybody asks please let me confirm this question is related to trading technical analysis.

The problem is quite simple, but I lack the scientific background required to solve it.

Data comes as an array of float values equally spaced temporally. Some local minimums (same apply for maximums) tend to repeat over time (although not at the very same value, but in a rather tight range), because of human psychology: if some price level was important in the past, stopping the price to go below, it will tend to resurface a few times, blocking again the price from going below.

What I'm interested in is to find the most significant levels over a given past period (say 5000 time intervals). Basically the "importance" of a level is determined by the number of times it was tested (meaning the number of times the price tried go past that level and failed); however, a level tested three times at relatively large time intervals (like once each month for a daily chart) is more significant than a level tested three times in a short period (like within a week for a daily chart).

Again, data is as follows:


data comes as an array of float values
data points are equally spaced temporally (however, time is of little to no importance)
some local extremes are more important than others and tend to repeat over time


Can somebody help me with some Python code to extract important levels? Or at least point me to some theory/frameworks to learn? I know about pandas/numpy but I lack the math background required to a least identify what could be used of these...

Thank you!
You could define a window (distance between red and blue line) and slide it from top to bottom over the y-axis and then count the local minima and maxima in that slice. One way with scipy is argrelextrema(x, np.less) for minima and argrelextrema(x, np.greater) for maxima which gives you the indices of the extrema. With len(argrelextrema(x, np.less)) you would get the number of minima.

Each slice would be a level and the results of counting the minima and maxima the corresponding importance. The size of the window is up to you. If your data is very granular you might want to consider smoothing it first (e.g. with numpy.convolve()