量化交易 数据获取接口

共 16113字,需浏览 33分钟

 ·

2022-02-18 16:44


获取数据

概述

工欲善其事, 必先利其器. 想做量化, 数据是基本! 本篇我们会介绍如何获取数据.

数据接口种类

  • 获取指定行业, 板块股票列表- history_bars: 指定股票合约历史数据- get_fundamentals: 查询财务数据

行业 & 板块 股票类表

股票代码 & 代码补齐

RiceQuant 上的股票

上证: 深证:

股票自动搜索及补全

  • Windows 用户: 输入 ctrl + i- Mac 用户: 输入 command + i 当我们输入了这个组合键之后, RiceQuant 在线 IDE 就会进入代码搜索和自动完成模式. 接着我们可以输入任何一种进行搜索和自动补全.
  • 股票数字代码: 自动补全为股票数字代码, 比如 “000001.XSHE”- 股票中文全称: 自动补全为股票中文全称, 比如 “贵州茅台”- 股票拼音缩写: 自动补全为股票中文全称, 因为股票拼音缩写并不是独一无二的, 比如 ZSDC 补全为 “招商地产”

获取行业

获取某行业股票数据, 代码如下:

industry (code)

返回:

属于该行业的股票 order_book_id 或 order_book_id list.

范例:

[In]
industry('A01')
[Out]
['600540.XSHG''600371.XSHG''600359.XSHG''600506.XSHG',...]
[In]
industry(industry_code.A01)
[Out]
['600540.XSHG''600371.XSHG''600359.XSHG''600506.XSHG',...]

获取板块

获取某板块股票列表, 代码如下:

sector(code, market='cn')

返回:

属于该板块的股票 order_book_id 或 order_book_id list.

范例:

[In]sector('Energy')
[Out]
['300023.XSHE''000571.XSHE''600997.XSHG''601798.XSHG''603568.XSHG', .....]
[In]sector(sector_code.Energy)
[Out]
['300023.XSHE''000571.XSHE''600997.XSHG''601798.XSHG''603568.XSHG', .....]

支持的行业如下, 想要了解全球行业标准划分标准参考全球行业标准分类:

代码展示

def init(context):
    # 在context中保存全局变量
    context.s1 = "000001.XSHE"
    context.stock = "000007.XSHE"

    # 获取计算机通信行业的一些公司股票代码
    context.stock_list = industry("C39")

    # 获取能源板块的股票代码
    context.sector_list = sector("energy")

    # 获取沪深300的指数股票
    context.index_list = index_components("000300.XSHG")

    # 调试输出
    logger.info(context.stock_list)
    logger.info(context.sector_list)
    logger.info(context.index_list)
   
