本章列出 QMT 内置 Python 中的核心数据结构和枚举常量,供开发时查阅。

ContextInfo 对象

ContextInfo 是策略运行的核心上下文对象。

属性

属性 类型 说明
stockcode str 当前品种代码
period str 当前周期(如 '1d', '5m')
timetag int 当前 K 线时间戳
barpos int 当前 K 线位置索引
do_back_test bool 是否处于回测模式
bar_size int K 线数量
run_time str 策略运行时间

方法 - 数据获取

方法 参数 返回值 说明
get_close_price() - float 收盘价
get_open_price() - float 开盘价
get_high_price() - float 最高价
get_low_price() - float 最低价
get_volume() - int 成交量
get_money() - float 成交额
get_last_price(stock) stock: str float 最新价
get_history_data(field, count, stock) field, count, stock list 历史数据
get_market_data(fields, stocks, period, count) - dict 市场数据

方法 - 交易

方法 参数 返回值 说明
order_volume(...) stockcode, volume, priceType, price, orderType str 按数量下单
order_value(...) stockcode, value, priceType, price, orderType str 按金额下单
order_target(...) stockcode, target_volume, priceType, price str 调仓到目标数量
order_target_value(...) stockcode, target_value, priceType, price str 调仓到目标金额
cancel(order_id, ContextInfo) order_id - 撤单
get_account() - Account 获取账户信息
get_positions() - dict 获取所有持仓
get_position(stock) stock: str Position 获取指定品种持仓
get_orders() - list 获取当日委托

方法 - 配置

方法 参数 说明
set_universe(stocks) stocks: list 设置股票池
get_universe() - 获取股票池
is_last_bar() - 是否为最新 K 线
do_download_history_data(stock, period, start, end) - 下载历史数据

Account 对象

账户信息对象,通过 ContextInfo.get_account() 获取。

字段 类型 说明
m_dBalance float 总资产
m_dAvailable float 可用资金
m_dPositionValue float 持仓市值
m_dFrozenCash float 冻结资金
m_dProfit float 当日盈亏
m_dFetchBalance float 可取资金
m_strAccountID str 资金账号
m_strBrokerID str 服务机构代码

Position 对象

持仓信息对象,通过 ContextInfo.get_position()ContextInfo.get_positions() 获取。

字段 类型 说明
m_strStockCode str 品种代码
m_nVolume int 持仓数量
m_nCanUseVolume int 可用数量
m_nFrozenVolume int 冻结数量
m_dOpenPrice float 持仓成本价
m_dProfit float 持仓盈亏
m_dMarketValue float 持仓市值
m_dProfitRatio float 盈亏比例

Order 对象

委托信息对象,通过 ContextInfo.get_orders()order_callback 获取。

字段 类型 说明
m_strStockCode str 品种代码
m_nOrderType int 委托类型(1=买, 2=卖)
m_nVolume int 委托数量
m_dPrice float 委托价格
m_nOrderStatus int 委托状态
m_strOrderSysID str 委托编号
m_nTradedVolume int 已成交数量
m_dTradedAmount float 已成交金额
m_strErrorInfo str 错误信息

枚举常量

委托类型 (orderType)

说明
1 买入
2 卖出

价格类型 (priceType)

说明
0 最优五档即时成交剩撤销
5 最优价
11 限价单
12 最新价
14 市价

委托状态 (orderStatus)

说明
50 委托已提交
55 委托已确认
56 部分成交
57 全部成交
58 已撤销
59 部分撤销

周期代码 (period)

说明
1d 日线
1m 1 分钟
5m 5 分钟
15m 15 分钟
30m 30 分钟
60m 60 分钟
tick Tick

复权类型

说明
0 不复权
1 前复权
2 后复权

回调函数

函数名 触发时机 参数
init(ContextInfo) 策略启动时 ContextInfo
handlebar(ContextInfo) 每根 K 线 ContextInfo
order_callback(ContextInfo, order_info) 委托状态变化 ContextInfo, Order
deal_callback(ContextInfo, deal_info) 成交回报 ContextInfo, DealInfo
before_init(ContextInfo) init 之前 ContextInfo
after_init(ContextInfo) init 之后 ContextInfo

DealInfo 对象

成交回报信息对象,通过 deal_callback 获取。

字段 类型 说明
m_strStockCode str 品种代码
m_nOrderType int 委托类型
m_nVolume int 成交数量
m_dPrice float 成交价格
m_strTradeID str 成交编号
m_strOrderSysID str 关联委托编号
m_dAmount float 成交金额
m_dCommission float 手续费