# Finding levels in an array of floats

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!

• So you're trying to find which local maxima and minima occur most frequently?
– user4710
Sep 20, 2016 at 16:38
• Basically yes, keeping in mind they may be grouped in a range instead of touching the exact same value. Sep 21, 2016 at 14:20
• Can you plot your values, to get an idea of the how your data typically looks like? Sep 22, 2016 at 5:53
• Hi @ascenator, here's a graph onto which I marked in red an "important" level, as it was tested multiple times (aka the price tried to go higher but the level "resisted"): dwq4do82y8xi7.cloudfront.net/x/ht49yWn8 Sep 26, 2016 at 12:30

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()