QMT Python API

变量约定

注入机制

用户策略脚本(user_script)在策略平台云端运行时,以下函数会自动注入到全局命名空间,无需 import,直接调用。

stgentry.py 在启动时:

  1. 遍历 functions.py 中所有非 _ 开头的公开名称,注入为全局函数
  2. accountaccountType 注入为全局变量
  3. user_args 中的 key-value 对逐一注入为全局变量
  4. 用户在脚本中定义的 inithandlebar 等回调绑定到 ContextInfo 实例(即回调参数 C

提示

注入机制意味着用户脚本中不需要也不应该 import 这些函数,直接调用即可。用户只需定义 inithandlebar 等回调函数,引擎会在相应时机自动调用。

全局变量

变量类型说明
accountstr交易账户 ID
accountTypestr账户类型(默认 'STOCK'

此外,param['user_args'] 中的所有 key-value 对也会逐一注入为全局变量。

全局函数分类速查表

时间工具

函数说明
datetime_to_timetag(timelabel, format='')日期字符串转毫秒时间戳
timetag_to_datetime(timetag, format='')毫秒时间戳转日期字符串

行情下载

函数说明
download_history_data(stockcode, period, startTime, endTime)下载历史 K 线数据
download_sector_data()下载全部板块数据
download_sector_weight(*args, **kwargs)已废弃,改用 download_sector_data

VBA 公式

函数说明
get_vba_func_result(func, stock_code, period='1d', start_time='', end_time='', count=-1, dividend_type=None, extend_param={}, subscribe=True)获取 VBA 公式计算结果

下单核心

函数说明
passorder(opType, orderType, accountid, orderCode, prType, modelprice, volume, C=None)通用下单(8 参数简化版)
passorder2(opType, orderType, accountid, orderCode, prType, modelprice, volume, strategyName, quickTrade, userOrderId, C=None)通用下单(11 参数完整版)
algo_order_async(accountid, order_type, stock_code, order_volume, price_type, price, strategy_name, user_order_id)异步算法下单

参数速查:

qmt://docs/python
Python
1opType: 0-期货开多 3-期货开空 6-平多今仓优先 8-平多昨仓优先
2 7-平空今仓优先 9-平空昨仓优先 23-买入 24-卖出
3orderType: 0-按股数 1-按金额 2-按百分比 3-按目标百分比
4prType: 0-卖五 … 4-卖一 5-最新价 6-买一 … 10-买五
5 11-限价 12-市价 13-挂单 14-对手价 15-自动 16-收盘价 17-均价

下单便捷封装

以下函数内部自动计算 opType 和 orderType,正值为买入,负值为卖出:

函数说明
order_value(stockcode, value, style='LATEST', price=0.0, C=None, accountid='')按金额下单
order_target(stockcode, target, style='LATEST', price=0.0, C=None, accountid='')调仓至目标股数
order_target_value(stockcode, tar_value, style='LATEST', price=0.0, C=None, accountid='')调仓至目标市值
order_percent(stockcode, percent, style='LATEST', price=0.0, C=None, accountid='')按总资产百分比下单
order_target_percent(stockcode, percent, style='LATEST', price=0.0, C=None, accountid='')调仓至目标百分比
order_shares(stockcode, shares, style='LATEST', price=0.0, C=None, accountid='')按股数下单
order_lots(stockcode, lots, style='LATEST', price=0.0, C=None, accountid='')按手数下单(1 手=100 股)

style 可选值:'LATEST'(最新价)、'FIX'(限价,需传 price)、'MARKET'(市价)、'CLOSE'(收盘价)、'BUY1'~'BUY5''SALE1'~'SALE5'

期货便捷下单

每条函数对应固定的 opType,无需手动指定:

函数opType说明
buy_open(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)0买开(开多)
sell_open(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)3卖开(开空)
sell_close_tdayfirst(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)6平多(今仓优先)
sell_close_ydayfirst(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)8平多(昨仓优先)
buy_close_tdayfirst(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)7平空(今仓优先)
buy_close_ydayfirst(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)9平空(昨仓优先)

账户与回调缓存

函数说明
get_trade_detail_data(accountid, accounttype, datatype)获取交易详情(持仓/订单/成交/账户)
get_account_callback_cache(data, C=None)获取缓存的账户信息
get_order_callback_cache(data, C=None)获取缓存的订单回报
get_deal_callback_cache(data, C=None)获取缓存的成交回报
get_position_callback_cache(data, C=None)获取缓存的持仓信息

状态速查表

正常可用

状态数量说明
正常可用 - 全局函数27时间工具、行情下载、VBA 公式、下单核心、下单便捷封装、期货便捷下单、账户与回调缓存等全局函数
正常可用 - C 方法32ContextInfo 实例方法,详见各函数文档

已废弃

以下函数已废弃,调用时不会产生实际效果,应改用替代方案:

函数替代方案
download_sector_weight改用 download_sector_data
C.set_auto_trade_callback空实现,无替代
C.set_account空实现,无替代
C.trade_callback已废弃
C.register_callback已废弃

空桩

以下函数为空桩实现,调用时返回 None

函数返回值
C.get_option_ivNone
C.bsm_priceNone
C.bsm_ivNone
C.get_backtest_indexNone
C.get_group_resultNone

未实现

以下函数未实现,调用时抛出 NotImplementedError

函数异常替代方案
C.get_financial_dataNotImplementedError改用 C.get_raw_financial_data

智能助手

咨询式 AI · 带入当前文档

智能助手加载中...