QMT Python API

交易函数

交易下单函数

综合下单函数 - passorder

综合下单函数,用于股票、期货、期权等下单和新股、新债申购、融资融券等交易操作。

提示

  1. 推荐使用
  2. 可覆盖多品种下单
  3. 注意参数的变化

原型

qmt://docs/python
Python
1passorder(
2 opType, orderType, accountid,
3 orderCode, prType, modelprice, volume,
4 strategyName, quickTrade, userOrderId,
5 ContextInfo
6)

释义

综合交易下单核心函数,支持股票买卖、期货开平仓、期权交易、融资融券、新股申购等多种交易类型。通过 opType 指定买卖方向,orderType 指定下单方式,prType 指定选价类型。

参数

参数名数据类型解释
opTypeint买卖方向,23=买入,24=卖出(含沪港通/深港通)。期货开多=0,开空=3,平多(平今优先)=6,平空(平今优先)=7,平多(平昨优先)=8,平空(平昨优先)=9。融资买入=27,融券卖出=28,担保品买入=33,担保品卖出=34。期权买入开仓=50,卖出平仓=51,卖出开仓=52,买入平仓=53。完整可选值参考枚举常量 - opType 操作类型
orderTypeint下单方式。1101=单股、单账号、普通、股/手方式下单(ETF申赎只能用此参数)。框架简化版:0=按数量(股/手),1=按金额(元),2=按可用资金比例[0~1],3=按总资产比例[0~1]。完整可选值参考枚举常量 - orderType 下单方式
accountidstring资金账号ID,如 '200360'。可填账号组名或套利组名
orderCodestring下单代码,如 600000.SH000001.SZ。组合交易时填篮子名称,组合套利时填"篮子名, 期货合约名"
prTypeint下单选价类型。0~4=卖5价→卖1价,5=最新价,6~10=买1价→买5价,11=指定价,12=涨跌停价,13=挂单价,14=对手价。详细对照表参考枚举常量 - prType 下单选价类型
modelpricefloat下单价格,仅 prType=11(指定价)或 49(盘后定价)时有效;其他情况需填写但可为 -1、0 等任意数字
volumeint下单数量(股/手/元/%),单位由 orderType 值最后一位确定
strategyNamestring策略名,用于区分委托/成交来源。通过 get_trade_detail_data 可按策略名过滤委托或成交子集
quickTradeint是否立即触发下单。0=逐bar模式,bar结束时发出信号;1=立即执行(仅实时数据驱动生效);2=立即执行(实时和历史都生效)。参考枚举常量 - quickTrade 快速下单
userOrderIdstring投资备注,对应委托和成交对象中的 m_strRemark 属性。如果传入该参数,则 strategyNamequickTrade 参数也需填写
ContextInfoobject系统上下文对象,含K线信息和接口的上下文对象,必填

返回值

示例

qmt://docs/python
Python
1#coding:gbk
2 
3def init(ContextInfo):
4 pass
5 
6def handlebar(ContextInfo):
7 # 买入:账号200360, 600000.SH, 2000股, 指定价9.5, 策略=定时策略, 备注=测试下单
8 passorder(23, 1101, '200360', '600000.SH', 11, 9.5, 2000, '定时策略', 2, '测试下单', ContextInfo)
9 
10 # 卖出:账号200360, 688001.SH, 500股, 最优五档即成剩撤(prType=42), 策略=市价最优策略
11 passorder(24, 1101, '200360', '688001.SH', 42, -1, 500, '市价最优策略', 2, '测试市价下单', ContextInfo)
12 
13 # 期货最新价买入 10 手
14 passorder(0, 1101, account, 'rb2405.SF', 5, -1, 10, '示例', 1, '投资备注', ContextInfo)
15 
16 # 期货指定价买入 10 手
17 passorder(0, 1101, account, 'rb2405.SF', 11, 3000, 10, '示例', 1, '投资备注', ContextInfo)
18 
19 # 股票最新价买入 100 股(1 手)
20 passorder(23, 1101, account, '000001.SZ', 5, 0, 100, '示例', 1, '投资备注', ContextInfo)
21 
22 # 股票指定价买入 100 股(1 手)
23 passorder(23, 1101, account, '000001.SZ', 11, 7, 100, '示例', 1, '投资备注', ContextInfo)

