问题
Looking to identify (flag True) when current values in col1 drop below the most recent local maximum in col1 achieved by a set percent such that there will be multiple such signals as maxima are achieved and current values drop by the set percent (i.e. resets automatically without a set threshold other than the percent). Note that flag True should occur only on downside, not upside.
percent = 0.7
df = pd.DataFrame({'col0':[1,2,3,4,5,6,7,8,9]
,'col1':[5,4.9,5.5,3.5,3.1,4.5,5.5,1.2,5.8]
,'col2':[3.5, 3.43, 3.85, 2.45, 2.17, 3.15, 3.85, 0.84, 4.06]
,'col3':[np.nan, 3.43, 3.85, 3.85, 3.85, 3.85, 3.85, 3.85, 4.06]
})
df['col2'] = df['col1'] * percent
df['col3'] = df['col2'].shift(-1).cummax().shift()
Current form of col3 generates cummax but desired result will find local maxima to the downside and col4 to flag True every time col1 breaches col3 to the downside. Here's one example of the resulting col3 and col4:
col0 col1 col2 col3 col4
0 1 5.0 3.50 NaN False
1 2 4.9 3.43 3.43 False
2 3 5.5 3.85 3.85 False
3 4 3.5 2.45 3.85 True
4 5 3.1 2.17 3.85 False
5 6 4.5 3.15 3.85 False
6 7 5.5 3.85 3.85 False
7 8 1.2 0.84 3.85 True
8 9 5.8 2.90 4.06 False
来源:https://stackoverflow.com/questions/59587218/local-maximum-identification-within-a-set-trigger-true-on-the-downside