python - 如何过滤掉连续3天涨价的股票
问题描述
我想通过连续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']
解决方案
尝试这个:
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()
推荐阅读
- c - 将 char 指针转换为 int 指针是如何工作的?
- mariadb - 为什么会出现此查询“未选择数据库”
- linux - C 编程 Fork 示例
- javascript - 如何使用 jquery 将数据更新到外部 json 文件中?
- ios - 子类化 UIView 时如何检测箭头键?
- ios - setEditing() 在 Swift 中究竟做了什么?
- c++ - 逻辑回归返回错误的预测
- javascript - 在 react redux 构造函数中设置状态
- r - 重新插入其输出的 R 函数
- javascript - 如何从 AgEditorComponent 的上下文中聚焦 ag-grid Angular 中的下一个单元格