掘金社区

人手必备!策略中最常用的5类Python数据接口,我们帮你整理好了Pinned highlighted

掘金小Q 【 论坛管理员 】 发表在策略研究 2021-12-02 17:59:49

策略研究
264
0
0

在日常中,小Q会收到来自用户的各种关于终端使用的提问,其中以数据接口的问题最为常见。

所以,今天为大家整理了在掘金量化中获取数据时常用的5类数据接口并展示相关的函数原型与参考示例,一起来看看吧~

不熟悉的亲可以收藏或者转发给你的小伙伴,下次需要的时候马上就能找到~

 ​

目录

 ​
01基础数据
    get_instrumentinfos - 查询交易标的基本信息
    get_instruments - 查询最新交易标的信息
    get_history_instruments - 查询交易标的历史信息数据
    get_trading_dates - 查询交易日列表
    get_previous_trading_date - 查询指定日期的上一交易日
    get_next_trading_date - 查询指定日期的下一个交易日

02行情数据
    current - 查询当前行情快照
    history - 查询历史行情
    history_n - 查询历史行情最新n条
    context.data - 查询订阅数据

03财务数据
    get_fundamentals - 查询基本面数据
    get_fundamentals_n - 查询基本面数据最新n条
    
04指数成分股数据
    get_constituents - 查询指数最新成份股
    get_history_constituents - 查询指数成份股的历史数据

05行业数据
    get_industry - 查询行业股票列表

 ​

01基础数据

 ​

1、get_instrumentinfos - 查询交易标的基本信息

由此获取的交易标的基本信息, 与时间无关。

函数原型:

get_instrumentinfos(symbols=None, exchanges=None, sec_types=None, names=None, fields=None, df=False)

示例:

get_instrumentinfos(symbols=['SHSE.000001','SHSE.000002'],df=True)

输出:

sec_name      symbol         price_tick  delisted_date          sec_type  sec_abbr   sec_id       listed_date     exchange上证指数     SHSE.000001            0  2038-01-01 00:00:00           3    szzs          000001    1991-07-15 00:00:00  SHSEA股指数      SHSE.000002            0  2038-01-01 00:00:00           3    Agzs          000002    1992-02-21 00:00:00  SHSE

 ​

2、get_instruments - 查询最新交易标的信息

可获取基本数据及最新日频数据。

函数原型:

get_instruments(symbols=None, exchanges=None, sec_types=None, names=None, skip_suspended=True, skip_st=True, fields=None, df=False)

示例:

get_instruments(exchanges='SZSE', df=True)

输出:

  adj_factor    is_st    upper_limit  sec_name      pre_close  symbol         price_tick  delisted_date        exchange    listed_date            sec_type    settle_price    lower_limit    multiplier  sec_abbr      position  trade_date               sec_id    is_suspended    margin_ratio   115.338          0          12.38  平安银行             11.25  SZSE.000001          0.01  2038-01-01 00:00:00  SZSE        1991-04-03 00:00:00           1               0          10.13             1  payx               0   2017-09-19 00:00:00    000001               0               1   127.812          0          30.84  万科A                28.04  SZSE.000002          0.01  2038-01-01 00:00:00  SZSE        1991-01-29 00:00:00           1               0          25.24             1  wkA                0   2017-09-19  00:00:00   000002               0               1   7.44538          0          27.24  国农科技             24.76  SZSE.000004          0.01  2038-01-01 00:00:00  SZSE        1991-01-14 00:00:00           1               0          22.28             1  gnkj               0   2017-09-19 00:00:00    000004               0               1   ······

 ​

3、get_history_instruments - 查询交易标的历史信息数据

可返回指定symbols的标的日频历史数据。

函数原型:

get_history_instruments(symbols, fields=None, start_date=None, end_date=None, df=False)

示例:

get_history_instruments(symbols='SZSE.000001,SZSE.000002', start_date='2017-09-19', end_date='2017-09-19', df=True)

输出:

  adj_factor    is_st       settle_price   upper_limit  symbol        pre_close    lower_limit       is_suspended    multiplier    position      trade_date             margin_ratio     115.338        0               0          12.38  SZSE.000001        11.25          10.13               0             1           0    2017-09-19 00:00:00               1     127.812        0               0          30.84  SZSE.000002        28.04          25.24               0             1           0    2017-09-19 00:00:00               1

 ​

4、get_trading_dates - 查询交易日列表

函数原型:

get_trading_dates(exchange, start_date, end_date)

示例:

get_trading_dates(exchange='SZSE', start_date='2017-01-01', end_date='2017-01-30')

输出:

['2017-01-03', '2017-01-04', '2017-01-05', '2017-01-06', ...]

 ​

5、get_previous_trading_date - 返回指定日期的上一个交易日

函数原型:

get_previous_trading_date(exchange, date)

示例:

get_previous_trading_date(exchange='SZSE', date='2017-05-01')

输出:

'2017-04-28'

 ​

6、get_next_trading_date - 返回指定日期的下一个交易日

