发送请求函数:ReqQryProduct

先查阅官方文档,可以知道以下重要信息:
1、需要发送的文件名是CThostFtdcQryProductField;
2、是否可作为过滤条件,意味着并非是必填项,可以根据需求筛选!
3、它的响应是:OnRspQryProduct
根据以上信息可以写出查询代码:
# 请求查询产品
def qryProduct(self):
# CthostFtdcMdSpi类提供了行情相关的回调接口,用户需要继承该类并重载这些接口,以获取响应数据。
queryFile = tdapi.CThostFtdcQryProductField()
# 请求查询产品# 响应: OnRspQryProduct
# 返回 # 0,代表成功。 # -1,表示网络连接失败; # -2,表示未处理请求超过许可数; # -3,表示每秒发送请求数超过许可数。
ret = self.tduserapi.ReqQryProduct(queryFile, 0)
if ret == 0:
print('发送查询产品成功!')
else:
print('发送查询产品失败!')
judge_ret(ret)
while ret != 0:
queryFile = tdapi.CThostFtdcQryProductField()
ret = self.tduserapi.ReqQryProduct(queryFile, 0)
print('正在查询产品...')
time.sleep(5)
time.sleep(1)
响应函数:OnRspQryProduct

返回的数据有很多,但根据我们的需求写出应答函数:
def OnRspQryProduct(self, pProduct, pRspInfo, nRequestID, bIsLast):
print(pProduct.ProductID)
print(pProduct.VolumeMultiple)
print(pProduct.PriceTick)
try:
sec = pProduct.ProductID
opt = '合约乘数'
# 需要判断section是否存在,如果不存在会报错,option不需要检查是否存在
if not g.productInfo.has_section(sec):
g.productInfo.add_section(sec)
g.productInfo.set(sec, opt, str(pProduct.VolumeMultiple))
opt = '最小变动价位'
g.productInfo.set(sec, opt, str(pProduct.PriceTick))
if bIsLast:
g.productInfo.write(open(g.productInfo_fileName, "w", encoding='utf-8'))
print('查询产品成功!')
except Exception as e:
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.CThostFtdcReqAuthenticateField()
authfield.BrokerID = str(self.Account.broker_id)
authfield.UserID = str(self.Account.investor_id)
authfield.AppID = str(self.Account.app_id)
authfield.AuthCode = str(self.Account.auth_code)
ret = self.tduserapi.ReqAuthenticate(authfield, 0)
if ret == 0:
print('发送穿透式认证请求成功!')
else:
print('发送穿透式认证请求失败!')
本主题为课程学员专享,成为股票量化投资课程学员后可免费阅读
成为学员