pandas 指标计算
所属分类 quant
浏览量 237
import pandas as pd
import matplotlib.pyplot as plt
# from datetime import datetime
from datetime import datetime, timedelta
start_date='2020-01-01'
# 获取当前日期
today = datetime.now()
# 使用timedelta来获取前一天
yesterday = today - timedelta(days=1)
enddate = yesterday.strftime('%Y-%m-%d')
print(enddate)
# enddate = '2024-05-27'
# etf 510300.XSHG 000300.XSHG 513180.XSHG 512880.XSHG 510900.XSHG
# 聚宽接口读取数据
df = get_price('000300.XSHG', start_date=start_date, end_date=enddate, frequency='daily', fields=['open','close','low','high'])
# 根据日期索引过滤
# df1 = df[df.index >='2023-01-01']
df['ma20'] = df['close'].rolling(window=20).mean()
df['std20'] = df['close'].rolling(window=20).std()
df['bias20'] = (df['close'] - df['ma20']) * 100 / df['ma20']
df['z20'] = (df['close'] - df['ma20']) / df['std20']
df['max20'] = df['close'].rolling(window=20).max()
df['min20'] = df['close'].rolling(window=20).min()
df['temp20'] = (df['close'] - df['min20']) * 100 / (df['max20'] - df['min20'])
df['rise20'] = (df['close'] - df['close'].shift(20)) * 100 / (df['close'].shift(20))
df['chg_abs'] = abs(df['close'] - df['close'].shift(1))
df['chg_abs_sum_20'] = df['chg_abs'].rolling(window=20).sum()
df['rsx20'] = (df['close'] - df['close'].shift(20)) * 100 / df['chg_abs_sum_20']
# 唐奇通道
df["high20"] = df["close"].rolling(window=20).max()
df["low20"] = df["close"].rolling(window=20).min()
# 布林通道
df["bollstd"] = df["close"].rolling(window=20).std()
df["bollmid"] = df["close"].rolling(window=20).mean()
df["bollhigh"] = df["bollmid"] + 2*df["bollstd"]
df["bolllow"] = df["bollmid"] - 2*df["bollstd"]
df['ma250'] = df['close'].rolling(window=250).mean()
df['bias250'] = (df['close'] - df['ma250']) * 100 / df['ma250']
# 每日振幅 极差 range
df['r'] = (df['high'] - df['low']) * 100 / df['close']
# 20日均振幅
df["vib20"] = df["r"].rolling(window=20).mean()
# 20日均线斜率 1阶导
df['mas20'] = (df['ma20'] - df['ma20'].shift(1))*100 / df['ma20'].shift(1)
# 20日均线斜率的斜率 2阶导
df['mass20'] = (df['mas20'] - df['mas20'].shift(1))*100 / df['mas20'].shift(1)
df2 = df.resample('M').min()
df3 = df.resample('M').max()
# 日期索引变每月的 最后一天, 保留 最小值 最大值的日期
# 重采样,按月计算最小值
monthly_min = df.resample('M').min()
# 每月最小值的具体日期
monthly_min_dates = df['close'].groupby(df.index.to_period('M')).idxmin()
# print(monthly_min_dates.tail(100))
# 将索引替换为 最小值对应的具体日期
monthly_min.index = monthly_min_dates.values
# print(monthly_min.tail(100))
# print(df.tail(100))
df['close'].plot()
plt.title('close')
plt.show()
df['bias250'].plot()
plt.title('bias250')
plt.show()
上一篇
下一篇
MongoDB 和 Redis
centos7 安装 mongodb
交易 理念 策略 技巧 案例
MAC Vue 开发环境搭建
vue2项目 目录结构 及 配置文件
vue2 项目运行笔记