函数原型:

get_next_trading_date(exchange, date)

示例:

get_next_trading_date(exchange='SZSE', date='2017-05-01')

输出:

'2017-05-02'

 ​

02行情数据

 ​

1、current - 查询当前行情快照

查询当前行情快照,返回tick数据。回测时,返回回测当前时间点的tick数据。

函数原型:

current(symbols, fields='')

示例:

current_data = current(symbols='SZSE.000001')

输出:

[{'symbol': 'SZSE.000001', 'open': 16.200000762939453, 'high': 16.920000076293945, 'low': 16.149999618530273, 'price': 16.559999465942383, 'quotes': [{'bid_p': 16.549999237060547, 'bid_v': 209200, 'ask_p': 16.559999465942383, 'ask_v': 296455}, {'bid_p': 16.540000915527344, 'bid_v': 188900, 'ask_p': 16.56999969482422, 'ask_v': 374405}, {'bid_p': 16.530000686645508, 'bid_v': 44900, 'ask_p': 16.579999923706055, 'ask_v': 187220}, {'bid_p': 16.520000457763672, 'bid_v': 20800, 'ask_p': 16.59000015258789, 'ask_v': 102622}, {'bid_p': 16.510000228881836, 'bid_v': 37700, 'ask_p': 16.600000381469727, 'ask_v': 337002}], 'cum_volume': 160006232, 'cum_amount': 2654379585.66, 'last_amount': 14153832.0, 'last_volume': 854700, 'trade_type': 7, 'created_at': datetime.datetime(2020, 10, 15, 15, 0, 3, tzinfo=tzfile('PRC'))}]

 ​

2、history - 查询历史行情

函数原型:

history(symbol, frequency, start_time, end_time, fields=None, skip_suspended=True,         fill_missing=None, adjust=ADJUST_NONE, adjust_end_time='', df=True)

示例:

history_data = history(symbol='SHSE.000300', frequency='1d', start_time='2010-07-28',  end_time='2017-07-30', fields='open, close, low, high, eob', adjust=ADJUST_PREV, df= True)

输出:

          open      close        low       high                       eob0     2796.4829  2863.7241  2784.1550  2866.4041 2010-07-28 00:00:00+08:001     2866.7720  2877.9761  2851.9961  2888.5991 2010-07-29 00:00:00+08:002     2871.4810  2868.8459  2844.6819  2876.1360 2010-07-30 00:00:00+08:003     2868.2791  2917.2749  2867.4500  2922.6121 2010-08-02 00:00:00+08:004     2925.2539  2865.9709  2865.7610  2929.6140 2010-08-03 00:00:00+08:00

 ​

3、history_n - 查询历史行情最新n条

函数原型:

history_n(symbol, frequency, count, end_time=None, fields=None, skip_suspended=True,           fill_missing=None, adjust=ADJUST_NONE, adjust_end_time='', df=False)

示例:

history_n_data = history_n(symbol='SHSE.600519', frequency='1d', count=100, end_time='2020-10-20 15:30:00', fields='symbol, open, close, low, high, eob', adjust=ADJUST_PREV, df=True)

输出:

 symbol       open  ...       high                       eob0   SHSE.600519  1350.2278  ...  1350.3265 2020-05-22 00:00:00+08:001   SHSE.600519  1314.6434  ...  1350.8010 2020-05-25 00:00:00+08:002   SHSE.600519  1354.0629  ...  1354.1321 2020-05-26 00:00:00+08:003   SHSE.600519  1343.3086  ...  1344.2970 2020-05-27 00:00:00+08:004   SHSE.600519  1322.5214  ...  1331.3878 2020-05-28 00:00:00+08:00

 ​

4、context.data - 查询订阅数据

函数原型:

context.data(symbol, frequency, count)

示例:

def init(context):    subscribe(symbols='SHSE.600519', frequency='60s', count=2)def on_bar(context,bars):    data = context.data(symbol='SHSE.600519', frequency='60s', count=1)

输出:

        symbol             eob                               bob              open     close     high         low         amount    pre_close    position    frequency    volume0    SHSE.600519    2020-12-21 09:31:00+08:00    2020-12-21 09:30:00+08:00    1840    1845.5    1845.5    1838.199951    210503484      0            0          60s      114365

 ​

03财务数据

 ​

1、get_fundamentals - 查询基本面数据

函数原型:

get_fundamentals(table, symbols, start_date, end_date, fields=None, filter=None, order_by=None, limit=1000, df=False)

示例:

取股票代码“SHSE.600000, SZSE.000001”, 离“2017-01-01”最近一个交易日的股票交易财务衍生表中“TCLOSE,NEGOTIABLEMV,TOTMKTCAP,TURNRATE,PELFY,PETTM,PEMRQ,PELFYNPAAEI,PETTMNPAAEI”字段的值:

