问题描述
我想在股市数据中找到摆动高点和摆动低点。我希望能够将每个点附加到一列。例如)有一个 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,这是峰值和谷值的总数
感谢任何帮助,谢谢
解决方法
利用带有 rolling
和 window=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