Wind API 学习笔记一(概述及WSD日期序列函数)

页面跳转

参考链接

疑似官方: Wind-Client APi 帮助中心

wind Api 学习

综述

 

目前万矿网支持的API函数有:

 

 

 

1、WSD日期序列函数支持股票、债券、基金、期货、指数等多种证券的基本资料、股东信息、市场行情、证券分析、预测评级、财务数据等各种数据。WSD可以支持取 多品种单指标 或者 单品种多指标 的时间序列数据。

2、WSS多维函数同样支持股票、债券、基金、期货、指数等多种证券的基本资料、股东信息、市场行情、证券分析、预测评级、财务数据等各种数据。但是WSS支持取多品种多指标某个时间点的截面数据。

3、WSQ行情数据函数支持股票、债券、基金、期货、指数等多种证券品种的实时行情数据,既可以选择获取一次性的快照数据,也可以选择订阅数据(即交易所有新的行情就推送)。

4、WSET数据集支持股票、债券、基金、期货、指数等多种证券品种板块成分指数历史成分股以及权重,以及各种市场常用报表的获取。

5、TDays 日期函数:日期函数包含日期序列函数(TDays)、日期偏移函数(TDaysOffset) 以及日期区间统计函数(TDaysCount)。

下面为大家介绍各个函数的详细用法。

注: 建议用户在使用取数函数时直接借助API函数生成相应的取数代码,然后修改其中的参数使其满足自己的取数需求。


定义

该命令用来获取选定证券品种的历史序列数据,包括日间的行情数据、基本面数据以及技术数据指标

函数输入

** WSD函数结构 w.wsd(security,fields,beginTime,endTime,option)**

作者:下面这行不知道干嘛用的,看起来像是title

| |Element|Type|Description| |

| :------------- :|:-------------:| :-----:  |

作者:原文中的这些 “| ” 排版非常乱。下面是我的理解排版的

|证券(必选)|Security |String|获取数据的证券列表|范例1:'600030.SH'说明:证券列表支持Wind代码及证券转换类工具函数输出的Wind代码结果|

|指标(必选)|Fields|String |获取数据的指标列表 |范例1:'CLOSE,HIGH,LOW,OPEN'|

|起始日期(必选)|beginTime ||时间序列的起始日期| 范例1:’2017-01-01’,’-5w’说明:支持日期类工具函数输出的标准日期结果,支持相对日期宏表达方式,日期宏具体使用方式参考’日期宏’部分内容|

|截止日期(必选) |endTime|| 时间序列的截止日期,若为空默认为系统当前日期 |范例1:’2017-05-30’,Sys.Date(),支持相对日期,比如’0w’; 不输入的话为当前时间说明:支持日期类工具函数输出的标准日期结果,支持相对日期宏表达方式|

作者:下面这些应该都是option选项的可选参数

|指标参数(可选)Parameter/Value | String提取指标时使用的参数名/指定参数的值 |范例:’TRADE_DATE=20110301;FUND_DATE=20101231’说明:多指标参数支持在不同引号内分开取值|

|变频参数(可选)| Period | String |每天一值:D/每周一值:W/每月一值:M/每季度一值:Q/每半年一值:S/每年一值:Y *| 范例:’Period=D’ ,默认Period=D

|输出日期(可选)| Days | String| 所有工作日:Weekdays/所有日历日:Alldays/排除所有非交易日:Trading| 范例:’Days=Trading’,默认Days=Trading

|填充方式(可选)| Fill| String| 沿用之前数据:Previous/返回空值:Blank| 范例:’Fill=Previous’,默认Fill=Blank|

|日期排序(可选)| Order| String| 升序:A/ 降序:D,最近日期在先||

|交易日历(可选)|TradingCalendar| String| 选择不同交易所所在国家地区日历| 范例1:’ TradingCalendar =SSE’,默认TradingCalendar =SSE;SSE表示上交所,SZSE表示深圳证券交易所,CFFE表示中金所……

