原创 +1银贝 期货CTP量化系列——查询持仓明细

小小的夏天 2023-11-12 144

前言:

查询持仓获得的是总持仓,那么查询持仓明细则得到持有的仓位中每一次开仓的明细!

发送请求函数:ReqQryInvestorPositionDetail

****

先查阅官方文档,可以知道以下重要信息:

1、需要发送的文件名是CThostFtdcQryInvestorPositionDetailField;

2、是否可作为过滤条件,意味着并非是必填项,其中重要的是合约代码,但一般来说我们更多的是查询固定合约的持仓明细

3、它的响应是:OnRspQryInvestorPositionDetail

根据以上信息可以写出查询代码:

    # CTP系统根据来自交易所的成交记录生成持仓明细记录,一笔成交记录对应一条持仓明细记录。
    def qryInvestorPositionDetail(self, InstrumentID=None):
        qryFile = tdapi.CThostFtdcQryInvestorPositionDetailField()
        qryFile.BrokerID = str(self.Account.broker_id)  # 经纪公司代码
        qryFile.InvestorID = str(self.Account.investor_id)  # 投资者代码
        qryFile.InstrumentID = InstrumentID  # 合约代码

        ret = self.tduserapi.ReqQryInvestorPositionDetail(qryFile, 0)
        if ret == 0:
            print('发送查询持仓明细成功!')
        else:
            print('发送查询持仓明细失败!')
            judge_ret(ret)
            while ret != 0:
                qryFile = tdapi.CThostFtdcQryInvestorPositionDetailField()
                ret = self.tduserapi.ReqQryInvestorPositionDetail(qryFile, 0)
                print('正在查询持仓明细...')
                time.sleep(5)
        time.sleep(1)

响应函数:OnRspQryInvestorPositionDetail

在官方文档里我们可以得到以下重要信息:

1、函数原型中带bIsLast,指示该次返回是否为最后一次返回,意思是返回多条数据,其中if bIsLast为真时,则已经查询结束,并返回最后一条数据,这里再详细写一下它写不写的区别。

2、返回的存储信息为pInvestorPositionDetail,返回的信息有很多,但我们这里只要自己想要的即可。

不写if bIsLast:的写法(这里只是范例,要根据策略改写代码):

        # pInvestorPosition:投资者持仓
    def OnRspQryInvestorPositionDetail(self, pInvestorPositionDetail, pRspInfo, nRequestID, bIsLast):
        try:
            pInvestorPositionDetail1 = {
            '交易所代码': pInvestorPositionDetail.ExchangeID,
            '买卖': pInvestorPositionDetail.Direction,
            '合约代码':  pInvestorPositionDetail.InstrumentID,
            '数量':  pInvestorPositionDetail.Volume,
            '开仓价':   pInvestorPositionDetail.OpenPrice,
            '开仓日期':    pInvestorPositionDetail.OpenDate,
        }
            print(pInvestorPositionDetail1)
        except Exception as e:
            print(e)

结果展示:

返回每次下单的明细。

写if bIsLast:的写法:

    def OnRspQryInvestorPositionDetail(self, pInvestorPositionDetail, pRspInfo, nRequestID, bIsLast):
        try:
            pInvestorPositionDetail1 = {
            '交易所代码': pInvestorPositionDetail.ExchangeID,
            '买卖': pInvestorPositionDetail.Direction,
            '合约代码':  pInvestorPositionDetail.InstrumentID,
            '数量':  pInvestorPositionDetail.Volume,
            '开仓价':   pInvestorPositionDetail.OpenPrice,
            '开仓日期':    pInvestorPositionDetail.OpenDate,
        }
            # print(pInvestorPositionDetail1)
            if bIsLast:
                print(pInvestorPositionDetail1)
        except Exception as e:
            print(e)

结果展示:

则返回最后一次下单明细。

完整代码:

import json
from program.function import *



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)
        a

本主题为课程学员专享,成为股票量化投资课程学员后可免费阅读

成为学员

上传的附件:

最新回复 ( 0条评论 )


官方微信
码力十足学量化
Powered by Xiuno BBS 4.0.7

官方微信