发送请求函数:ReqQryInstrumentCommissionRate

先查阅官方文档,可以知道以下重要信息:
1、需要发送的文件名是CThostFtdcQryInstrumentCommissionRateField;
2、是否可作为过滤条件,意味着并非是必填项,可以根据自己需求过滤
3、它的响应是:OnRspQryInstrumentCommissionRate
根据以上信息可以写出查询代码:
# 请求查询合约手续费率,对应响应 OnRspQryInstrumentCommissionRate。如果InstrumentID填空,则返回持仓对应的合约手续费率。
def qryInstrumentCommissionRate(self, code):
qryFile = tdapi.CThostFtdcQryInstrumentCommissionRateField()
qryFile.BrokerID = str(self.Account.broker_id)
qryFile.InvestorID = str(self.Account.investor_id)
qryFile.InstrumentID = code
# 请求查询合约手续费率,对应响应 OnRspQryInstrumentCommissionRate。如果InstrumentID填空,则返回持仓对应的合约手续费率。
ret = self.tduserapi.ReqQryInstrumentCommissionRate(qryFile, 0)
if ret == 0:
timePrintLog('发送查询手续费成功!')
else:
timePrintLog('发送查询手续费失败!')
judge_ret(ret)
while ret != 0:
qryFile = tdapi.CThostFtdcQryInstrumentCommissionRateField()
qryFile.BrokerID = str(self.Account.broker_id)
qryFile.InvestorID = str(self.Account.investor_id)
qryFile.InstrumentID = code
ret = self.tduserapi.ReqQryInstrumentCommissionRate(qryFile, 0)
timePrintLog('正在查询手续费...')
time.sleep(5)
time.sleep(1)
响应函数:OnRspQryInstrumentCommissionRate

根据参数选择自己需要的数据,响应代码示例(保存到本地):
# 请求查询合约手续费率响应,当执行ReqQryInstrumentCommissionRate后,该方法被调用。
def OnRspQryInstrumentCommissionRate(self, pInstrumentCommissionRate, pRspInfo, nRequestID, bIsLast):
try:
print(f'合约名称:{pInstrumentCommissionRate.InstrumentID}')
print(f'开仓手续费率:{pInstrumentCommissionRate.OpenRatioByMoney}')
print(f'开仓手续费:{pInstrumentCommissionRate.OpenRatioByVolume}')
print(f'平仓手续费率:{pInstrumentCommissionRate.CloseRatioByMoney}')
print(f'平仓手续费:{pInstrumentCommissionRate.CloseRatioByVolume}')
sec = pInstrumentCommissionRate.InstrumentID
# 需要判断section是否存在,如果不存在会报错,option不需要检查是否存在
if not g.productInfo.has_section(sec):
g.productInfo.add_section(sec)
# 填写开仓手续费率
opt = '开仓手续费率'
g.productInfo.set(sec, opt, str(pInstrumentCommissionRate.OpenRatioByMoney))
# 填写开仓手续费
opt = '开仓手续费'
g.productInfo.set(sec, opt, str(pInstrumentCommissionRate.OpenRatioByVolume))
# 填写平仓手续费率
opt = '平仓手续费率'
g.productInfo.set(sec, opt, str(pInstrumentCommissionRate.CloseRatioByMoney))
# 填写平仓手续费
opt = '平仓手续费'
g.productInfo.set(sec, opt, str(pInstrumentCommissionRate.CloseRatioByVolume))
# 填写平今手续费率
opt = '平今手续费率'
g.productInfo.set(sec, opt, str(pInstrumentCommissionRate.CloseTodayRatioByMoney))
# 填写平今手续费
opt = '平今手续费'
g.productInfo.set(sec, opt, str(pInstrumentCommissionRate.CloseTodayRatioByVolume))
# 写入ini文件
g.productInfo.write(open(g.productInfo_fileName, "w", encoding='utf-8'))
except Exception as e:
red_print(e)
保存的位置:

结果展示:

完整代码:
import json
import time
from threading import Thread
from API import thosttraderapi as tdapi # 交易接口
from API import thostmduserapi as mdapi # 行情接口
import program.Global as g # 全局变量
from program.function import *
import traceback
class CTraderSpi(tdapi.CThostFtdcTraderSpi):
def __init__(self, tduserapi, Account):
tdapi.CThostFtdcTraderSpi.__init__(self)
self.tduserapi = tduserapi
self.Account = Account
# 连接前台
def OnFrontConnected(self):
print("开始建立交易连接")
authfield = tdapi.CThostFtdcReqAuthenticate
本主题为课程学员专享,成为股票量化投资课程学员后可免费阅读
成为学员