| |输出币种(可选)| Currency| String| *使用货币设置: ORIGINAL:原始货币/HKD:港币/USD:美元/CNY:人民币|范例1:’Currency =Original’,默认Currency =Original|

 

关于指标参数的详细说明见 7.指标常见参数说明

函数输出

作者:下面是重新排版的

||输出内容|说明|

| :------------- :|:-------------:| :-----:|

|错误ID |ErrorCode| 返回值为0 ,则表示代码运行正常。若为其他则需查找原因|

|数据列表|Data|函数读取的数据存到此列表中,比如:读取000592.SZ 的close,open指标从'2017-05-08'到'2017-05-18'区间的数据.Data=[[5.12,5.16,5.02,4.9,4.91,5.13,5.35,5.42,5.32],[5.3,5.12,5.17,4.98,4.94,4.93,5.1,5.4,5.4]]| |证券代码列表| Codes|输入的证券代码列表 .Codes=[000592.SZ]|

|字段列表| Field |函数输入中请求的字段列表 .Fields=[CLOSE,OPEN]|

|时间列表| Times |输出时间序列.Times=[20170508,20170509,20170510,20170511,20170512,20170515,20170516,20170517,20170518]|

 

输出格式分析

情形1:多个代码+多个日期+多个指标

w.wsd("010504.SH,112108078.IB", "municipalbond,cityinvestmentbondgeo", "2021-12-21", "2021-12-24",usedf=True)


报错
(-40522018,        OUTMESSAGE
0  CWSDService: multi-codes with multi-indicators...)

情形2:多个代码+多个日期+单个指标

w.wsd("010504.SH,112108078.IB", "municipalbond", "2021-12-21", "2021-12-24",usedf=True)

(0,            010504.SH 112108078.IB
2021-12-21         否            否
2021-12-22         否            否
2021-12-23         否            否
2021-12-24         否            否)

情形3:多个代码+单个日期+单个指标

w.wsd("010504.SH,112108078.IB", "municipalbond", "2021-12-24", "2021-12-24",usedf=True)


(0,              MUNICIPALBOND
010504.SH                否
112108078.IB             否)

情形4:多个代码+单个日期+多个指标

w.wsd("010504.SH,112108078.IB", "municipalbond,cityinvestmentbondgeo", "2021-12-24", "2021-12-24",usedf=True)


报错
(-40522018,        OUTMESSAGE
0  CWSDService: multi-codes with multi-indicators...)

情形5:单个代码+多个日期+多个指标

w.wsd("010504.SH", "municipalbond,cityinvestmentbondgeo", "2021-12-21", "2021-12-24",usedf=True)


(0,            MUNICIPALBOND CITYINVESTMENTBONDGEO
2021-12-21             否                  None
2021-12-22             否                  None
2021-12-23             否                  None
2021-12-24             否                  None)

情形6:单个代码+多个日期+单个指标

w.wsd("010504.SH", "municipalbond", "2021-12-21", "2021-12-24",usedf=True)


(0,            MUNICIPALBOND
2021-12-21             否
2021-12-22             否
2021-12-23             否
2021-12-24             否)


情形7:单个代码+单个日期+多个指标

w.wsd("010504.SH", "municipalbond,cityinvestmentbondgeo", "2021-12-24", "2021-12-24",usedf=True)


(0,           MUNICIPALBOND CITYINVESTMENTBONDGEO
010504.SH             否                  None)

情形8:单个代码+单个日期+单个指标

w.wsd("010504.SH", "municipalbond", "2021-12-24", "2021-12-24",usedf=True)


(0,           MUNICIPALBOND
010504.SH             否)

总结:

1. 不支持多个指标和多个代码同时请求

2. 假设行标题为x,列标题为y。加上一个表格的隐含属性。分析如下表

 

 共得到4种不同格式的结果集。

