复制
数据获取接口
1、数据接口种类
- 获取指定行业、板块股票列表
- history_bars:指定股票合约历史数据(基本行情数据:基本交易信息、K线图)
- get_fundamentals:查询财务数据(基本面数据:财务数据等)
2、获取行业、板块以及概念股票列表
2.1 关于股票代码以及代码补齐
米筐上的股票代码标记:
XSH(G)=shan(G)hai
XSH(E)=sh(E)zhen
股票自动搜索补全: ctrl+i
def init(context):
# 在context中保存全局变量
context.s1 = "000001.XSHE"
context.stock = "0000007.XSHE" #这个股票代码就是自动补齐的
def before_trading(context):
print(context.stock)
logger.info(context.stock) #两种输出方法
- 股票数字代码: 自动补全为股票数字代码,比如"000024.XSHE";
- 股票中文全称: 自动补全为股票中文全称,比如"招商地产";
- 股票拼音缩写: 这里比较特殊,自动补全为股票中文全称,因为股票拼音缩写并不是独一无二的,比如 ZSDC 补全为”招裔地产。
2.2 获取行业
industry:行业股票列表
industry(code) #获取某一行业的所有股票列表
参数code: 行业名称或行业代码。如农业为industry_code.A01或’A01’
返回: 获得属于某一行业的所有股票的order_book_id list。
范例:
def init(context):
# 获取计算机通信行业的一些公司股票代码
context.stock_list = industry('C39')
def before_trading(context):
print(context.stock_list)
2.3 获取板块
sector:板块股票列表
sector(code) #获取属于某一板块的所有股票列表
参数code: 板块名称或板块代码。如能源为’Energy’、‘能源’或’sector_code.Energy’
返回: 获得属于该板块的所有股票的order_book_id或order_book_id list。
范例:
def init(context):
# 获取某个板块
context.sector_list = sector('energy')
def before_trading(context):
print(context.sector_list)
2.4 获取概念
2.5 获取指数成分股
index_components:指数成分股
index_components(order_book_id, data=None)
获取某一指数的股票构成列表,也支持指数的历史构成查询
返回: 获得构成该指数的所有股票的order_book_id list。
常见的指数获取代码为:
范例:
def init(context):
# 指数成分股的接口
context.index_list = index_components('000300.XSHG')
def before_trading(context):
print(context.index_list)
3、获取股票合约数据
3.1 history_bars:某一合约历史数据
history_bars(order_book_id, bar_count, frequency, fields=None, skip_suspended=True, include_now)
def handle_bar(context, bar_dict):
# 5:从当前获取到的日期之前5天的价格
close = history_bars(context.s1, 5, '1d', 'close')
logger.info(close)
def handle_bar(context, bar_dict):
# 获取开盘和收盘价
history_1 = history_bars(context.s1, 5, '1d', ['close','open'])
logger.info(history_1)
3.2 代码以及注意的问题
3.3 其他—通过bar_dict获取
获取当前价格的bar_dict: