概述

量化投资研究服务平台行情数据接口支持通过股票代码、指数板块代码获取。

[1]:
from __future__ import print_function, unicode_literals
import pandas as pd
import datetime
from data_provider.datafeed.quote_feed import QuoteFeed
[2]:
help(QuoteFeed)
Help on class QuoteFeed in module data_provider.datafeed.quote_feed:

class QuoteFeed(builtins.object)
 |  取行情数据的类, 包括股票和指数
 |
 |  1. 取股票行情:
 |      取股票行情一般可以通过3步,先初始化,然后load, 再get quote, e.g:
 |          qf = QuoteFeed(universe_ticker='000300.SH', begin_day=20160101,
 |                         end_day=20170822, tracking_freq=86400,
 |                         adjust_method='forward', load_large=False,
 |                         fill_back=False, columns=None, data_source=None)
 |          qf.load_feed()
 |          quote = qf.get_stock_quote()
 |
 |      也可以取股票panel, e.g:
 |          qf = QuoteFeed(universe_ticker='000300.SH', begin_day=20160101,
 |                         end_day=20170822, tracking_freq=86400,
 |                         adjust_method='forward',
 |                         load_large=False,
 |                         fill_back=False, columns=None, data_source=None)
 |          qf.load_feed()
 |          panel = qf.getBarPanel()
 |
 |  2. 取指数行情
 |      取指数行情一般也可以通过3步,先初始化,然后load, 再get quote, e.g:
 |          qf = QuoteFeed(universe_ticker='000300.SH', begin_day=20160101,
 |                         end_day=20170822, tracking_freq=86400,
 |                         columns=None, is_index=True, data_source=None)
 |          qf.load_feed()
 |          quote = qf.get_index_quote()
 |
 |  Methods defined here:
 |
 |  __init__(self, universe_ticker, begin_day, end_day, tracking_freq, adjust_method=None, columns=None, is_index=False, use_cache=True, fill_back=False, **kwargs)
 |      行情feed类
 |
 |      Args:
 |          begin_day: 取数据的开始日期,int格式或string类型,e.g. 20170717,'20170101'
 |          end_day:  取数据的截止日期,int格式或string类型,e.g. 20170717, '20170101'
 |          tracking_freq: on which the quotes are loaded.
 |          universe_ticker: 取数据的范围,当is_index=False时,可以是指数,e.g.'0003000.SH',
 |              也可以是ticker的list.如果是None,代表取全A股的行情;
 |              当is_index是True时,只能是指数
 |          adjust_method: 复权方式, 支持forward/None/complex,当tracking_freq
 |              小于86400时,不允许为complex
 |          is_index: 是否取指数行情, bool, 默认False, 为True时,adjust_method参数不起作用
 |          columns: 行情列,default None, 表示获取所有列
 |              否则获取['ticker', 'datetime', 'datetime_str'] + columns中的列
 |          use_cache: bool, 是否使用缓存,默认使用缓存
 |          fill_back: bool, 是否取回填行情,只对日行情有效(tracking_freq >= 86400)
 |          **kwargs: 关键字参数, 若kwargs.keys()包括'data_source',
 |              则data_source取传入的kwargs['data_source']
 |              否则data_source取data_provider.env.DATA_SOURCE
 |      Returns:
 |          None
 |
 |  del_cache_by_day(self, day)
 |      删除某天所生成的缓存
 |
 |      Args:
 |          folder: datetime, str or int date format of %Y%m%d
 |
 |      Returns:
 |          None
 |
 |  del_eariler_cach(self, days=5)
 |      删除几天前所生成的缓存
 |
 |      Args:
 |          days: int value for earier days, default 5
 |
 |      Returns:
 |          None
 |
 |  getBarPanel(self)
 |      获取行情数据的panel
 |
 |      Args:
 |          None
 |
 |      Returns:
 |          pandas.DataFrame, 三维DataFrame, index: ticker and datetime
 |              按行情columns分割
 |          可以使用.open, .close获取对应panel数据
 |
 |  get_index_quote(self)
 |      获取指数行情数据
 |
 |      Args:
 |          None
 |
 |      Returns:
 |          pandas.DataFrame, 行情数据
 |
 |  get_multiple_dividend_tinysoft(self, tickers, begin_day, end_day)
 |
 |  get_stock_quote(self)
 |      获取股票行情数据
 |
 |      Args:
 |          None
 |
 |      Returns:
 |          pandas.DataFrame, 行情数据
 |
 |  load_feed(self)
 |      执行行情获取
 |
 |      执行完成后,可通过get_stock_quote获取行情,通过getBarPanel获取panel
 |      Args:
 |          None
 |
 |      Returns:
 |          None
 |
 |  load_feed_by_index(self, index)
 |      装载指数行情
 |
 |      Args:
 |          index, str value of index code
 |
 |      Returns:
 |          None
 |
 |  load_feed_by_tickers(self, ticker_ls)
 |      装载股票行情
 |
 |      Args:
 |          None
 |
 |      Returns:
 |          None
 |
 |  set_cache_folder(self, folder)
 |      设置缓存目录
 |
 |      Args:
 |          folder: string of cache folder
 |
 |      Returns:
 |          None
 |
 |  set_should_write_cache(self, write)
 |      设置是否需要写入缓存
 |
 |      Args:
 |          write: bool value if shoud write cache
 |
 |      Returns:
 |          None
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)

参数说明:

  • begin_day: 取数据的开始日期,int或str,e.g. 20170717
  • end_day: 取数据的截止日期,int或str,e.g. 20170717
  • tracking_freq: int,行情频率,以秒为单位,如60表示分钟行情,86400表示日行情等
  • universe_ticker: 当is_index=False时表示取股票的范围,可以是指数,e.g. ‘0003000.SH’, 这表示指数内的所有股票的集合;也可以是ticker的集合,如[‘600000.SH’, ‘000001.SZ’]如果是None,代表取全A股的行情,当is_index=True时,则只能为str, 表示取该指数行情,如‘000300.SH’表示取沪深300指数行情
  • adjust_method: str,复权方式, 支持forward/None/complex,forward-前复权, None-不复权, complex-复杂复权(以截止时间为基准,向前复权)
  • is_index: 是否取指数行情, bool, 默认False, 为True时,adjust_method参数不起作用
  • columns: 行情列,default None, 表示获取所有列,否则获取[‘ticker’, ‘datetime’] + columns中的列
  • use_cache: bool, 是否使用缓存,默认使用缓存
  • fill_back: bool,是否进行行情回填,只对is_index=False且 tracking_freq >= 86400有效表示当股票停牌时, 该日行情的价格回填上一交易日
  • kwargs: 关键字参数, 若kwargs.keys()包括’data_source’, 则data_source取传入的kwargs[‘data_source’], 否则data_source取data_provider.env.DATA_SOURCE

列说明:

  • open: 开盘价
  • high: 最高价
  • low: 最低价
  • amount: 成交金额
  • close: 收盘价
  • amount: 成交金额
  • bought_amount: 主买金额
  • buy_vol: 主买量
  • lb: 量比
  • sold_amount: 主卖金额
  • volume: 成交量
  • w_buy: 委买
  • w_sale: 委卖
  • wb: 委比 委买/委卖( w_buy/w_sale)
  • datetime: 时间
  • ticker: 股票代码
  • datetiem_str: 字符串类型的时间
[3]:
# universe_ticker 可以是指数,e.g.'000902.SH',
# 也可以是ticker的list, e.g.['000001.SZ', '000002.SZ', '000009.SZ']
all_a_stock = "000902.SH"
begin_day = 20160101
end_day = 20170731
# tracking_freq, 频率,单位为秒,如86400为日
day_freq = 86400
# adjust_method,复权方式,支持'forward'、‘complex’和None
no_adjust_method = None
# load_large 是否大批量取行情,当股票数量较多时,可置为True, 速度更快
load_large = True

1、获取指数内股票未复权不回填日行情,数据源使用默认数据源

