我想通过价格连续3天上涨的属性过滤掉一批公司的强劲表现股票。到目前为止在代码下面。如果有什么帮助的话。
换句话说,我想要一份股票名称的清单,这些股票的价格在过去3天里一直在持续上涨。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pandas_datareader as web
tick = ['AMZN', 'AAPL', 'NFLX', 'XOM', 'T']
df = web.get_data_yahoo(tick,
start = '2020-01-01',
end = '2020-12-16')['Adj Close']发布于 2020-12-16 05:30:28
您可以通过使用rolling方法来比较这种差异是否越来越大:
increase_table = df.rolling(3).apply(lambda x: np.all(np.diff(x) > 0)).astype('boolean')前10行的输出:
Symbols AMZN AAPL NFLX XOM T
Date
2020-01-02 <NA> <NA> <NA> <NA> <NA>
2020-01-03 <NA> <NA> <NA> <NA> <NA>
2020-01-06 False False False False True
2020-01-07 True False False False True
2020-01-08 False False False False True
2020-01-09 False True False False False
2020-01-10 False True False False False
2020-01-13 False True False False False
2020-01-14 False False False False False
2020-01-15 False False False False False我没有从你想要的清单中得到逻辑。但是,这里有一些有用的东西,用来验证每只股票连续3天上涨了多少:
increase_table.sum()返回:
Symbols
AMZN 74
AAPL 59
NFLX 57
XOM 40
T 58
dtype: int64因此,我们知道,"AMZN“有更多的日子,随着增加在过去3天。
注:为了正确解释,这里的True值告诉我们,最后两个值及其本身(当前值;总计=3值)正在增加。要正确放置True值以指示最后三行(以及行本身)正在增加,只需执行increase_table.shift()。
发布于 2020-12-16 04:41:26
试试这个:
def compute_consecutive_increase(tick_data, window_size=3):
# Make sure the time series is sorted by date (assuming that date is the index)
tick_data = tick_data.sort_index()
# Put side-by-side the comparison of current day ('t-0') with previous day ('t-1'),
# then 't-1' with 't-2', etc.
shifted_comparison = pd.concat({f't-{i}': tick_data.shift(i) > tick_data.shift(i+1) \
for i in range(window_size)}, axis=1)
# Collapse horizontally (by date) and return True for each row where all values
# are True, i.e. all last 'window_size' days have positive increases
return shifted_comparison.all(axis=1)
df.transform(compute_consecutive_increase)
# To collapse for each tick, if there's at least one 3-day sequence with increases
df.transform(compute_consecutive_increase).any()https://stackoverflow.com/questions/65317013
复制相似问题