调取数据


下面介绍两种常用的数据调取方式:

  • 通过dcube python包
  • 使用http协议直接获取

前提条件

1、已经获取到datacube token凭证 【注册用户
2、已经安装好python环境

Python SDK

安装Python包

pip install dcube

导入datacube

import dcube as dc

用token初始化pro接口

pro = dc.pro_api('your token')

数据调取

以获取交易日历信息为例:

df = pro.trade_cal(exchange='', start_date='20180901', end_date='20181001', fields='exchange,cal_date,is_open,pretrade_date', is_open='0')

或者

df = pro.query('trade_cal', exchange='', start_date='20180901', end_date='20181001', fields='exchange,cal_date,is_open,pretrade_date', is_open='0')

调取结果:

    exchange  cal_date    is_open pretrade_date
0          SSE       20180901        0      20180831
1          SSE       20180902        0      20180831
2          SSE       20180908        0      20180907
3          SSE       20180909        0      20180907
4          SSE       20180915        0      20180914
5          SSE       20180916        0      20180914
6          SSE       20180922        0      20180921
7          SSE       20180923        0      20180921
8          SSE       20180924        0      20180921
9          SSE       20180929        0      20180928
10         SSE       20180930        0      20180928
11         SSE       20181001        0      20180928

HTTP协议方式

http restful 采用post方式,通过json body传入接口参数,请求地址为datacubeapi.foundersc.com

输入参数

  • api_name,接口名称;
  • token,用于识别唯一用户的标识;
  • params,接口参数,如daily接口中start_date和end_date;
  • fields,字段列表,用于接口获取指定的字段,以逗号分隔,如"open,high,low,close";

输出参数

  • code: 接口返回码,2002表示权限问题。
  • msg: 错误信息;
  • data: 具体数据,成功的请求包含fields和items字段,fields与items数据一一对齐;

示例

采用命令行工具curl的请求示例如下:

curl -X POST -d '{"api_name": "trade_cal", "token": "xxxxxxxx", "params": {"exchange":"", "start_date":"20180901", "end_date":"20181001", "is_open":"0"}, "fields": "exchange,cal_date,is_open,pretrade_date"}' http://datacubeapi.foundersc.com

返回结果:

```json
{
"code":0,
"msg":null,
"data":{
"fields":[
"exchange",
"cal_date",
"is_open",
"pretrade_date"
],
"items":[
[
"SSE",
"20180901",
0,
"20180831"
],
[
"SSE",
"20180902",
0,
"20180831"
],
[
"SSE",
"20180908",
0,
"20180907"
],

        ...

        [
            "SSE",
            "20180929",
            0,
            "20180928"
        ],
        [
            "SSE",
            "20180930",
            0,
            "20180928"
        ],
        [
            "SSE",
            "20181001",
            0,
            "20180928"
        ]
    ]
}

}```

置顶