文档
测试

17)获取行情数据

POST

说明 / 示例

用法: ContextInfo.get_market_data(fields, stock_code = [], start_time = ‘’, end_time = ‘’, skip_paused = True, period = ‘1d’, dividend_type = ‘none’, count = -1) 释义: 获取行情数据 参数: fields:字段列表: ‘open’:开 ‘high’:高 ‘low’:低 ‘close’:收 ‘volume’:成交量 ‘amount’:成交额 ‘settle’:结算价 ‘quoter’:分笔数据(包括历史) ‘quoter’分笔数据结构:dict { lastPrice:最新价 amount:成交额 volume:成交量 pvolumn:前成交量 openInt:持仓量 stockStatus:股票状态 lastSettlementPrice:最新结算价 open:开盘价 high:最高价 low:最低价 settlementPrice:结算价 lastClose:收盘价 askPrice:列表,卖价五档 bidPrice:列表,买价五档 askVol:列表,卖量五档 bidVol;列表,买量五档 } stock_code :默认参数,合约代码列表,合约格式 code.market, 如 ‘600000.SH’,不指定时为当前图合约 start_time:默认参数,开始时间,格式 ‘20171209’ 或 ‘20171209010101’ end_time:默认参数,结束时间,格式 ‘20171209’ 或 ‘20171209010101’ skip_paused:默认参数,可选值: true:如果是停牌股,会自动填充未停牌前的价格作为停牌日的价格 False:停牌数据为nan period:默认参数,周期类型: ‘tick’:分笔线 ‘1d’:日线 ‘1m’:1分钟线 ‘3m’:3分钟线 ‘5m’:5分钟线 ‘15m’:15分钟线 ‘30m’:30分钟线 ‘1h’:小时线 ‘1w’:周线 ‘1mon’:月线 ‘1q’:季线 ‘1hy’:半年线 ‘1y’:年线 dividend_type:默认参数,缺省值为 ‘none’,除复权,可选值: ‘none’:不复权 ‘front’:向前复权 ‘back’:向后复权 ‘front_ratio’:等比向前复权 ‘back_ratio’:等比向后复权 count:默认参数,缺省值为 -1,大于等于 0 时,效果与 get_history_data 保持一致。 count 和开始时间、结束时间同时设置的效果如下表: count 取值 时间设置是否生效 开始时间和结束时间设置效果 count >= 0 无效 从当前时间往回取多少个周期的值,取值数量完全取决于count count = -1 生效 同时设置开始时间和结束时间,在所设置的时间段内取值 count = -1 生效 开始时间结束时间都不设置,取当前最新bar的值 count = -1 生效 只设置开始时间,取所设开始时间到当前时间的值 count = -1 生效 只设置结束时间,取股票上市第一根 bar 到所设结束时间的值 *注: 特别的,默认参数需带上参数名方可生效,调用方式与 Python 别无二致 策略点击运行,以策略代码中所设置的开始时间和结束时间为准,策略点击回测,以 ‘回测参数’ 里所设置的开始时间和结束时间为准。 可缺省参数:start_time,end_time,skip_paused,dividend_type,count 返回: count >= 0,不同的参数,返回的数据结构不同,以下举例说明了不同的数据结构的类型。(代码指股票代码;时间即为函数中所设置的时间,主要由 count、start_time、end_time 决定;字段即 fields 里的字段) (1)1支股票代码,在1个时间点( count 缺省默认为 -1 ,即为当前时间点的 bar),取1个字段的值,则返回相应字段的值。 代码: ```py def init(ContextInfo): ContextInfo.set_universe(['000831.SZ']) def handlebar(ContextInfo): df = ContextInfo.get_market_data(['close'], stock_code = ContextInfo.get_universe(), skip_paused = True, period = '1d', dividend_type = 'front') print(df) ``` 输出: 17.40 ## 2)1支股票代码,在一个时间点(count、start_time、end_time 都缺省,即为当前时间点的 bar),取多个字段的值,返回 pandas.Series( pandas 一维数组)类型的值。 代码: ```py def init(ContextInfo): ContextInfo.set_universe(['000831.SZ']) def handlebar(ContextInfo): df = ContextInfo.get_market_data(['close', 'open'], stock_code = ContextInfo.get_universe(), skip_paused = True, period = '1d', dividend_type = 'front') print(df) ``` 输出: close 17.40 open 17.11 ## (3)1支股票代码,在一个时间段取值,返回 pandas.DataFrame ( pandas 二维表格型数据结构)类型的值。 代码: ```py def init(ContextInfo): ContextInfo.set_universe(['000831.SZ']) def handlebar(ContextInfo): df = ContextInfo.get_market_data(['close', 'open'], stock_code = ContextInfo.get_universe(), skip_paused = True, period = '1d', dividend_type = 'front', count = 2) print(df) ``` 输出: close open 20190618 17.59 17.60 20190619 17.40 17.11 ## (4)多个股票代码,在一个时间点取值,返回 pandas.DataFrame ( pandas 二维表格型数据结构)类型的值。 代码: ```py def init(ContextInfo): ContextInfo.set_universe(['000831.SZ', '600000.SH']) def handlebar(ContextInfo): df = ContextInfo.get_market_data(['close', 'open'], stock_code = ContextInfo.get_universe(), skip_paused = True, period = '1d', dividend_type = 'front', count = -1) print(df) ``` 输出: close open 000831.SZ 17.40 17.11 600000.SH 11.88 12.04 (## 5)多支股票代码,在一个时间段取值,返回 pandas.Panel (pandas 三维数组结构)类型的值。 代码: ```py def init(ContextInfo): ContextInfo.set_universe(['000831.SZ', '600000.SH']) def handlebar(ContextInfo): df = ContextInfo.get_market_data(['close', 'open'], stock_code = ContextInfo.get_universe(), skip_paused = True, period = '1d', dividend_type = 'front', count = 2) print(df) ``` 输出: <class 'pandas.core.panel.Panel'> Dimensions: 2 (items) x 2 (major_axis) x 2 (minor_axis) Items axis: 000831.SZ to 600000.SH Major_axis axis: 20190618 to 20190619 Minor_axis axis: close to open # *注: 需要下载历史数据; pandas相关介绍详见pandas官方文档; Python需要安装pandas; 时间区间:两边闭区间。 get_history_data() 和 get_market_data() 两者区别:get_history_data() 是获取设定的股票池中股票某段行情,主要用来构建简单的指标;get_market_data() 可获取任意股票行情,返回行情数据可以是 dataframe ,可用于复杂的分析。 示例1: ```py def handlebar(ContextInfo): # 以 period 参数指定需要的周期 Result = ContextInfo.get_market_data(['open','high','low','close'], ['600000.SH'], '20170101', '20170102', True, '1d', 'none') d_m = ContextInfo.get_market_data(['close'], ['600000.SH'], start_time = '20181230', end_time = '20190107', period = '1d') print(d_m) m_m = ContextInfo.get_market_data(['close'], ['600000.SH'], start_time = '20181230', end_time = '20190107', period = '1m') print(m_m) ``` 示例2: ```py def init(ContextInfo): # 先设置股票池,此时股票池只有一个股票 ContextInfo.set_universe(['000001.SZ']) def handlebar(ContextInfo): # 获取平安银行当前 bar 的收盘价 close = ContextInfo.get_market_data(['close'], stock_code = ContextInfo.get_universe(), period = '1d', dividend_type = 'front', count = 1) # 打印平安银行当前bar的收盘价 print(close) # close 是一个数值 # 获取平安银行最新的开高低收四个价格 df1 = ContextInfo.get_market_data(['open', 'high', 'low', 'close'], stock_code = ContextInfo.get_universe(), period = '1d', dividend_type = 'front', count = 1) # 打印平安银行当前bar的最高价 print(df1['high']) # df1 是一个 pandas.series # 获取平安银行最近两天的高开低收 df2 = ContextInfo.get_market_data(['open', 'high', 'low', 'close'], stock_code = ContextInfo.get_universe(), period = '1d', dividend_type = 'front', count = 2) # 打印平安银行昨天的收盘价 print(df2['close'][-2]) # df2 是一个 pandas.dataframe ```