输出结果:
2016-01-03
INFO
['300847.XSHE''601138.XSHG''688183.XSHG''688138.XSHG''000810.XSHE''603380.XSHG''600363.XSHG''600980.XSHG''300
211.XSHE'
'600870.XSHG''688181.XSHG''600353.XSHG''688081.XSHG''000823.XSHE''600745.XSHG''002308.XSHE''600839.X
SHG'
'002528.XSHE''603516.XSHG''300666.XSHE''688123.XSHG''002222.XSHE''002841.XSHE''300078.XSHE''688418.XSHG',
'002992.XSHE''688020.XSHG''603002.XSHG''300205.XSHE''300390.XSHE''000066.XSHE''300046.XSHE''688208.XSHG''688
027.XSHG'
'300213.XSHE''600405.XSHG''600171.XSHG''300455.XSHE''300077.XSHE''002983.XSHE''002414.XSHE''000687.X
SHE'
'002806.XSHE''300739.XSHE''000050.XSHE''300042.XSHE''300458.XSHE''002888.XSHE''300686.XSHE''002993.XSHE',
'300939.XSHE''600764.XSHG''002156.XSHE''603160.XSHG''600584.XSHG''603803.XSHG''002897.XSHE''300327.XSHE''300
811.XSHE'
'300456.XSHE''300548.XSHE''605118.XSHG''002660.XSHE''002681.XSHE''300831.XSHE''002089.XSHE''002161.X
SHE'
'300088.XSHE''002241.XSHE''002115.XSHE''688195.XSHG''300752.XSHE''002792.XSHE''688079.XSHG''688667.XSHG',
'603023.XSHG''300835.XSHE''002655.XSHE''002579.XSHE''002180.XSHE''603019.XSHG''300220.XSHE''002119.XSHE''603
996.XSHG'
'002281.XSHE''688005.XSHG''300590.XSHE''002519.XSHE''688288.XSHG''300115.XSHE''601869.XSHG''300479.X
SHE'
'300083.XSHE''300079.XSHE''688665.XSHG''688608.XSHG''300303.XSHE''002402.XSHE''002456.XSHE''002866.XSHE',
'300566.XSHE''002138.XSHE''300120.XSHE''300351.XSHE''000413.XSHE''300155.XSHE''300940.XSHE''603986.XSHG''002
214.XSHE'
'002796.XSHE''600485.XSHG''300270.XSHE''688609.XSHG''002475.XSHE''300602.XSHE''300516.XSHE''300353.X
SHE'
'605058.XSHG''300514.XSHE''002925.XSHE''603773.XSHG''002426.XSHE''002829.XSHE''688696.XSHG''600183.XSHG',
'000801.XSHE''002296.XSHE''002376.XSHE''300709.XSHE''002782.XSHE''600330.XSHG''300162.XSHE''002351.XSHE''688
311.XSHG'
'002383.XSHE''000063.XSHE''600151.XSHG''000977.XSHE''002017.XSHE''000021.XSHE''002937.XSHE''002415.X
SHE'
'000725.XSHE''688080.XSHG''000100.XSHE''688019.XSHG''002231.XSHE''688002.XSHG''002055.XSHE''300308.XSHE',
'300691.XSHE''002835.XSHE''600288.XSHG''002388.XSHE''002815.XSHE''603595.XSHG''300102.XSHE''600562.XSHG''300
698.XSHE'
'300397.XSHE''300656.XSHE''300502.XSHE''300627.XSHE''300301.XSHE''002960.XSHE''003015.XSHE''000586.X
SHE'
'300555.XSHE''002413.XSHE''688025.XSHG''002179.XSHE''300916.XSHE''603115.XSHG''688008.XSHG''300546.XSHE',
'002972.XSHE''002137.XSHE''002025.XSHE''600898.XSHG''300782.XSHE''002861.XSHE''002387.XSHE''002952.XSHE''300
903.XSHE'
'688396.XSHG''600775.XSHG''300065.XSHE''300657.XSHE''603328.XSHG''603528.XSHG''002192.XSHE''002079.X
SHE'
'002045.XSHE''300708.XSHE''605218.XSHG''300956.XSHE''688260.XSHG''600776.XSHG''300531.XSHE''600345.XSHG',
'002587.XSHE''300114.XSHE''600100.XSHG''300884.XSHE''300139.XSHE''603005.XSHG''600260.XSHG''603678.XSHG''300
629.XSHE'
'300227.XSHE''002583.XSHE''300919.XSHE''600118.XSHG''603106.XSHG''300282.XSHE''002217.XSHE''300256.X
SHE'
'002729.XSHE''300647.XSHE''603236.XSHG', ...
2016-01-03
INFO
['600028.XSHG''000554.XSHE''000159.XSHE''600397.XSHG''002221.XSHE''600583.XSHG''601918.XSHG''603113.XSHG''600
725.XSHG'
'002259.XSHE''601225.XSHG''000968.XSHE''002629.XSHE''601011.XSHG''603353.XSHG''600508.XSHG''600777.X
SHG'
'000571.XSHE''600123.XSHG''603619.XSHG''600871.XSHG''601088.XSHG''600740.XSHG''002128.XSHE''300839.XSHE',
'002554.XSHE''000937.XSHE''600968.XSHG''600688.XSHG''000723.XSHE''002278.XSHE''300309.XSHE''000983.XSHE''000
096.XSHE'
'002490.XSHE''601699.XSHG''601898.XSHG''002828.XSHE''600387.XSHG''000852.XSHE''300084.XSHE''600403.X
SHG'
'300164.XSHE''000059.XSHE''600758.XSHG''600121.XSHG''600256.XSHG''601808.XSHG''600188.XSHG''601001.XSHG',
'002353.XSHE''688377.XSHG''603727.XSHG''600971.XSHG''002207.XSHE''000698.XSHE''601101.XSHG''000552.XSHE''000
780.XSHE'
'601857.XSHG''601015.XSHG''300471.XSHE''600989.XSHG''601666.XSHG''603800.XSHG''600759.XSHG''300157.X
SHE'
'300540.XSHE''603798.XSHG''600792.XSHG''601798.XSHG''000637.XSHE''600997.XSHG''603036.XSHG''300191.XSHE',
'600348.XSHG''600395.XSHG']
2016-01-03
INFO
['600030.XSHG''002153.XSHE''002065.XSHE''002594.XSHE''600332.XSHG''601016.XSHG''600739.XSHG''600010.XSHG''600
588.XSHG'
'601216.XSHG''601600.XSHG''601818.XSHG''601333.XSHG''000983.XSHE''600867.XSHG''600028.XSHG''600549.X
SHG'
'601099.XSHG''000793.XSHE''002739.XSHE''600048.XSHG''002252.XSHE''600718.XSHG''601958.XSHG''002456.XSHE',
'600958.XSHG''600827.XSHG''600196.XSHG''600837.XSHG''000027.XSHE''600021.XSHG''600104.XSHG''600166.XSHG''000
568.XSHE'
'600309.XSHG''601377.XSHG''000002.XSHE''000826.XSHE''000402.XSHE''000415.XSHE''000333.XSHE''000917.X
SHE'
'002736.XSHE''000413.XSHE''002202.XSHE''002399.XSHE''300146.XSHE''600023.XSHG''600489.XSHG''601991.XSHG',
'601166.XSHG''000712.XSHE''300002.XSHE''601788.XSHG''600038.XSHG''600783.XSHG''601919.XSHG''002410.XSHE''000
825.XSHE'
'600031.XSHG''000400.XSHE''000001.XSHE''601668.XSHG''601688.XSHG''601866.XSHG''600276.XSHG''600518.X
SHG'
'601989.XSHG''300104.XSHE''002081.XSHE''601555.XSHG''600703.XSHG''002500.XSHE''600066.XSHG''000063.XSHE',
'002450.XSHE''600068.XSHG''601006.XSHG''600406.XSHG''000629.XSHE''600369.XSHG''601988.XSHG''300017.XSHE''600
085.XSHG'
'601288.XSHG''601106.XSHG''601179.XSHG''000883.XSHE''600893.XSHG''601238.XSHG''601158.XSHG''600011.X
SHG'
'600674.XSHG''600016.XSHG''600570.XSHG''600317.XSHG''300070.XSHE''300144.XSHE''000768.XSHE''300015.XSHE',
'000060.XSHE''601872.XSHG''000686.XSHE''603993.XSHG''600485.XSHG''600999.XSHG''600153.XSHG''601618.XSHG''600
663.XSHG'
'600372.XSHG''601117.XSHG''000876.XSHE''002007.XSHE''002422.XSHE''300027.XSHE''600373.XSHG''000581.X
SHE'
'000338.XSHE''600583.XSHG''601328.XSHG''600717.XSHG''603288.XSHG''600642.XSHG''000503.XSHE''601186.XSHG',
'600115.XSHG''000725.XSHE''000538.XSHE''600050.XSHG''600900.XSHG''600177.XSHG''600150.XSHG''600383.XSHG''000
999.XSHE'
'601898.XSHG''600863.XSHG''300124.XSHE''600633.XSHG''002465.XSHE''300003.XSHE''600350.XSHG''601800.X
SHG'
'002142.XSHE''000009.XSHE''601111.XSHG''600170.XSHG''601899.XSHG''600895.XSHG''600795.XSHG''600221.XSHG',
'600839.XSHG''000630.XSHE''000792.XSHE''000831.XSHE''000963.XSHE''600208.XSHG''601992.XSHG''600705.XSHG''601
018.XSHG'
'601398.XSHG''002292.XSHE''000046.XSHE''603885.XSHG''600600.XSHG''000166.XSHE''601009.XSHG''001979.X
SHE'
'600188.XSHG''600690.XSHG''000157.XSHE''000776.XSHE''601766.XSHG''002304.XSHE''000898.XSHE''300058.XSHE',
'600018.XSHG''600015.XSHG''601118.XSHG''601225.XSHG''600029.XSHG''600352.XSHG''000039.XSHE''601231.XSHG''002
008.XSHE'
'300315.XSHE''600009.XSHG''000100.XSHE''000750.XSHE''600019.XSHG''600100.XSHG''600252.XSHG''600887.X
SHG'
'601098.XSHG''300133.XSHE''000858.XSHE''601928.XSHG''000539.XSHE''002470.XSHE''600111.XSHG''601969.XSHG', ...


获取股票合约数据

某一合约历史数据

history_bars(order_book_id, bar_count, frequency, fields=None, skip_suspended=True, include_now=False)

获取指定合约的历史行情, 同时支持日以及分钟历史数据. 不能再 init 中调用.

参数:

通过 bar_dict 获取

bar_dict 对象也可以获取行情数据: 只能获取当前运行日期的, 不能获取之前日期.

Bar 对象 范例:

logger.info(bar_dict[context.s1].close)

代码展示

def handle_bar(context, bar_dict):
    # 开始编写你的主要的算法逻辑

    # bar_dict[order_book_id] 可以拿到某个证券的bar信息
    # context.portfolio 可以拿到现在的投资组合信息

    # 使用order_shares(id_or_ins, amount)方法进行落单

    TODO: 开始编写你的算法吧!
    # 获取从当前提取运行开始之前5天的行情数据
    history_1 = history_bars(context.s1, 5, "1d", ["close""open"])

    # 调试输出
    logger.info(history_1)
 
输出结果:
 2016-01-04
INFO
[(11.98, 12.43) (12.09, 11.99) (12.1 , 12.09) (11.99, 12.1 )
(11.33, 12. )]
2016-01-05
INFO
[(12.09, 11.99) (12.1 , 12.09) (11.99, 12.1 ) (11.33, 12. )
(11.4 , 11.27)]
2016-01-06
INFO
[(12.1 , 12.09) (11.99, 12.1 ) (11.33, 12. ) (11.4 , 11.27)
(11.53, 11.42)]
2016-01-07
INFO
[(11.99, 12.1 ) (11.33, 12. ) (11.4 , 11.27) (11.53, 11.42)
(10.94, 11.41)]
2016-01-08
INFO
[(11.33, 12. ) (11.4 , 11.27) (11.53, 11.42) (10.94, 11.41)
(11.12, 11.21)]
2016-01-11
INFO
[(11.4 , 11.27) (11.53, 11.42) (10.94, 11.41) (11.12, 11.21)
(10.76, 11. )]
2016-01-12
INFO
[(11.53, 11.42) (10.94, 11.41) (11.12, 11.21) (10.76, 11. )
(10.81, 10.83)]
2016-01-13
INFO
[(10.94, 11.41) (11.12, 11.21) (10.76, 11. ) (10.81, 10.83)
(10.71, 10.89)]
2016-01-14
INFO
[(11.12, 11.21) (10.76, 11. ) (10.81, 10.83) (10.71, 10.89)
(10.77, 10.59)]
2016-01-15
INFO
[(10.76, 11. ) (10.81, 10.83) (10.71, 10.89) (10.77, 10.59)
(10.46, 10.66)]
2016-01-18
INFO
[(10.81, 10.83) (10.71, 10.89) (10.77, 10.59) (10.46, 10.66)
(10.41, 10.34)]
2016-01-19
INFO
[(10.71, 10.89) (10.77, 10.59) (10.46, 10.66) (10.41, 10.34)
(10.71, 10.45)]
2016-01-20
INFO
[(10.77, 10.59) (10.46, 10.66) (10.41, 10.34) (10.71, 10.45)
(10.54, 10.7 )]
2016-01-21
INFO
[(10.46, 10.66) (10.41, 10.34) (10.71, 10.45) (10.54, 10.7 )
(10.32, 10.48)]
2016-01-22
INFO
[(10.41, 10.34) (10.71, 10.45) (10.54, 10.7 ) (10.32, 10.48)
(10.4 , 10.4 )]
2016-01-25
INFO
[(10.71, 10.45) (10.54, 10.7 ) (10.32, 10.48) (10.4 , 10.4 )
(10.37, 10.4 )]
2016-01-26
INFO
[(10.54, 10.7 ) (10.32, 10.48) (10.4 , 10.4 ) (10.37, 10.4 )
( 9.87, 10.32)]
2016-01-27
INFO
[(10.32, 10.48) (10.4 , 10.4 ) (10.37, 10.4 ) ( 9.87, 10.32)
( 9.88, 9.93)]
2016-01-28
INFO
[(10.4 , 10.4 ) (10.37, 10.4 ) ( 9.87, 10.32) ( 9.88, 9.93)
( 9.69, 9.82)]
2016-01-29
INFO
[(10.37, 10.4 ) ( 9.87, 10.32) ( 9.88, 9.93) ( 9.69, 9.82)
(10. , 9.74)]
2016-02-01
...

获取财务数据

查询财务数据

get_fundamentals - 查询财务数据

get_fundamentals(query, entry_date, interval=None, report_quarter=False, expect_df=False)

获取历史财务数据表格. 目前支持中国市场超过 400 个指标, 具体请参考财务数据文档 . 目前仅支持中国市场. 需要注意, 在 RiceQuant 上查询基本面数据时, 我们是以所有年报的发布日期 (announcement date) 为准, 因为只有财报发布后才成为市场上公开可以获取的数据.

参数:

|参数|类型|说明 |------ |query|SQLAlchemyQueryObject|SQLAlchemy 的 Query 对象。其中可在’query’内填写需要查询的指标,'filter’内填写数据过滤条件。具体可参考 sqlalchemy’s query documentation 学习使用更多的方便的查询语句。从数据科学家的观点来看,sqlalchemy 的使用比 sql 更加简单和强大 |entry_date|str, datetime.date, datetime.datetime, pandasTimestamp|查询财务数据的基准开始日期 |interval|str|查询财务数据的间隔。例如,填写’5y’,则代表从 entry_date 开始(包括 entry_date)回溯 5 年,返回数据时间以年为间隔。‘d’ - 天,‘w’ - 周,‘m’ - 月, ‘q’ - 季,‘y’ - 年 |report_quarter|bool|是否显示报告期,默认为不显示。‘Q1’ - 一季报,‘Q2’ - 半年报,‘Q3’ - 三季报,‘Q4’ - 年报 |expect_df|boolean|默认返回原有的 Panel 数据结构。如果调为真,则返回 pandas dataframe

返回 pandas DataPanel - 财务数据查询结果.

范例

[In]
dp = get_fundamentals(query(fundamentals.eod_derivative_indicator.pe_ratio).filter(fundamentals.stockcode == '000001.XSHE'), '2016-08-01','4q' ,report_quarter = True)
[In]
dp.minor_xs('000001.XSHE')
[Out]
  report_quarter pe_ratio
2016-08-01 2016-Q1  7.0768
2016-04-29 2016-Q1  6.7755
2016-01-29 2015-Q3  6.5492
2015-10-29 2015-Q3  7.3809

过滤指标条件

  • query().filter: 过滤大小- query().order_by: 排序- query.limit(): 限制数量- fundamentals.stockcode.in_(): 在指定股票池当中过滤 案例:
q = query(
            fundamentals.eod_derivative_indicator.pe_ratio,  # 获取市盈率
            fundamentals.eod_derivative_indicator.pcf_ratio  # 获取市现率 
        ).filter(
            fundamentals.eod_derivative_indicator.pe_ratio > 20,  # 过滤市盈率大于20
            fundamentals.eod_derivative_indicator.pcf_ratio < 50  # 过滤市现率小于50
        ).order_by(
            fundamentals.eod_derivative_indicator.pe_ratio # 用盈率排序, 从小到大
        ).limit(10)  # 取10个

代码展示

def handle_bar(context, bar_dict):
    # 开始编写你的主要的算法逻辑

    # bar_dict[order_book_id] 可以拿到某个证券的bar信息
    # context.portfolio 可以拿到现在的投资组合信息

    # 使用order_shares(id_or_ins, amount)方法进行落单

    TODO: 开始编写你的算法吧!
    # 创建一个查询语句 (pb_ratio: 市净率)
    q = query(
            fundamentals.eod_derivative_indicator.pb_ratio
        )

    # 回测不需要日期, 默认当天的数据
    fund = get_fundamentals(q)

    # 转置
    fund = fund.T

    # 调试输出
    logger.info(fund)
    
输出结果:
 2016-01-04
INFO
pb_ratio
000010.XSHE 3.8286
000014.XSHE 6.4342
000006.XSHE 3.537
000008.XSHE 9.7601
000012.XSHE 3.6253
000011.XSHE 4.121
000007.XSHE 14.8823
000009.XSHE 6.3938
000001.XSHE 1.0623
000002.XSHE 2.695
000004.XSHE 47.2814
000005.XSHE 9.2519
000016.XSHE 5.9121
000017.XSHE 651.399
000018.XSHE 17.1477
000019.XSHE 5.4866
000021.XSHE 3.4096
000022.XSHE 2.8029
000023.XSHE 11.0294
000020.XSHE 27.2062
000025.XSHE 28.7225
000026.XSHE 2.7981
000027.XSHE 1.7913
000028.XSHE 4.3987
000029.XSHE 5.6056
000030.XSHE 2.816
000031.XSHE 4.2624
000032.XSHE 6.5288
000033.XSHE 355.578
000034.XSHE 53.5046
... ...
300426.XSHE 15.1151
002736.XSHE 3.6212
300488.XSHE 9.2783
300434.XSHE 11.4555
603398.XSHG 16.4336
603808.XSHG 5.3673
300439.XSHE 10.5654
603223.XSHG 7.9353
300450.XSHE 18.5618
300446.XSHE 16.6062
300493.XSHE 10.9242
300427.XSHE 11.9498
300487.XSHE 8.0302
300459.XSHE 7.8616
603968.XSHG 5.5646
300457.XSHE 15.0464
300451.XSHE 20.5081
300394.XSHE 12.6802
603669.XSHG 8.1029
300481.XSHE 10.3629
300458.XSHE 12.4225
300413.XSHE 10.0609
300408.XSHE 7.5259
603996.XSHG 4.3534
000166.XSHE 3.1675
300498.XSHE 7.4478
601155.XSHG 2.8596
001979.XSHE 3.3113
001872.XSHE 2.8029
001914.XSHE 2.0273
[2811 rows x 1 columns]
2016-01-05
INFO
pb_ratio
000010.XSHE 3.4495
000014.XSHE 5.791
000006.XSHE 3.1898
000008.XSHE 8.7908
000012.XSHE 3.2641
000011.XSHE 3.7094
000007.XSHE 14.8823
000009.XSHE 5.753
000001.XSHE 1.0038
000002.XSHE 2.695
000004.XSHE 42.5533
000005.XSHE 8.3304
000016.XSHE 5.3217
000017.XSHE 586.259
000018.XSHE 15.4643
000019.XSHE 4.9428
000021.XSHE 3.0675
000022.XSHE 2.5256
000023.XSHE 11.0294
000020.XSHE 24.5874
000025.XSHE 25.8496
000026.XSHE 2.518
000027.XSHE 1.6142
000028.XSHE 4.3987
000029.XSHE 5.0459
000030.XSHE 2.5426
000031.XSHE 3.8468
000032.XSHE 5.8759
000033.XSHE 355.578
000034.XSHE 53.5046
...

定时获取数据

每天运行

scheduler.run_daily

scheduler.run_daily(function)

参数:

|参数|类型|注释 |------ |function|function|使传入的function每日运行。注意,function 函数一定要包含(并且只能包含)context, bar_dict 两个输入参数

每周运行

scheduler.run_weekly

scheduler.run_weekly(function, weekday=x, tradingday=t)

注意:

  • tradingday 中的负数表示倒数- tradingday 表示交易日. 如某周只有四个交易日, 则此周的tradingday=4 与 tradingday=-1 表示同一天- weekday 和 tradingday 不能同时使用

每月运行

scheduler.run_monthly

scheduler.run_monthly(function, tradingday=t)

注意:

  • tradingday 中的负数表示倒数- tradingday 表示交易日. 如某周只有四个交易日, 则此周的tradingday=4 与 tradingday=-1 表示同一天

运行顺序

如果我们设定了一个按月运行: scheduler.run_monthly(get_data, tradingday=1). 假设是 2021年 3 月份, 3 月 1 号是第一个交易日:

  • 3 月 1 号: before_trading -> get_data -> handle_bar- 3 月的其他日期: before_trading -> handle_bar


浏览 89
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报