章节大纲

  • 数据检索


    简介

    用户可以使用 Qlib 获取股票数据。以下示例展示了其基本的用户界面。

    示例

    QLib 初始化:

    注意

    为了获取数据,用户需要先使用 qlib.init 初始化 Qlib。请参阅初始化

    如果用户按照初始化中的步骤下载了数据,则应使用以下代码初始化 qlib:

    Python
    >> import qlib
    >> qlib.init(provider_uri='~/.qlib/qlib_data/cn_data')
    

    加载指定时间范围和频率的交易日历:

    Python
    >> from qlib.data import D
    >> D.calendar(start_time='2010-01-01', end_time='2017-12-31', freq='day')[:2]
    [Timestamp('2010-01-04 00:00:00'), Timestamp('2010-01-05 00:00:00')]
    

    将给定的市场名称解析为股票池配置:

    Python
    >> from qlib.data import D
    >> D.instruments(market='all')
    {'market': 'all', 'filter_pipe': []}
    

    加载指定时间范围内的特定股票池的成分股:

    Python
    >> from qlib.data import D
    >> instruments = D.instruments(market='csi300')
    >> D.list_instruments(instruments=instruments, start_time='2010-01-01', end_time='2017-12-31', as_list=True)[:6]
    ['SH600036', 'SH600110', 'SH600087', 'SH600900', 'SH600089', 'SZ000912']
    

    根据名称过滤器从基础市场加载动态成分股:

    Python
    >> from qlib.data import D
    >> from qlib.data.filter import NameDFilter
    >> nameDFilter = NameDFilter(name_rule_re='SH[0-9]{4}55')
    >> instruments = D.instruments(market='csi300', filter_pipe=[nameDFilter])
    >> D.list_instruments(instruments=instruments, start_time='2015-01-01', end_time='2016-02-15', as_list=True)
    ['SH600655', 'SH601555']
    

    根据表达式过滤器从基础市场加载动态成分股:

    Python
    >> from qlib.data import D
    >> from qlib.data.filter import ExpressionDFilter
    >> expressionDFilter = ExpressionDFilter(rule_expression='$close>2000')
    >> instruments = D.instruments(market='csi300', filter_pipe=[expressionDFilter])
    >> D.list_instruments(instruments=instruments, start_time='2015-01-01', end_time='2016-02-15', as_list=True)
    ['SZ000651', 'SZ000002', 'SH600655', 'SH600570']
    

    有关过滤器的更多详细信息,请参阅过滤器 API

    加载指定时间范围内特定成分股的特征:

    Python
    >> from qlib.data import D
    >> instruments = ['SH600000']
    >> fields = ['$close', '$volume', 'Ref($close, 1)', 'Mean($close, 3)', '$high-$low']
    >> D.features(instruments, fields, start_time='2010-01-01', end_time='2017-12-31', freq='day').head().to_string()
    '                           $close     $volume  Ref($close, 1)  Mean($close, 3)  $high-$low... 
    instrument  datetime... 
    SH600000    2010-01-04  86.778313  16162960.0       88.825928        88.061483    2.907631... 
                2010-01-05  87.433578  28117442.0       86.778313        87.679273    3.235252... 
                2010-01-06  85.713585  23632884.0       87.433578        86.641825    1.720009... 
                2010-01-07  83.788803  20813402.0       85.713585        85.645322    3.030487... 
                2010-01-08  84.730675  16044853.0       83.788803        84.744354    2.047623'
    

    加载指定时间范围内特定股票池的特征:

    注意

    启用缓存后,qlib 数据服务器将为请求的股票池和字段始终缓存数据,这可能会导致首次处理请求的时间比没有缓存时更长。但第一次之后,即使请求的时间段发生变化,具有相同股票池和字段的请求也会命中缓存并处理得更快。

    Python
    >> from qlib.data import D
    >> from qlib.data.filter import NameDFilter, ExpressionDFilter
    >> nameDFilter = NameDFilter(name_rule_re='SH[0-9]{4}55')
    >> expressionDFilter = ExpressionDFilter(rule_expression='$close>Ref($close,1)')
    >> instruments = D.instruments(market='csi300', filter_pipe=[nameDFilter, expressionDFilter])
    >> fields = ['$close', '$volume', 'Ref($close, 1)', 'Mean($close, 3)', '$high-$low']
    >> D.features(instruments, fields, start_time='2010-01-01', end_time='2017-12-31', freq='day').head().to_string()
    '                              $close        $volume  Ref($close, 1)  Mean($close, 3)  $high-$low... 
    instrument  datetime... 
    SH600655    2010-01-04  2699.567383  158193.328125     2619.070312      2626.097738  124.580566... 
                2010-01-08  2612.359619   77501.406250     2584.567627      2623.220133   83.373047... 
                2010-01-11  2712.982422  160852.390625     2612.359619      2636.636556  146.621582... 
                2010-01-12  2788.688232  164587.937500     2712.982422      2704.676758  128.413818... 
                2010-01-13  2790.604004  145460.453125     2788.688232      2764.091553  128.413818'
    

    有关特征的更多详细信息,请参阅特征 API

    注意

    在客户端调用 D.features() 时,使用参数 disk_cache=0 跳过数据集缓存,使用 disk_cache=1 生成并使用数据集缓存。此外,在服务器端调用时,用户可以使用 disk_cache=2 更新数据集缓存。

    当您构建复杂的表达式时,在一个字符串中实现所有表达式可能并不容易。例如,它看起来相当长且复杂:

    Python
    >> from qlib.data import D
    >> data = D.features(["sh600519"], ["(($high / $close) + ($open / $close)) * (($high / $close) + ($open / $close)) / (($high / $close) + ($open / $close))"], start_time="20200101")
    

    但使用字符串并不是实现表达式的唯一方法。您也可以通过代码实现表达式。下面是一个与上面示例做同样事情的例子。

    Python
    >> from qlib.data.ops import *
    >> f1 = Feature("high") / Feature("close")
    >> f2 = Feature("open") / Feature("close")
    >> f3 = f1 + f2
    >> f4 = f3 * f3 / f3
    >> data = D.features(["sh600519"], [f4], start_time="20200101")
    >> data.head()
    

    API

    要了解如何使用数据,请转到 API 参考:数据 API