get_fundamentals(table='trading_derivative_indicator', symbols='SHSE.600000, SZSE.000001', start_date='2017-01-01', end_date='2017-01-01', fields='TCLOSE,NEGOTIABLEMV,TOTMKTCAP,TURNRATE,PELFY,PETTM,PEMRQ,PELFYNPAAEI,PETTMNPAAEI', df=True)

输出:

  symbol       pub_date             end_date             NEGOTIABLEMV  PEMRQ    PELFYNPAAEI    PETTMNPAAEI  PELFY    TURNRATE    PETTM    TOTMKTCAP    TCLOSESHSE.600000  2016-12-30 00:00:00  2016-12-30 00:00:00     3.3261e+11    6.4605         7.0707         6.6184   6.925       0.0598   6.4746  3.50432e+11     16.21SZSE.000001  2016-12-30 00:00:00  2016-12-30 00:00:00     1.33144e+11   6.2604         7.1341         6.2644   7.1462      0.2068   6.8399  1.56251e+11      9.1

 ​

2、get_fundamentals_n - 查询基本面数据最新n条

函数原型:

get_fundamentals_n(table, symbols, end_date, fields=None, filter=None, order_by=None, count=1, df=False)

示例:

取股票代码“SHSE.600000, SZSE.000001”, 离“2017-01-01”最近3条(每个股票都有3条) 股票交易财务衍生表中“TCLOSE,NEGOTIABLEMV,TOTMKTCAP,TURNRATE,PELFY,PETTM,PEMRQ,PELFYNPAAEI,PETTMNPAAEI”字段的值:

get_fundamentals_n(table='trading_derivative_indicator', symbols='SHSE.600000, SZSE.000001', end_date='2017-01-01', count=3,           fields='TCLOSE,NEGOTIABLEMV,TOTMKTCAP,TURNRATE,PELFY,PETTM,PEMRQ,PELFYNPAAEI,PETTMNPAAEI',df=True)

输出:

   symbol        pub_date             end_date           TCLOSE   TOTMKTCAP    PETTM      TURNRATE     PETTMNPAAEI  PELFY    PELFYNPAAEI    NEGOTIABLEMV    PEMRQSZSE.000001  2016-12-30 00:00:00  2016-12-30 00:00:00      9.1   1.56251e+11   6.8399      0.2068         6.2644   7.1462         7.1341     1.33144e+11   6.2604SZSE.000001  2016-12-29 00:00:00  2016-12-29 00:00:00      9.08  1.55907e+11   6.8249      0.2315         6.2506   7.1305         7.1184     1.32851e+11   6.2466SZSE.000001  2016-12-28 00:00:00  2016-12-28 00:00:00      9.06  1.55564e+11   6.8098      0.2297         6.2369   7.1147         7.1027     1.32558e+11   6.2329SHSE.600000  2016-12-30 00:00:00  2016-12-30 00:00:00     16.21  3.50432e+11   6.4746      0.0598         6.6184   6.925          7.0707     3.3261e+11    6.4605SHSE.600000  2016-12-29 00:00:00  2016-12-29 00:00:00     16.07  3.47406e+11   6.4187      0.0578         6.5613   6.8652         7.0097     3.29737e+11   6.4047SHSE.600000  2016-12-28 00:00:00  2016-12-28 00:00:00     16.09  3.47838e+11   6.4267      0.0704         6.5694   6.8737         7.0184     3.30148e+11   6.4126

 ​

04指数成分股数据

 ​

1、get_constituents - 查询指数最新成份股

函数原型:

get_constituents(index, fields=None, df=False)

示例:

get_constituents(index='SHSE.000001', fields='symbol, weight', df=False)

输出:

[{'symbol': 'SHSE.603681', 'weight': 0.009999999776482582}, {'symbol': 'SHSE.601518', 'weight': 0.009999999776482582}, {'symbol': 'SHSE.600010', 'weight': 0.12999999523162842}···]

 ​
2、get_history_constituents - 查询指数成份股的历史数据

函数原型:

get_history_constituents(index, start_date=None, end_date=None)

示例:

get_history_constituents(index='SHSE.000001', start_date='2017-07-10')

输出:

constituents                                                    trade_date
{'SHSE.600527': 0.009999999776482582, 'SHSE.600461': 0.019999999552965164,···}     2017-07-31 00:00:00
{'SHSE.603966': 0.009999999776482582, 'SHSE.603960': 0.009999999776482582,···}     2017-08-31 00:00:00
···

 ​

05行业数据

 ​

1、get_industry - 查询行业股票列表

函数原型:

get_industry(code)

示例:

#返回所有以J6开头的行业代码对应的成分股(包括:J66,J67,J68,J69的成分股)
get_industry(code='j6')

输出:

['SHSE.600000', 'SHSE.600016', 'SHSE.600030',···]

 ​
以上就是今天的全部内容,如果想了解每个函数具体参数返回字段,可访问“帮助中心”-“Python SDK文档”-“API介绍”查看。

暂无评论

Looks like your connection to 掘金量化社区 - 量化交易者的交流社区 was lost, please wait while we try to reconnect.