I have been using scipy.signal.argrelextrema but I'm not getting the desired results. My goal is to: identify the peaks/valleys in a 1D timeseries that are a minimum of 5% away from the prior peak/valley.
The following snippet computes the extreme peak/valleys with order of 15:
df = pd.DataFrame(1000 + np.cumsum(0.1 + np.random.randint(-10, 10, 1000)), columns=['close'])
min_peaks = argrelextrema(df["close"].values, np.less, order=15)[0]
max_peaks = argrelextrema(df["close"].values, np.greater, order=15)[0]
However, this is not achieving my goals because: I want to identify the soonest peak/valley that is a minimum of 5% away from the prior peak/valley.
Whereas argrelextrema imposes a horizontal window (order) parameter - I don't have any constraint of horizontal distance. I only care that the peaks/valleys identified are a min of 5% away from the prior peak/valley.
UPDATE: An example for illustration:
- A peak is identified at 1000
- The next valley that can be identified must be >1050 or < 950.
- It is ok for two valleys (or peaks) to be identified in succession (same as with
argrelextrema). You can see examples in the attached chart.
TY for any pointers/guidance in advance. LMK if/where I can add more detail in my question.
