本文参考 进行一些改动和细化 主要为了能够读取期货品种的昨结今结
另外安装WIND财经windows终端的情况下,查询相应文档的路径是
“量化-数据接口-API接口(API)”,“量化-数据接口-代码生成器”,WIND本身提供VBA, C#,C++,Python,R和MATLAB这几个接口,视方便可选用WindAPI文档_小黄人的黄的博客-CSDN博客_wind的apipython - WindAPI文档https://blog.csdn.net/Minion_Yellow/article/details/105679716
测试环境:
Windows+Python 3.7
from WindPy import w
import pandas as pd
import numpy as np
w.start() # 必须 需要登陆WIND账户
w.isconnected() # 判断WindPy是否已经登录成功
1.简单测试读取
codes = ["PP2203"]
fields = ["CLOSE"]
beginTime='2021-08-01'
endTime='2022-03-02'
w.wsd(codes, fields, beginTime, endTime)
返回数据应该为
.ErrorCode=0
.Codes=[PP2203]
.Fields=[CLOSE]
.Times=[20210802,20210803,20210804,20210805,20210806,20210809,20210810,20210811,20210812,20210813,...]
.Data=[[None,None,None,None,None,None,None,None,None,None,...]]
其中ErrorCode=0代表读取成功,如果读取数据超限,ErrorCode=40XXXXX之类的数字
注意到Data数据全部为None,为什么呢?
2.读取期货多列数据
# 不加交易所的话 无法提取对应的数据
# Y2203.DCE会成功,Y2203会失败
w.wsd("Y2203.DCE", "close,pre_settle,settle,trade_code,windcode,open,high,low",
"2022-02-05", "2022-03-03", "")
这样就可以成功返回多列数据了
.ErrorCode=0
.Codes=[Y2203.DCE]
.Fields=[CLOSE,PRE_SETTLE,SETTLE,TRADE_CODE,WINDCODE,OPEN,HIGH,LOW,LASTRADEDAY_S,FIRSTRADEDAY_S,...]
.Times=[20220207,20220208,20220209,20220210,20220211,20220214,20220215,20220216,20220217,20220218,...]
.Data=[[10320.0,10162.0,10088.0,10124.0,10102.0,10256.0,10260.0,10288.0,10354.0,10510.0,...],[9970.0,10432.0,10286.0,10040.0,10148.0,10110.0,10206.0,10200.0,10178.0,10320.0,...],[10432.0,10286.0,10040.0,10148.0,10110.0,10206.0,10200.0,10178.0,10320.0,10426.0,...],[Y2203,Y2203,Y2203,Y2203,Y2203,Y2203,Y2203,Y2203,Y2203,Y2203,...],[Y2203.DCE,Y2203.DCE,Y2203.DCE,Y2203.DCE,Y2203.DCE,Y2203.DCE,Y2203.DCE,Y2203.DCE,Y2203.DCE,Y2203.DCE,...],[10256.0,10320.0,10116.0,10156.0,10148.0,10182.0,10206.0,10192.0,10294.0,10410.0,...],[10648.0,10418.0,10116.0,10202.0,10182.0,10372.0,10260.0,10296.0,10412.0,10518.0,...],[10256.0,10134.0,9920.0,10046.0,10010.0,10150.0,10148.0,10016.0,10244.0,10384.0,...],[2022-02-07 00:00:00,2022-02-08 00:00:00,2022-02-09 00:00:00,2022-02-10 00:00:00,2022-02-11 00:00:00,2022-02-14 00:00:00,2022-02-15 00:00:00,2022-02-16 00:00:00,2022-02-17 00:00:00,2022-02-18 00:00:00,...],[None,None,None,None,None,None,None,None,None,None,...],...]
需要注意的是,使用wsd命令时,多个字段情况下,只能每次查询一个代码,也就是说,Y2203和Y2204要分两次查询,请特别注意
3.将得到的数据转换为 dataframe(供参考)
以股票数据为例
w_data = w.wsd("000002.SZ", "trade_code,sec_name,compindex2,indexweight", "2022-02-02", "2022-05-06", "index=3;unit=1;windCode3=000300.SH")
df = pd.DataFrame(w_data.Data,index=w_data.Fields, columns=w_data.Times).T
print(df)
这样就可以得到对应的dataframe了 ,然后操作起来就方便很多
4.不同品种对应的交易所,也稍微发一下
"exchange": {"SHF": ["CU","AL","ZN","PB","AU","AG","RB","WR","FU","RU","BU","HC",
"NI","SN","SP","SS"], "INE": ["SC","NR","LU","BC"],"DCE": ["C","A","B","M","Y","P",
"L","V","J","JM","FB","BB","I","JD","PP","CS","EG","RR","EB","PG","LH"],
"CZC": ["WH","PM","CF","SR","TA","OI","RI","MA","FG","RS","RM","JR","LR","SF","SM","ZC",
"CY","AP","CJ","UR","SA","PF","PK"],"CFE": ["IF","IC","IH","T","TS","TF","IO"],
"SH": ["510050"]},
以上,完结撒花。