示例

# 加载相关的包
from WindPy import *
import time
import pandas as pd
w.start()
作者:实际测试这段代码是会报登陆失败的,这个后面再说。下面是文档中给的执行结果

COPYRIGHT (C) 2017 Wind Information Co., Ltd. ALL RIGHTS RESERVED.
IN NO CIRCUMSTANCE SHALL WIND BE RESPONSIBLE FOR ANY DAMAGES OR LOSSES
CAUSED BY USING WIND QUANT API FOR PYTHON.

 例1、 取富士康概念股近七个交易日的每天机构资金流入额

date=time.strftime("%Y-%m-%d", time.localtime()) 
stock=w.wset("sectorconstituent","date="+date+";sectorid=1000011346000000").Data[1]  #富士康概念股最新板块成分
buyamt=w.wsd(stock, "mfd_buyamt_d", "ED-7TD", date, "unit=1;traderType=1") #traderType表示类型,如机构、大户、中户、散户,具体参数设置可以借助API函数了解
buyamt

 

返回结果:
.ErrorCode=0 .RequestID=795 .Codes=[000727.SZ,002138.SZ,002182.SZ,002388.SZ,002681.SZ,002735.SZ,002831.SZ,002861.SZ,002885.SZ,002886.SZ,...] .Fields=[MFD_BUYAMT_D] .Times=[20180614,20180615,20180619,20180620,20180621,20180622,20180625,20180626] .Data=[[4080000.0,430600.0,2555114.0,2654878.0,1080770.0,0.0,1379156.9999999998,nan],[703075.9999999999,4646056.0,7385931.000000001,41127382.0,9715612.0,1833266.9999999998,2006920.0,nan],[1370000.0,713212.0,1566466.0,162635.0,0.0,0.0,0.0,nan],[0.0,448462.00000000006,0.0,0.0,0.0,0.0,0.0,nan],[6393914.0,7288929.000000001,10012665.0,1693427.0,974352.0,1139238.0,0.0,nan],[nan,nan,nan,nan,nan,nan,nan,nan],[2423169.0,3989380.0,3885000.0,0.0,11359000.0,5429559.0,10675815.0,nan],[20436142.0,6524435.0,2883179.0,0.0,3644898.0,2440908.0,516831.00000000006,nan],[56344942.0,47787275.0,6537384.0,26814889.0,3486963.0,13616599.000000002,15223910.0,nan],[101045277.0,20793487.0,2350000.0,12947593.0,2598133.0000000005,51000.0,0.0,nan],...]

 

pd.DataFrame(buyamt.Data,index=stock,columns=buyamt.Times).T

 

 000727.SZ002138.SZ002182.SZ002388.SZ002681.SZ...600673.SH601137.SH601138.SH603228.SH603595.SH
2018-06-14 00:00:00.005 4080000.0 703076.0 1370000.0 0.0 6393914.0 ... 45833092.0 0.0 1.080932e+09 0.0 0.0
2018-06-15 00:00:00.005 430600.0 4646056.0 713212.0 448462.0 7288929.0 ... 42659629.0 0.0 7.987039e+08 0.0 0.0
2018-06-19 00:00:00.005 2555114.0 7385931.0 1566466.0 0.0 10012665.0 ... 77407028.0 0.0 7.203009e+08 1058400.0 5113660.0
2018-06-20 00:00:00.005 2654878.0 41127382.0 162635.0 0.0 1693427.0 ... 69425880.0 0.0 4.781276e+08 0.0 1433600.0
2018-06-21 00:00:00.005 1080770.0 9715612.0 0.0 0.0 974352.0 ... 41665049.0 0.0 4.146398e+08 0.0 6688679.0
2018-06-22 00:00:00.005 0.0 1833267.0 0.0 0.0 1139238.0 ... 23974394.0 0.0 4.132930e+08 1440000.0 2397780.0
2018-06-25 00:00:00.005 1379157.0 2006920.0 0.0 0.0 0.0 ... 17107278.0 0.0 1.747496e+08 0.0 2386818.0
2018-06-26 00:00:00.005 NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
8 rows × 26 columns
 

