聚宽 沪深300指数 数据读取分析
所属分类 quant
浏览量 230
JoinQuant 聚宽量化平台
https://www.joinquant.com/
策略研究 / 研究环境
打开 在线 notebook
新手指引.ipynb
常用操作
New
Text File:新建txt文件
Folder:新建文件夹
Python 2:新建基于Python2的notebook
Python 3:新建基于Python3的notebook
Upload
上传文件,文件上传后可以在研究和回测中使用
000300.XSHG 沪深300指数
000001.XSHE 平安银行
600000.XSHG 浦发银行
获取所有指数
df = get_all_securities("index")
df.head(2000)
000001.XSHG 上证指数
000016.XSHG 上证50
000300.XSHG 沪深300
399300.XSHE 沪深300 深市
000688.XSHG,科创50
000698.XSHG,科创100
000852.XSHG,中证1000指数
399006.XSHE,创业板指
399673.XSHE,创业板50
rolling 滚动计算 需要 python3 , python2不支持
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
# etf 510300.XSHG
# etf 510300.XSHG 000300.XSHG 513180.XSHG
df = get_price('000300.XSHG', start_date='2000-01-01', end_date='2024-05-07', frequency='daily', fields=['open','close'])
print(df.info)
print(df.columns)
print(df.index)
# python3
df['ma250'] = df['close'].rolling(window=250).mean()
df['std250'] = df['close'].rolling(window=250).std()
df['ma20'] = df['close'].rolling(window=20).mean()
df['std20'] = df['close'].rolling(window=20).std()
df['bias250'] = (df['close'] - df['ma250']) * 100 / df['ma250']
df['z250'] = (df['close'] - df['ma250']) / df['std250']
df['z20'] = (df['close'] - df['ma20']) / df['std20']
df['max250'] = df['close'].rolling(window=250).max()
df['min250'] = df['close'].rolling(window=250).min()
df['temp250'] = (df['close'] - df['min250']) * 100 / (df['max250'] - df['min250'])
# 唐奇通道
df["up"] = df["close"].rolling(window=20).max()
df["down"] = df["close"].rolling(window=20).min()
df["mid"] = (df["up"] + df["down"]) / 2
# df[["up","down","close"]].plot()
# 布林通道
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[["bollhigh","bolllow","close"]].plot()
# 根据日期过滤
# df = df[df['date'] >='2020-01-01']
# 转成日期
# df['dt']= pd.to_datetime(df['date'])
# 设置索引
# df.set_index('dt')
df['dt'] = df.index
plt.plot(df['dt'],df['close'],color='k',label='close')
# 买入信号
buymask = df[df['bias250']< -15 ]
print(buymask.shape)
buymask = buymask[buymask['z20'] < -2]
print(buymask.shape)
plt.plot(buymask['dt'],buymask['close'],color='red',linestyle='',marker='o')
# 卖出信号
sellmask = df[df['bias250'] > 20 ]
sellmask = sellmask[sellmask['z20'] > 2]
plt.plot(sellmask['dt'],sellmask['close'],color='g',linestyle='',marker='o')
# plt.xticks(rotation=45)
# x轴日期自适应 避免重叠
plt.gcf().autofmt_xdate()
plt.legend()
plt.show()
# bias250
plt.plot(df['dt'],df['bias250'],label='bias250')
# plt.gca() 获当前 Axes 对象
ax = plt.gca()
# 添加 bias250 上下阈值线
ax.axhline(-15, color='red', linestyle='--')
ax.axhline(20, color='red', linestyle='--')
# plt.xticks(rotation=45)
# x轴日期自适应 避免重叠
plt.gcf().autofmt_xdate()
plt.legend()
plt.show()
# z250
plt.plot(df['dt'],df['z250'],label='z250')
ax = plt.gca()
# plt.gca() 获当前 Axes 对象
ax = plt.gca()
# 添加 z250 上下阈值线
ax.axhline(-2, color='red', linestyle='--')
ax.axhline(2, color='red', linestyle='--')
plt.gcf().autofmt_xdate()
plt.legend()
plt.show()
# z20
plt.plot(df['dt'],df['z20'],label='z20')
ax = plt.gca()
# plt.gca() 获当前 Axes 对象
ax = plt.gca()
# 添加 z20 上下阈值线
ax.axhline(-2, color='red', linestyle='--')
ax.axhline(2, color='red', linestyle='--')
plt.gcf().autofmt_xdate()
plt.legend()
plt.show()
上一篇
下一篇
pandas matplot 读取日线数据,滚动计算bias250 添加阈值线
pandas matplot 画图 超出阈值 变颜色
沪深300指数 bias250 z20 z250 分析
油车 电车 使用成本
pandas read_csv 和 to_csv 索引问题
大模型简介