# Algorithm to detect down-up-down pattern in time series

I'm trying to write an online algorithm in Python to detect this below Down-Up-Down pattern in time-series.

It's not hard to do roughly if I calculate 3 contiguous non-overlapping moving averages, and just check that the first and last are both < -X, and the middle one is > 1.5*X.

But this has many downsides. For example, the time frames need to be strictly hardcoded. And, it ignores intrawindow variation that may obviously invalidate the overall pattern if a human were to eyeball it as a sense check.

What can I do? Wavelets aren't ideal because of edge effects .. I need an online algorithm that computes in real-time. Very first thought here:

• Aggressive Low Pass (to get the trend, would be a pretty low cut-off you can experiment with).
• Local maxima and minima detection (you can use the 1st derivative's 0-crossings for example). You can set a threshold for the appropriate distance between two local extrema (maxima/minima) below which you consider two extrema to be one and the same.

These maxima and minima will give you the time points at which the trend starts going up, and starts going down.

Can you narrow down how the plot should generalize?

Ie how much variation do you expect for the time between edges and absolute step height? What might noise (variable stuff that should lead to no detection) look like?

My hunch would be something like:

1. Subtract the mean
2. Threshold/quantize the vector to -A, 0 and +A
3. Find the zero crossings.
4. If 3 consequtive zero crossings are all spaced more that «lo_thr» samples and less than «hi_thr» samples, you have a detection

Tuning A, lo_thr and hi_thr would be done for a suitable set of test data

Step 1: Low Pass Filter

Step 2: 2nd derivative. The 2nd derivative gives you the curvature.

Step 3: If it switches sign three times, check the values at those points to confirm they're actual peaks.

Some form of that should get you what you want.