[4]:
hs00_quote = QuoteFeed(
    universe_ticker=["600000.SH"],
    begin_day=begin_day,
    end_day=end_day,
    tracking_freq=day_freq,
    use_cache=True,
)
hs00_quote.load_feed()
hs00_quote.get_stock_quote()[:5]
amount bought_amount buy_vol close datetime datetime_str high lb low open sale_vol sold_amount ticker volume w_buy w_sale wb
0 7.544258e+08 366130102.0 20503878.0 17.80 2016-01-04 20160104 18.28 0.976343 17.55 18.28 21736732.0 388295681.0 600000.SH 42240610.0 379149108.0 2.864072e+08 1.144544
1 1.034181e+09 523423264.5 29375869.0 17.96 2016-01-05 20160105 18.06 1.428983 17.40 17.51 28678924.0 510758209.5 600000.SH 58054793.0 427342935.0 6.846612e+08 0.125389
2 8.386674e+08 362600847.0 20241237.0 18.10 2016-01-06 20160106 18.14 1.107268 17.69 17.90 26531416.0 476066551.0 600000.SH 46772653.0 614546431.0 9.131011e+08 0.893159
3 1.995027e+08 100187910.0 5707305.0 17.51 2016-01-07 20160107 17.90 0.269362 17.40 17.80 5643174.0 99314792.0 600000.SH 11350479.0 52544953.0 4.030308e+07 2.973913
4 1.262105e+09 629830973.0 35952734.0 17.49 2016-01-08 20160108 17.88 1.929608 16.91 17.74 35965562.0 632274087.0 600000.SH 71918296.0 446440253.0 1.054792e+09 0.702181

2、获取股票列表前复权日行情

[5]:
tickers = ["000001.SZ", "600000.SH"]
qfq_quote = QuoteFeed(
    universe_ticker=tickers,
    begin_day=begin_day,
    end_day=end_day,
    tracking_freq=day_freq,
    adjust_method="forward",
)
qfq_quote.load_feed()
qfq_quote.get_stock_quote()[:5]
amount bought_amount buy_vol close datetime datetime_str high lb low open sale_vol sold_amount ticker volume w_buy w_sale wb
0 6.603762e+08 2.463796e+08 2.521074e+07 9.020166 2016-01-04 20160104 9.603500 0.983080 8.936833 9.578500 4.240900e+07 4.139965e+08 000001.SZ 6.761975e+07 2.320391e+09 1.122328e+09 0.000000
1 7.544258e+08 3.661301e+08 2.932055e+07 11.833567 2016-01-04 20160104 12.169231 0.976343 11.658741 12.169231 3.108353e+07 3.882957e+08 600000.SH 6.040407e+07 5.421832e+08 4.095623e+08 1.144544
2 1.034181e+09 5.234233e+08 4.200749e+07 11.945455 2016-01-05 20160105 12.015385 1.428983 11.553846 11.630769 4.101086e+07 5.107582e+08 600000.SH 8.301835e+07 6.111004e+08 9.790655e+08 0.125389
3 7.555314e+08 3.707608e+08 3.903745e+07 9.078500 2016-01-05 20160105 9.220166 1.094637 8.870166 8.970166 4.055494e+07 3.847705e+08 000001.SZ 7.959240e+07 2.789444e+09 1.963425e+09 2.857381
4 5.916985e+08 2.855352e+08 2.985179e+07 9.186833 2016-01-06 20160106 9.211833 0.898288 9.070166 9.095166 3.203299e+07 3.061633e+08 000001.SZ 6.188478e+07 3.928046e+09 3.378212e+09 0.892001

3、获取股票列表分钟行情

