Skip to content

QMT 怎么获取持仓

查当日持仓、可用数量、成本,用主动查询 get_trade_detail_data。委托状态变化走 order_callback,不要混成「没有持仓接口」。

怎么查

strDatatype拿到什么
POSITION持仓明细
ACCOUNT资金(可用、余额等)
ORDER / DEAL当日委托 / 成交

账号类型常用 'STOCK',两融 'CREDIT',期货 'FUTURE'

python
#coding:gbk
def init(C):
    C.acc = account   # 交易界面会注入;编辑器里改成资金账号
    C.acct_type = 'STOCK'

def handlebar(C):
    if not C.is_last_bar():
        return
    poss = get_trade_detail_data(C.acc, C.acct_type, 'POSITION') or []
    for p in poss:
        print(getattr(p, 'm_strInstrumentID', ''),
              'vol', getattr(p, 'm_nVolume', None),
              'can', getattr(p, 'm_nCanUseVolume', None),
              'cost', getattr(p, 'm_dOpenPrice', None))
    accts = get_trade_detail_data(C.acc, C.acct_type, 'ACCOUNT') or []
    if accts:
        a = accts[0]
        print('available', getattr(a, 'm_dAvailable', None))

字段名版本可能略差,先 dir(obj)。完整属性见手册 持仓账号

常见坑

  1. 可用 ≠ 总量:T+1、新买入、冻结都会让 m_nCanUseVolume 小于 m_nVolume
  2. strategyName 只过滤委托/成交:POSITION / ACCOUNT 仍是账户级,不是策略隔离账本。
  3. 回测 / 编辑器:未绑真实账号时列表常为空;在策略交易界面跑再对。
  4. 和 PTrade 字段不是一套:PTrade 是 amount / enable_amount,见 查持仓和资金

相关:get_trade_detail_data · 委托回调 · API 查询

策略要从聚宽 / PTrade / QMT 互转或接到账户,可在 公众号 后台留言「策略代写」。不代操盘。