我想知道如何标记价格属于4个涨价部分的行。 “是连续的”实际上是标记
我设法在行之间进行了区分:
df['diff1'] = df['Close'].diff()
但我没能找出哪一行是4次涨价的一部分
我想到了使用df.rolling()
例如df
在第0-3行上,我们需要在[“is_Concertive”]列上获得“True”的输出,因为此连续行上的['diff1']增加了4行
在第8-11行,我们需要在[“is_Concertive”]列上获得'False'的输出,因为这个连续行上的['diff1']为零
Date Price diff1 is_consecutive
0 1/22/20 0 0 True
1 1/23/20 130 130 True
2 1/24/20 144 14 True
3 1/25/20 150 6 True
4 1/27/20 60 -90 False
5 1/28/20 95 35 False
6 1/29/20 100 5 False
7 1/30/20 50 -50 False
8 2/01/20 100 0 False
9 1/02/20 100 0 False
10 1/03/20 100 0 False
11 1/04/20 100 0 False
12 1/05/20 50 -50 False
一般示例:
如果 价格=[30,55,60,65,25]
列表中连续数字的不同形式为:
diff1=[0,25,5,5,-40]
所以当diff1加上它实际上意味着连续的价格在上涨
我需要标记(在df中)有4个连续上升的行
感谢您的帮助(-:
尝试:
.rolling
,窗口大小为4
,最小周期为1
:印刷品:
假设数据帧已排序。一种方法是基于差异的总和,以确定继3天上涨趋势(即4天上涨趋势)之后的首次上涨价格移动
请注意,以上仅考虑了严格上涨的价格(不相等)
然后,我们还使用win_view自定义函数覆盖前3个价格的is_Continuous标签,使其也TRUE:
请注意,我们将替换为True的值
编辑1 受自定义的win_view函数的启发,我意识到,只需win_view(无需使用累积和)即可获得解决方案,如下所示:
我们维护2个数组,1个用于返回,1个用于索引。我们收集有3个连续正回报
np.all(arr[:, 1:] > 0, axis=1
(即4个上涨价格)的指数,并替换原始df中的指数函数将返回名为
"consecutive_up"
的列,该列表示5个递增序列中的所有行;返回名为"consecutive_down"
的列,该列表示4个法令序列中的所有行相关问题 更多 >
编程相关推荐