例2、 任取一只国债010107.SH六月份以来的净值历史行情数据

 

history_data=w.wsd("010107.SH", "sec_name,ytm_b,volume,duration,convexity,open,high,low,close,vwap", "2018-06-01", "2018-06-11", "returnType=1;PriceAdj=CP") # returnType表示到期收益率计算方法,PriceAdj表示债券价格类型‘
#pd.DataFrame(history_data.Data,index=history_data.Fields,columns=history_data.Times).T
pd.DataFrame(history_data.Data,index=["中文简称","YTM","成交量","久期","凸性","开盘价","最高价","最低价","收盘价","均价"],columns=history_data.Times).T

 

 中文简称YTM成交量久期凸性开盘价最高价最低价收盘价均价
2018-06-01 00:00:00.005 21国债(7) 3.59133 3.8318e+07 2.95846 11.2978 102.1 102.15 101.92 101.98 101.977
2018-06-04 00:00:00.005 21国债(7) 3.6132 3.024e+07 2.95016 11.2398 101.91 101.98 101.9 101.91 101.941
2018-06-05 00:00:00.005 21国债(7) 3.596 3.1485e+07 2.94749 11.2262 101.85 101.97 101.85 101.96 101.933
2018-06-06 00:00:00.005 21国债(7) 3.60554 1.77095e+08 2.94471 11.2064 102.06 102.06 101.76 101.93 101.868
2018-06-07 00:00:00.005 21国债(7) 3.59166 5.244e+07 2.94202 11.1921 101.92 102 101.79 101.97 101.936
2018-06-08 00:00:00.005 21国债(7) 3.57107 4.0053e+07 2.93937 11.1793 101.82 102.05 101.82 102.03 101.999
2018-06-11 00:00:00.005 21国债(7) 3.5561 7.946e+07 2.93121 11.1301 102.02 102.09 101.95 102.07 102.016

 

例3、 取国内所有农产品主力合约不同日期所对应的具体合约

1 future=w.wset("sectorconstituent","date="+date+";sectorid=1000009337000000") #农产品主力合约板块
2 tradecode=w.wsd(future.Data[1], "trade_hiscode", "2018-01-01", "2018-06-11", "")
3 pd.DataFrame(tradecode.Data,index=future.Data[2],columns=tradecode.Times).T

 

 DCE豆一CZCE苹果DCE豆二DCE胶合板BOCE棉花新疆...CZCE菜粕CZCE菜籽CZCE白糖CZCE强麦DCE豆油