更多示例

  1. 股票
  2. 基金
  3. 两融
  4. 期货
  5. 期权
  6. 新股申购
  7. 债券
  8. ETF
  9. 组合交易
  10. 组合套利交易

通用下单(11参数完整版) - passorder2

通用下单函数,为 passorder 的 11 参数完整版本,支持全部策略名、快速下单、投资备注参数。

原型

qmt://docs/python
Python
1passorder2(
2 opType, orderType, accountid,
3 orderCode, prType, modelprice, volume,
4 strategyName, quickTrade, userOrderId,
5 C=None
6)

释义

passorder 功能一致,为 11 参数完整版通用下单函数。C.passorder2(...) 也可作为 passorder 的别名通过 ContextInfo 实例调用。

参数

参数名数据类型解释
opTypeint买卖方向,参考枚举常量 - opType 操作类型
orderTypeint下单方式,参考枚举常量 - orderType 下单方式
accountidstring资金账号ID
orderCodestring下单代码
prTypeint下单选价类型,参考枚举常量 - prType 下单选价类型
modelpricefloat下单价格
volumeint下单数量
strategyNamestring策略名
quickTradeint快速下单标记
userOrderIdstring投资备注
Cobject系统上下文对象

返回值

按股数下单 - order_shares

按股数下单,正数买入,负数卖出。

原型

qmt://docs/python
Python
1order_shares(stockcode, shares, style='LATEST', price=0.0, C=None, accountid='')

释义

按股数下单的便捷封装函数,内部自动计算 opType 和 orderType。正数为买入,负数为卖出。

参数

参数名数据类型解释
stockcodestring股票代码,如 '000002.SZ'
sharesint股数,正数买入,负数卖出
stylestring选价类型,默认 'LATEST'(最新价)。可选值见style 选价类型一览
pricefloat指定价格,仅 style='FIX' 时有效,默认 0.0
Cobject系统上下文对象
accountidstring资金账号,默认空字符串

返回值

示例

qmt://docs/python
Python
1order_shares('000002.SZ', 100, C=C) # 最新价买 100 股
2order_shares('000002.SZ', -100, 'COMPETE', C=C) # 对手价卖 100 股
3order_shares('000002.SZ', 200, 'FIX', 37.5, C=C) # 指定价 37.5 买 200 股

按手数下单 - order_lots

按手数下单(A股 1 手 = 100 股)。

原型

qmt://docs/python
Python
1order_lots(stockcode, lots, style='LATEST', price=0.0, C=None, accountid='')

释义

按手数下单的便捷封装函数,内部自动计算 opType 和 orderType。正数为买入,负数为卖出。

参数

参数名数据类型解释
stockcodestring股票代码
lotsint手数(1 手 = 100 股),正数买入,负数卖出
stylestring选价类型,默认 'LATEST'
pricefloat指定价格,仅 style='FIX' 时有效
Cobject系统上下文对象
accountidstring资金账号

返回值

按金额下单 - order_value

按金额下单,正数买入,负数卖出。

原型

qmt://docs/python
Python
1order_value(stockcode, value, style='LATEST', price=0.0, C=None, accountid='')

释义

按金额下单的便捷封装函数,内部自动计算 opType 和 orderType。正数为买入,负数为卖出。

参数

参数名数据类型解释
stockcodestring股票代码
valuefloat金额(元),正数买入,负数卖出
stylestring选价类型,默认 'LATEST'
pricefloat指定价格,仅 style='FIX' 时有效
Cobject系统上下文对象
accountidstring资金账号

返回值

示例

qmt://docs/python
Python
1order_value('000002.SZ', 10000, C=C) # 最新价买 10000 元
2order_value('000002.SZ', -10000, C=C) # 最新价卖出 10000 元市值的股票

调整到目标持仓数 - order_target

调整到目标持仓数。

原型

