Local maximum identification within a set % (trigger True on the downside)

回眸只為那壹抹淺笑 提交于 2020-01-05 07:11:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!