data.current(assets, fields)
用于获取当前回测日期可以看到的最新数据
参数
返回
示例
>>>current = data.current(symbol('jd'),
fields=['open','close','high','low','volume','last_traded','price'])
>>>print(current)
open 53.35
close 54.36
high 54.5
low 53.29
volume 117857
data.history(assets, fields, bar_count, frequency)
用于获取当前回测日期前 bar_count 个交易日的 k 线数据。
参数
返回
示例
#参数传入 单个 asset 单个 fields, 返回Series
>>>hist = data.history(assets=symbol('AAPL'),
fields='close',
bar_count=5,
frequency='1d')
>>>print(hist)
2017-12-04 00:00:00+00:00 37.45
2017-12-05 00:00:00+00:00 37.55
2017-12-06 00:00:00+00:00 36.30
2017-12-07 00:00:00+00:00 36.25
2017-12-08 00:00:00+00:00 35.70
# 参数传入 单个assets 多个fields,返回 DataFrame
>>>hist = data.history(assets=symbol('AAPL'),
fields=['close','volume'],
bar_count=5,
frequency='1d')
>>>print(hist)
close volume
2017-12-04 00:00:00+00:00 37.45 254633.0
2017-12-05 00:00:00+00:00 37.55 253858.0
2017-12-06 00:00:00+00:00 36.30 208441.0
2017-12-07 00:00:00+00:00 36.25 136271.0
2017-12-08 00:00:00+00:00 35.70 102565.0
# 参数传入 多个 assets 单个 fields,返回 DataFrame
>>>hist = data.history(assets=[symbol('AAPL'),symbol('JD')],
fields='volume',
bar_count=5,
frequency='1d')
>>>print(hist)
Equity(12 [AAPL]) Equity(3647 [JD])
2017-12-04 00:00:00+00:00 37.45 52.55
2017-12-05 00:00:00+00:00 37.55 52.98
2017-12-06 00:00:00+00:00 36.30 52.35
2017-12-07 00:00:00+00:00 36.25 53.44
2017-12-08 00:00:00+00:00 35.70 52.93
# 参数传入 多个 assets 多个 fields,返回 Panel
>>>hist = data.history(assets=assets,
fields=['close','volume'],
bar_count=5,
frequency='1d')
>>>print(hist)
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 5 (major_axis) x 2 (minor_axis)
Items axis: close to volume
Major_axis axis: 2017-12-26 00:00:00+00:00 to 2018-01-02 00:00:00+00:00
Minor_axis axis: Equity(12 [AAPL]) to Equity(3647 [JD])
# panel 是一个三维的数据结构,可以从三维中的任意维度切片,返回结果是一个 DataFrame
# 获取所有 assets 的收盘价
>>>print(hist.xs('close',axis=0))
Equity(12 [AAPL]) Equity(3647 [JD])
2017-12-26 00:00:00+00:00 36.60 53.00
2017-12-27 00:00:00+00:00 36.50 52.72
2017-12-28 00:00:00+00:00 36.80 52.80
2017-12-29 00:00:00+00:00 36.70 53.00
2018-01-02 00:00:00+00:00 36.95 54.36
# 获取 2017-12-26 所有 assets 的所有 fields
>>>from datetime import datetime
>>>import pytz
>>>print(hist.xs(datetime(2017, 12, 26, tzinfo=pytz.utc), axis=1))
close volume
Equity(12 [AAPL]) 36.6 143262.0
Equity(3647 [JD]) 53.0 112277.0
# 获取 jd 的收盘价和成交量
>>>print(hist.xs(symbol('JD'), axis=2))
close volume
2017-12-26 00:00:00+00:00 53.00 112277.0
2017-12-27 00:00:00+00:00 52.72 140665.0
2017-12-28 00:00:00+00:00 52.80 94743.0
2017-12-29 00:00:00+00:00 53.00 126968.0
2018-01-02 00:00:00+00:00 54.36 117857.0