2018-01-02 00:00:00.005 A1805.DCE AP805.CZC B1805.DCE BB1804.DCE BCXJSPT.BCE ... RM805.CZC RS807.CZC SR805.CZC WH805.CZC Y1805.DCE
2018-01-03 00:00:00.005 A1805.DCE AP805.CZC B1805.DCE BB1804.DCE BCXJSPT.BCE ... RM805.CZC RS807.CZC SR805.CZC WH805.CZC Y1805.DCE
2018-01-04 00:00:00.005 A1805.DCE AP805.CZC B1805.DCE BB1804.DCE BCXJSPT.BCE ... RM805.CZC RS807.CZC SR805.CZC WH805.CZC Y1805.DCE
2018-01-05 00:00:00.005 A1805.DCE AP805.CZC B1805.DCE BB1804.DCE BCXJSPT.BCE ... RM805.CZC RS807.CZC SR805.CZC WH805.CZC Y1805.DCE
2018-01-08 00:00:00.005 A1805.DCE AP805.CZC B1805.DCE BB1804.DCE BCXJSPT.BCE ... RM805.CZC RS807.CZC SR805.CZC WH805.CZC Y1805.DCE
2018-01-09 00:00:00.005 A1805.DCE AP805.CZC B1805.DCE BB1804.DCE BCXJSPT.BCE ... RM805.CZC RS807.CZC SR805.CZC WH805.CZC Y1805.DCE
2018-01-10 00:00:00.005 A1805.DCE AP805.CZC B1805.DCE BB1804.DCE BCXJSPT.BCE ... RM805.CZC RS807.CZC SR805.CZC WH805.CZC Y1805.DCE
2018-01-11 00:00:00.005 A1805.DCE AP805.CZC B1805.DCE BB1804.DCE BCXJSPT.BCE ... RM805.CZC RS807.CZC SR805.CZC WH805.CZC Y1805.DCE
2018-01-12 00:00:00.005 A1805.DCE AP805.CZC B1805.DCE BB1804.DCE BCXJSPT.BCE ... RM805.CZC RS807.CZC SR805.CZC WH805.CZC Y1805.DCE
2018-01-15 00:00:00.005 A1805.DCE AP805.CZC B1805.DCE BB1805.DCE BCXJSPT.BCE ... RM805.CZC RS807.CZC SR805.CZC WH805.CZC Y1805.DCE
... ... ... ... ... ... ... ... ... ... ... ...
2018-05-29 00:00:00.005 A1809.DCE AP810.CZC B1809.DCE BB1809.DCE BCXJSPT.BCE ... RM809.CZC RS809.CZC SR809.CZC WH809.CZC Y1809.DCE
2018-05-30 00:00:00.005 A1809.DCE AP810.CZC B1809.DCE BB1809.DCE BCXJSPT.BCE ... RM809.CZC RS809.CZC SR809.CZC WH809.CZC Y1809.DCE
2018-05-31 00:00:00.005 A1809.DCE AP810.CZC B1809.DCE BB1809.DCE BCXJSPT.BCE ... RM809.CZC RS809.CZC SR809.CZC WH809.CZC Y1809.DCE
2018-06-01 00:00:00.005 A1809.DCE AP810.CZC B1809.DCE BB1809.DCE BCXJSPT.BCE ... RM809.CZC RS809.CZC SR809.CZC WH809.CZC Y1809.DCE
2018-06-04 00:00:00.005 A1809.DCE AP810.CZC B1809.DCE BB1809.DCE BCXJSPT.BCE ... RM809.CZC RS809.CZC SR809.CZC WH809.CZC Y1809.DCE
2018-06-05 00:00:00.005 A1809.DCE AP810.CZC B1809.DCE BB1809.DCE BCXJSPT.BCE ... RM809.CZC RS809.CZC SR809.CZC WH809.CZC Y1809.DCE
2018-06-06 00:00:00.005 A1809.DCE AP810.CZC B1809.DCE BB1809.DCE BCXJSPT.BCE ... RM809.CZC RS809.CZC SR809.CZC WH809.CZC Y1809.DCE
2018-06-07 00:00:00.005 A1809.DCE AP810.CZC B1809.DCE BB1809.DCE BCXJSPT.BCE ... RM809.CZC RS809.CZC SR809.CZC WH809.CZC Y1809.DCE
2018-06-08 00:00:00.005 A1809.DCE AP810.CZC B1809.DCE BB1809.DCE BCXJSPT.BCE ... RM809.CZC RS809.CZC SR809.CZC WH809.CZC Y1809.DCE
2018-06-11 00:00:00.005 A1809.DCE AP810.CZC B1809.DCE BB1809.DCE BCXJSPT.BCE ... RM809.CZC RS809.CZC SR809.CZC WH809.CZC Y1809.DCE



106 rows × 21 columns

 

 

 
 
 
 
 
 
 
 
 
posted @ 2021-12-03 11:22  踏叶乘风  阅读(7763)  评论(0编辑  收藏  举报