【问题标题】:Find maxima and minima within data and append True/False to corresponding row - Python在数据中查找最大值和最小值并将 True/False 附加到相应的行 - Python
【发布时间】:2021-04-29 16:52:38
【问题描述】:

我想在股市数据中找到摆动高点和摆动低点。我希望能够将每个点附加到一列。例如)有一个与收盘价列对齐的 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,即峰值和谷值的总数

感谢任何帮助,谢谢

【问题讨论】:

  • 欢迎来到Stack Overflow.。为了让我们为您提供帮助,您有必要提供一个最小的可重现问题集,其中包括样本输入、预期输出、实际输出以及重现该示例所需的所有相关代码。你所提供的没有达到这个目标。请编辑您的问题以显示最小的可重现集。请参阅Minimal Reproducible Example,了解如何最好地帮助我们。
  • 感谢@itprorh66 的见解,下次我会这样做。我能够弄清楚我的问题,所以没必要。现在更新。再次感谢。

标签: python pandas numpy scipy minmax


【解决方案1】:

利用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

【讨论】:

  • 谢谢@Noah。我能够弄清楚我在寻找什么,但这绝对是有帮助的,并且是一个可以添加到我的腰带的新工具。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-08
  • 1970-01-01
  • 2018-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多