qmt://docs/python
Python
1order_target(stockcode, target, style='LATEST', price=0.0, C=None, accountid='')

释义

将指定股票调整到目标持仓数。内部自动计算差额并下单。

参数

参数名数据类型解释
stockcodestring股票代码
targetint目标持仓股数
stylestring选价类型,默认 'LATEST'
pricefloat指定价格,仅 style='FIX' 时有效
Cobject系统上下文对象
accountidstring资金账号

返回值

调整到目标持仓市值 - order_target_value

调整到目标持仓市值。

原型

qmt://docs/python
Python
1order_target_value(stockcode, tar_value, style='LATEST', price=0.0, C=None, accountid='')

释义

将指定股票调整到目标持仓市值。内部自动计算差额并下单。

参数

参数名数据类型解释
stockcodestring股票代码
tar_valuefloat目标持仓市值(元)
stylestring选价类型,默认 'LATEST'
pricefloat指定价格,仅 style='FIX' 时有效
Cobject系统上下文对象
accountidstring资金账号

返回值

按总资产百分比下单 - order_percent

按总资产百分比下单。

原型

qmt://docs/python
Python
1order_percent(stockcode, percent, style='LATEST', price=0.0, C=None, accountid='')

释义

按总资产百分比下单的便捷封装函数。percent 范围 0~1,正数买入,负数卖出。

参数

参数名数据类型解释
stockcodestring股票代码
percentfloat总资产百分比,范围 0~1,正数买入,负数卖出
stylestring选价类型,默认 'LATEST'
pricefloat指定价格,仅 style='FIX' 时有效
Cobject系统上下文对象
accountidstring资金账号

返回值

调整仓位到总资产的目标百分比 - order_target_percent

调整仓位到总资产的目标百分比。

原型

qmt://docs/python
Python
1order_target_percent(stockcode, percent, style='LATEST', price=0.0, C=None, accountid='')

释义

将指定股票的仓位调整到总资产的目标百分比。内部自动计算差额并下单。

参数

参数名数据类型解释
stockcodestring股票代码
percentfloat目标百分比,范围 0~1
stylestring选价类型,默认 'LATEST'
pricefloat指定价格,仅 style='FIX' 时有效
Cobject系统上下文对象
accountidstring资金账号

返回值

异步算法下单 - algo_order_async

异步算法下单函数。

原型

qmt://docs/python
Python
1algo_order_async(accountid, order_type, stock_code, order_volume,
2 price_type, price, strategy_name, user_order_id)

释义

异步算法下单函数,用于通过算法引擎异步执行下单操作。

参数

参数名数据类型解释
accountidstring资金账号
order_typeint订单类型,参考枚举常量 - opType 操作类型
stock_codestring股票/合约代码
order_volumeint下单数量
price_typeint报价类型,参考枚举常量 - prType 下单选价类型
pricefloat下单价格
strategy_namestring策略名
user_order_idstring用户自设委托ID

返回值

style 选价类型一览

便捷下单函数(order_sharesorder_lotsorder_valueorder_targetorder_target_valueorder_percentorder_target_percent)的 style 参数可选值如下:

styleprType说明
LATEST5最新价(默认)
FIX11指定价
MARKET12涨跌停价
HANG13挂单价
COMPETE14对手价
SALE1~SALE54~0卖1~卖5价
BUY1~BUY56~10买1~买5价

提示

style 还支持 'CLOSE'(收盘价)等可选值。当 style='FIX' 时需同时传入 price 参数指定价格。

期货便捷下单函数

期货便捷下单函数每条对应固定的 opType,无需手动指定买卖方向。正数为买入/开仓,负数为卖出/平仓。

期货开多 - buy_open

期货买开(开多),对应 opType=0。

原型

qmt://docs/python
Python
1buy_open(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)

释义

期货开多仓的便捷下单函数,内部自动设置 opType=0。

参数

参数名数据类型解释
orderCodestring期货合约代码,如 'IF1805.IF'
modelVolumeint下单数量(手)
priceStylestring选价类型,参考style 选价类型一览,默认空字符串
pricefloat指定价格,默认 0.0
Cobject系统上下文对象
accountIDstring资金账号
quckTradeint快速下单标记,默认 0

