3

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.

enter image description here

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.

Mark
  • 53
  • 4
  • What do you mean by 5% of the previous peak/valley? For instance, if a peak is at 1000, you want to detect the next peak that is greater than 1050 or lower than 950, is it correct? – Nicolas Martin Aug 22 '22 at 14:18
  • Correct @NicolasMartin - I added some extra detail to the question. I'd also add that is is ok for there to be two valleys (or peaks) identified in succession. Thanks for your question. – Mark Aug 22 '22 at 21:47

1 Answers1

0

The following zigzag python library provides this ability.

https://github.com/jbn/ZigZag/

Mark
  • 53
  • 4