指数和ETF指标计算统计
所属分类 quant
浏览量 223
# 波段 特征指标 计算
# y250 按月采样 最大最小值
# 大于 0 小于0 分别计算 温度 , bias为0时 温度为 50度
# 5 10 日 ma y z mas
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# from datetime import datetime
from datetime import datetime, timedelta
# 2000-01-01
start_date='2010-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
code = '000300.XSHG'
# 聚宽接口读取数据
df = get_price(code, start_date=start_date, end_date=enddate, frequency='daily', fields=['open','close','low','high'])
print(df.shape)
print(df.index[0],df.index[-1])
# 根据日期索引过滤
# df1 = df[df.index >='2023-01-01']
df['ma5'] = df['close'].rolling(window=5).mean()
df['ma10'] = df['close'].rolling(window=10).mean()
df['ma20'] = df['close'].rolling(window=20).mean()
df['ma60'] = df['close'].rolling(window=60).mean()
df['ma120'] = df['close'].rolling(window=120).mean()
df['ma250'] = df['close'].rolling(window=250).mean()
df['dif10'] = (df['ma5'] - df['ma10']) * 100 / df['ma5']
df['dif20'] = (df['ma5'] - df['ma20']) * 100 / df['ma5']
df['dif60'] = (df['ma5'] - df['ma60']) * 100 / df['ma5']
df['dif120'] = (df['ma5'] - df['ma120']) * 100 / df['ma5']
df['dif250'] = (df['ma5'] - df['ma250']) * 100 / df['ma5']
df['bias5'] = (df['close'] - df['ma5']) * 100 / df['ma5']
df['bias10'] = (df['close'] - df['ma10']) * 100 / df['ma10']
df['bias20'] = (df['close'] - df['ma20']) * 100 / df['ma20']
df['bias60'] = (df['close'] - df['ma60']) * 100 / df['ma60']
df['bias120'] = (df['close'] - df['ma120']) * 100 / df['ma120']
df['bias250'] = (df['close'] - df['ma250']) * 100 / df['ma250']
df['min20'] = df['close'].rolling(window=20).min()
df['max20'] = df['close'].rolling(window=20).max()
df['ice20'] = (df['max20'] - df['close']) * 100 / df['max20']
df['hot20'] = (df['close'] - df['min20']) * 100 / df['min20']
# 5日均线斜率
df['mas5'] = (df['ma5'] - df['ma5'].shift(1))*100 / df['ma5'].shift(1)
df['mas10'] = (df['ma10'] - df['ma10'].shift(1))*100 / df['ma10'].shift(1)
df['mas20'] = (df['ma20'] - df['ma20'].shift(1))*100 / df['ma20'].shift(1)
df['mas60'] = (df['ma60'] - df['ma60'].shift(1))*100 / df['ma60'].shift(1)
df['mas120'] = (df['ma120'] - df['ma120'].shift(1))*100 / df['ma120'].shift(1)
df['mas250'] = (df['ma250'] - df['ma250'].shift(1))*100 / df['ma250'].shift(1)
df['xmas'] = (df['mas5'] + df['mas10'] + df['mas20']) / 3
df['std5'] = df['close'].rolling(window=5).std()
df['std10'] = df['close'].rolling(window=10).std()
df['std20'] = df['close'].rolling(window=20).std()
df['std60'] = df['close'].rolling(window=60).std()
df['std120'] = df['close'].rolling(window=120).std()
df['std250'] = df['close'].rolling(window=250).std()
df['z5'] = (df['close'] - df['ma5']) / df['std5']
df['z10'] = (df['close'] - df['ma10']) / df['std10']
df['z20'] = (df['close'] - df['ma20']) / df['std20']
df['z60'] = (df['close'] - df['ma60']) / df['std60']
df['z120'] = (df['close'] - df['ma120']) / df['std120']
df['z250'] = (df['close'] - df['ma250']) / df['std250']
df = df.dropna()
df2 = df.resample('M').min()
df3 = df.resample('M').max()
# 描述性统计 0.25,0.5,0.75 分位
# df.describe()
df.describe()
a1 = np.arange(0, 0.1, 0.01)
a2 = np.arange(0.1, 0.9, 0.05)
a3 = np.arange(0.9, 1.0, 0.01)
# print(a1,a2,a3)
# a = a1+a2+a3
a = np.concatenate((a1, a2,a3))
print(a)
df.describe(a)
# 指定列
cols = ['bias20','bias60','bias120','bias250','mas5','mas10','mas20','z5','z10','z20','z60','z120','z250']
df[cols].describe()
a = [0.02,0.05,0.1,0.2,0.25,0.3,0.5,0.75,0.8,0.9,0.95,0.98]
# 最后250行
df.tail(250)[cols].describe(a)
df.tail(500)[cols].describe(a)
descdf = df[cols].describe(a)
# descdf.to_html("./data/"+code+'_desc.html')
descdf
#df.tail(250)['close'].plot()
# df.tail(250)['ma5'].plot()
#df.tail(250)['ma10'].plot()
#df.tail(250)['ma20'].plot()
#df.tail(250)['ma60'].plot()
# df.tail(250)['mas20'].plot()
# df['z250'].plot()
# plt.show()
df.tail(250)['dif10'].plot()
plt.title('dif10')
ax = plt.gca()
ax.axhline(0, color='red', linestyle='--')
plt.show()
df.tail(250)['dif20'].plot()
ax = plt.gca()
ax.axhline(0, color='red', linestyle='--')
plt.title('dif20')
plt.show()
df.tail(250)['dif60'].plot()
ax = plt.gca()
ax.axhline(0, color='red', linestyle='--')
plt.title('dif60')
plt.show()
# 关键特征计算 保存
cols = ['mas5','mas10','mas20','z5','z10','z20','z60','z120','z250']
p = [0.001,0.01,0.02,0.05,0.1,0.9,0.95,0.98,0.99,0.999]
descdf = df[cols].describe(p)
descdf.to_html("./data/"+code+'_kf.html')
descdf
上一篇
下一篇
vue2 项目运行笔记
《布林线》笔记
ETF投资技巧
grafana静音设置
DuckDB OLAP数据库介绍
hutool简介