返回值

示例

qmt://docs/python
Python
1buy_open('IF1805.IF', 1, 'LATEST', C=C) # 最新价 1 手开多
2buy_open('IF1805.IF', 2, 'FIX', 3750, C=C) # 指定价 3750 开 2 手

期货开空 - sell_open

期货卖开(开空),对应 opType=3。

原型

qmt://docs/python
Python
1sell_open(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)

释义

期货开空仓的便捷下单函数,内部自动设置 opType=3。

参数

参数名数据类型解释
orderCodestring期货合约代码
modelVolumeint下单数量(手)
priceStylestring选价类型,参考style 选价类型一览
pricefloat指定价格
Cobject系统上下文对象
accountIDstring资金账号
quckTradeint快速下单标记

返回值

平多(今仓优先) - sell_close_tdayfirst

期货平多,今仓优先,对应 opType=6。

原型

qmt://docs/python
Python
1sell_close_tdayfirst(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)

释义

期货平多仓的便捷下单函数,优先平今仓,内部自动设置 opType=6。

参数

参数名数据类型解释
orderCodestring期货合约代码
modelVolumeint下单数量(手)
priceStylestring选价类型,参考style 选价类型一览
pricefloat指定价格
Cobject系统上下文对象
accountIDstring资金账号
quckTradeint快速下单标记

返回值

示例

qmt://docs/python
Python
1sell_close_tdayfirst('IF1805.IF', 1, C=C) # 最新价平 1 手多

平多(昨仓优先) - sell_close_ydayfirst

期货平多,昨仓优先,对应 opType=8。

原型

qmt://docs/python
Python
1sell_close_ydayfirst(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)

释义

期货平多仓的便捷下单函数,优先平昨仓,内部自动设置 opType=8。

参数

参数名数据类型解释
orderCodestring期货合约代码
modelVolumeint下单数量(手)
priceStylestring选价类型,参考style 选价类型一览
pricefloat指定价格
Cobject系统上下文对象
accountIDstring资金账号
quckTradeint快速下单标记

返回值

平空(今仓优先) - buy_close_tdayfirst

期货平空,今仓优先,对应 opType=7。

原型

qmt://docs/python
Python
1buy_close_tdayfirst(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)

释义

期货平空仓的便捷下单函数,优先平今仓,内部自动设置 opType=7。

参数

参数名数据类型解释
orderCodestring期货合约代码
modelVolumeint下单数量(手)
priceStylestring选价类型,参考style 选价类型一览
pricefloat指定价格
Cobject系统上下文对象
accountIDstring资金账号
quckTradeint快速下单标记

返回值

平空(昨仓优先) - buy_close_ydayfirst

期货平空,昨仓优先,对应 opType=9。

原型

qmt://docs/python
Python
1buy_close_ydayfirst(orderCode, modelVolume, priceStyle='', price=0.0, C=None, accountID='', quckTrade=0)

释义

期货平空仓的便捷下单函数,优先平昨仓,内部自动设置 opType=9。

参数

参数名数据类型解释
orderCodestring期货合约代码
modelVolumeint下单数量(手)
priceStylestring选价类型,参考style 选价类型一览
pricefloat指定价格
Cobject系统上下文对象
accountIDstring资金账号
quckTradeint快速下单标记

返回值

期货便捷下单函数一览

函数opType说明
buy_open0期货开多
sell_open3期货开空
sell_close_tdayfirst6平多(平今优先)
sell_close_ydayfirst8平多(平昨优先)
buy_close_tdayfirst7平空(平今优先)
buy_close_ydayfirst9平空(平昨优先)

提示

期货便捷下单函数每条对应固定的 opType,无需手动指定买卖方向。priceStyle 参数可选值与便捷下单函数的 style 一致,参考style 选价类型一览

提示

交易相关的回调函数(委托回报、成交回报、持仓变化等)请参考回调函数。交易明细查询请参考交易查询函数

智能助手

咨询式 AI · 带入当前文档

智能助手加载中...