[6]:
tickers = ["600000.SH"]
min_quote = QuoteFeed(
    universe_ticker=tickers, begin_day=begin_day, end_day=end_day, tracking_freq=300
)
min_quote.load_feed()
min_quote.get_stock_quote()[:10]
datetime open high low close amount volume bought_amount sold_amount ticker
0 2016-01-04 09:35:00 18.28 18.28 18.15 18.19 21085642.0 1157800.0 8555718.0 12529924.0 600000.SH
1 2016-01-04 09:40:00 18.18 18.19 18.11 18.15 17588200.0 969100.0 10929829.0 6658371.0 600000.SH
2 2016-01-04 09:45:00 18.17 18.17 18.13 18.15 13244534.0 730080.0 7364844.0 5879690.0 600000.SH
3 2016-01-04 09:50:00 18.15 18.15 18.03 18.05 27035544.0 1494465.0 7324161.0 19711383.0 600000.SH
4 2016-01-04 09:55:00 18.04 18.04 17.88 18.00 56665209.0 3155535.0 20770423.0 35894786.0 600000.SH
5 2016-01-04 10:00:00 17.96 18.00 17.90 17.96 43613805.0 2431020.0 16775726.0 26838079.0 600000.SH
6 2016-01-04 10:05:00 17.97 17.97 17.85 17.85 27770845.0 1551100.0 9348143.0 18422702.0 600000.SH
7 2016-01-04 10:10:00 17.88 17.88 17.70 17.74 50399010.0 2837111.0 32516628.0 17882382.0 600000.SH
8 2016-01-04 10:15:00 17.73 17.74 17.55 17.62 53404911.0 3027889.0 20781621.0 32623290.0 600000.SH
9 2016-01-04 10:20:00 17.62 17.70 17.62 17.70 29755783.0 1685271.0 19219003.0 10536780.0 600000.SH

4、获取股票列表小时行情

[7]:
hour_quote = QuoteFeed(
    universe_ticker=tickers, begin_day=begin_day, end_day=end_day, tracking_freq=3600
)
hour_quote.load_feed()
hour_quote.get_stock_quote()[:5]
datetime open high low close amount volume bought_amount sold_amount ticker
0 2016-01-04 10:00:00 18.28 18.28 17.88 17.96 179232934.0 9938000.0 71720701.0 107512233.0 600000.SH
1 2016-01-04 11:00:00 17.97 17.97 17.55 17.77 273005839.0 15391306.0 147871807.0 125134032.0 600000.SH
2 2016-01-04 11:30:00 17.77 18.02 17.68 18.02 132792569.0 7449753.0 66833908.0 65958661.0 600000.SH
3 2016-01-04 14:00:00 18.00 18.02 17.70 17.73 169394441.0 9461551.0 79703686.0 89690755.0 600000.SH
4 2016-01-04 15:00:00 17.73 17.80 17.73 17.80 0.0 0.0 0.0 0.0 600000.SH

5、获取股票周行情

[8]:
tickers = ["600000.SH"]
week_quote = QuoteFeed(
    universe_ticker=tickers,
    begin_day=begin_day,
    end_day=end_day,
    tracking_freq=86400 * 5,
)
week_quote.load_feed()
week_quote.get_stock_quote()[:5]
datetime open high low close amount volume bought_amount sold_amount ticker
0 2016-01-08 18.28 18.28 16.91 17.49 4.088882e+09 230336831.0 1.982173e+09 2.106709e+09 600000.SH
1 2016-01-13 17.21 17.90 16.96 17.39 3.357935e+09 193420901.0 1.559786e+09 1.798148e+09 600000.SH
2 2016-01-18 17.00 18.15 16.89 17.68 2.385556e+09 134290978.0 1.166374e+09 1.219182e+09 600000.SH
3 2016-01-22 17.78 17.88 16.49 17.02 2.442325e+09 141980228.0 1.121794e+09 1.320531e+09 600000.SH
4 2016-01-28 17.00 17.17 15.88 16.58 2.430682e+09 145032224.0 1.199273e+09 1.231409e+09 600000.SH

6、复杂复权

