3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在融资融券(两融)交易中,合理利用**担保品折算率(Assure Ratio)**能够显著提升账户的整体保证金使用效率和购买力。本文将详细介绍如何通过 QMT API 查询信用账户的担保品折算率,并编写自动化调仓策略。
get_assure_contract(accId):获取信用账户担保品合约明细对象(StkSubjects),其属性 m_dAssureRatio 即为担保品折算率。passorder(opType, orderType, accountid, orderCode, ...):下单接口,两融担保品买入使用 opType=33(信用账号股票买入)或 opType=23。以下示例展示了如何在 QMT 中获取股票池中各标的的折算率,并按折算率由高到低分配仓位:
#encoding:gbk
import pandas as pd
def init(ContextInfo):
# 1. 设置信用资金账号
ContextInfo.accID = '6000000001'
ContextInfo.set_account(ContextInfo.accID)
# 2. 设置标的股票池
ContextInfo.stocks = ['600000.SH', '000001.SZ', '600519.SH']
ContextInfo.set_universe(ContextInfo.stocks)
# 定时运行调仓逻辑
ContextInfo.run_time("rebalance", "30nSecond", "2023-01-01 09:30:00", "SH")
def rebalance(ContextInfo):
if not ContextInfo.is_last_bar():
return
# 3. 查询信用账户担保品明细,提取折算率
assure_list = get_assure_contract(ContextInfo.accID)
assure_dict = {}
for item in assure_list:
# item.m_strInstrumentID: 股票代码, item.m_dAssureRatio: 折算率
stk = item.m_strInstrumentID + '.' + item.m_strExchangeID
assure_dict[stk] = item.m_dAssureRatio
print("当前担保品折算率列表:", assure_dict)
# 4. 根据折算率高低筛选优质标的(例如折算率 >= 0.7)
target_stocks = [stk for stk, ratio in assure_dict.items() if stk in ContextInfo.stocks and ratio >= 0.7]
if not target_stocks:
print("无满足折算率条件的标的")
return
# 5. 平均分配可用资金下单买入(使用信用账户担保品买入 opType=33)
# orderType=1123 表示单股、单账号、可用资金比例方式下单
weight = 1.0 / len(target_stocks)
for stk in target_stocks:
passorder(33, 1123, ContextInfo.accID, stk, 5, -1, weight, "两融折算率策略", 1, "", ContextInfo)
print(f"对标的 {stk} 按可用资金比例 {weight:.2f} 发起担保品买入")
def handlebar(ContextInfo):
pass
m_dPerAssurescaleValue),防止因市场大幅波动触及平仓线。get_assure_contract 能有效适应动态变化。