3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在华泰等券商提供的 QMT 平台上遇到 NameError: name 'get_history_trade_detail_data' is not defined 报错,通常是由以下几个原因造成的:
get_trade_detail_data,而不是带有 history 前缀的函数。既然 get_history_trade_detail_data 无法使用,您可以使用 QMT 标准 API 中提供的 get_trade_detail_data 函数来获取交易明细数据(包括委托、成交、持仓等)。
get_trade_detail_data 函数用法用法:
get_trade_detail_data(accountID, strAccountType, strDatatype, strategyName)
或不区分策略:
get_trade_detail_data(accountID, strAccountType, strDatatype)
参数说明:
string,资金账号。string,账号类型。可选值:'STOCK'(股票), 'CREDIT'(信用), 'FUTURE'(期货)等。string,数据类型。可选值:
'POSITION':持仓'ORDER':委托'DEAL' :成交'ACCOUNT':账号资金'TASK':任务def init(ContextInfo):
# 设置您的资金账号
ContextInfo.accid = '您的资金账号'
def handlebar(ContextInfo):
# 1. 获取当日成交明细 (DEAL)
deal_list = get_trade_detail_data(ContextInfo.accid, 'stock', 'deal')
print("=== 当日成交明细 ===")
for deal in deal_list:
print(f"合约代码: {deal.m_strInstrumentID}, 成交均价: {deal.m_dPrice}, 成交量: {deal.m_nVolume}, 买卖方向: {deal.m_nDirection}")
# 2. 获取当日委托明细 (ORDER)
order_list = get_trade_detail_data(ContextInfo.accid, 'stock', 'order')
print("=== 当日委托明细 ===")
for order in order_list:
print(f"合约代码: {order.m_strInstrumentID}, 委托价格: {order.m_dLimitPrice}, 委托量: {order.m_nVolumeTotalOriginal}, 状态: {order.m_nOrderStatus}")
需要注意的是,get_trade_detail_data 通常只能获取**当日(当前交易日)**的委托和成交明细。如果您在量化策略中强依赖历史的交割单或历史成交记录,建议采取以下变通方法:
deal_callback)时,将成交信息写入本地的 CSV 文件或 SQLite 数据库中。这样您的策略就可以随时读取自己维护的历史交易记录。