[9]:
complex_quote = QuoteFeed(
    universe_ticker=["600000.SH"],
    begin_day=20170101,
    end_day=20170707,
    tracking_freq=86400,
    adjust_method="complex",
)
complex_quote.load_feed()
complex_quote.get_stock_quote()[:5]
amount bought_amount buy_vol close datetime datetime_str high lb low open sale_vol sold_amount ticker volume w_buy w_sale wb
0 265043268.0 148671794.0 9101853.5 12.384615 2017-01-03 20170103 12.492308 1.274434 12.284615 12.315385 7135271.5 116371474.0 600000.SH 16237125.0 9.695746e+08 9.325709e+08 1.314533
1 482612222.0 213954051.5 13144423.0 12.407692 2017-01-04 20170104 12.423077 2.265480 12.292308 12.376923 16514311.0 268658170.5 600000.SH 29658734.0 3.562279e+09 1.208949e+09 0.507936
2 431449126.0 225027049.0 13785917.0 12.384615 2017-01-05 20170105 12.446154 1.565268 12.338462 12.384615 12651729.0 206422077.0 600000.SH 26437646.0 4.645900e+09 2.211675e+09 1.746183
3 278864536.0 97456343.0 6009279.0 12.292308 2017-01-06 20170106 12.384615 0.891453 12.253846 12.384615 11186319.0 181408193.0 600000.SH 17195598.0 2.751586e+09 9.552984e+08 9.237467
4 241579598.0 100924678.0 6225352.0 12.307692 2017-01-09 20170109 12.376923 0.732319 12.253846 12.338462 8683393.0 140654920.0 600000.SH 14908745.0 2.444795e+09 1.631682e+09 4.035549

7、获取行情panel

[10]:
qf = QuoteFeed(
    universe_ticker=tickers,
    begin_day=begin_day,
    end_day=end_day,
    tracking_freq=86400,
    adjust_method="complex",
)
qf.load_feed()
panel = qf.getBarPanel()
print(panel.close[:5])
print()
print(panel.open[:5])
ticker      600000.SH
datetime
2016-01-04  11.933566
2016-01-05  12.045455
2016-01-06  12.143357
2016-01-07  11.730769
2016-01-08  11.716783

ticker      600000.SH
datetime
2016-01-04  12.269231
2016-01-05  11.730769
2016-01-06  12.003497
2016-01-07  11.933566
2016-01-08  11.891608

8、获取指数日行情

[11]:
tickers = "000016.SH"
week_quote = QuoteFeed(
    universe_ticker=tickers,
    begin_day=begin_day,
    end_day=end_day,
    tracking_freq=86400,
    is_index=True,
)
week_quote.load_feed()
week_quote.get_index_quote()[:5]
amount bought_amount close datetime datetime_str high low open sold_amount ticker volume
0 3.775372e+10 0.0 2270.4609 2016-01-04 20160104 2417.0246 2269.9671 2417.0246 0.0 000016.SH 3.462085e+09
1 5.104514e+10 0.0 2288.1127 2016-01-05 20160105 2309.5937 2223.0521 2230.0800 0.0 000016.SH 4.807596e+09
2 3.989499e+10 0.0 2317.6465 2016-01-06 20160106 2321.1692 2274.4683 2284.9228 0.0 000016.SH 3.808882e+09
3 1.147481e+10 0.0 2180.5889 2016-01-07 20160107 2281.9337 2171.3153 2281.8810 0.0 000016.SH 1.108158e+09
4 5.379758e+10 0.0 2219.6119 2016-01-08 20160108 2257.4100 2154.5816 2225.4754 0.0 000016.SH 5.152593e+09

9、获取指数分钟行情

[12]:
tickers = "000016.SH"
week_quote = QuoteFeed(
    universe_ticker=tickers,
    begin_day=begin_day,
    end_day=end_day,
    tracking_freq=60,
    is_index=True,
)
week_quote.load_feed()
week_quote.get_index_quote()[:5]
amount bought_amount close high low open sold_amount volume ticker datetime datetime_str
0 826161301.0 0.0 2411.5378 2417.0246 2411.1922 2417.0246 0.0 727880.0 000016.SH 2016-01-04 09:31:00 20160104093100
1 297446315.0 0.0 2412.1536 2412.1536 2410.0082 2411.0769 0.0 227362.0 000016.SH 2016-01-04 09:32:00 20160104093200
2 210433010.0 0.0 2412.2863 2412.7298 2411.1478 2411.1478 0.0 183821.0 000016.SH 2016-01-04 09:33:00 20160104093300
3 187062067.0 0.0 2414.7728 2414.7728 2413.1286 2413.1593 0.0 155147.0 000016.SH 2016-01-04 09:34:00 20160104093400
4 255725786.0 0.0 2415.1714 2415.4165 2414.5434 2415.4165 0.0 210179.0 000016.SH 2016-01-04 09:35:00 20160104093500