微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在数据中查找最大值和最小值并将真/假附加到相应的行 - Python

如何解决在数据中查找最大值和最小值并将真/假附加到相应的行 - Python

我想在股市数据中找到摆动高点和摆动低点。我希望能够将每个点附加到一列。例如)有一个 isHigh 和 isLow 列与收盘价列对齐。所以每天继续,如果价格不是摆动高点或低点,它会在 isHigh/isLow 列中返回 False。如果是摆动高点或低点,则返回 True。

我已经能够在股市数据中找到最大值/最小值或转折点,但它返回的只是转折点的数量或每个点的数量

我无法提取参考价​​格的实际点数。

我使用过 numpy 和 scipy argrelextrema。

```  
import matplotlib
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
 
#Generate random data.
data_x = df['close']
data_y = df['close']
 
#Find peaks(max).
peak_indexes = signal.argrelextrema(data_y.to_numpy(),np.greater)
peak_indexes = peak_indexes[0]
 
#Find valleys(min).
valley_indexes = signal.argrelextrema(data_y.to_numpy(),np.less)
valley_indexes = valley_indexes[0]

**peak_indexes and valley_indexes only returns a numbered list of the points in numerical order.**
---

我已经尝试过了。

close = df['Adj Close']

def turningpoints(close):
    dx = np.diff(close)
    return np.sum(dx[1:] * dx[:-1] < 0)```

这将返回一个数字 646,这是峰值和谷值的总数

感谢任何帮助,谢谢

解决方法

利用带有 rollingwindow=len(df)min_periods=1 为您提供一个简单的窗口,直到当前点。然后只需检查值是 min()max() 到那个点。

df["isHigh"] = ( df["close"].rolling(len(df),1).max() == df["close"] )
df["isLow"]  = ( df["close"].rolling(len(df),1).min() == df["close"] )

示例:

   close  isHigh  isLow
0      0    True   True
1     11    True  False
2      2   False  False
3     22    True  False
4    -55   False   True
5     44    True  False
6     43   False  False
7     44    True  False

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。