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

小小的夏天 2023-11-11 131

发送请求函数:ReqQryInvestorPosition

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

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

2、是否可作为过滤条件,意味着并非是必填项,其中重要的是合约代码,意味着它既可以查询特定合约的持仓和所有持仓

3、它的响应是:OnRspQryInvestorPosition

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

    # 请求查询投资者持仓,对应响应 OnRspQryInvestorPosition。CTP系统将持仓明细记录按合约,持仓方向,开仓日期(仅针对上期所和能源所,区分昨仓、今仓)进行汇总。
    def reqQryInvestorPosition(self, InstrumentID=None):
        qryFile = tdapi.CThostFtdcQryInvestorPositionField()
        qryFile.BrokerID = str(self.Account.broker_id)  # 经纪公司代码
        qryFile.InvestorID = str(self.Account.investor_id)  # 投资者代码
        qryFile.InstrumentID = InstrumentID  # 合约代码

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

响应函数:OnRspQryInvestorPosition

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

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

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

不写if bIsLast:的写法

    def OnRspQryInvestorPosition(self, pInvestorPosition: 'CThostFtdcInvestorPositionField', pRspInfo: 'CThostFtdcRspInfoField', nRequestID: 'int', bIsLast: 'bool') -> "void":
        try:
            pInvestorPosition = {
                '交易所代码': pInvestorPosition.ExchangeID,
                '合约代码': pInvestorPosition.InstrumentID,
                '持仓多空方向': pInvestorPosition.PosiDirection,
                '当前持仓数量': pInvestorPosition.Position,
                '持仓成本': pInvestorPosition.PositionCost,
                '手续费': pInvestorPosition.Commission,
                '平仓盈亏': pInvestorPosition.CloseProfit,
                '持仓盈亏': pInvestorPosition.PositionProfit,
                '开仓成本': pInvestorPosition.OpenCost,
                '交易所保证金': pInvestorPosition.ExchangeMargin,
                '逐日盯市平仓盈亏': pInvestorPosition.CloseProfitByDate,
                '逐笔对冲平仓盈亏': pInvestorPosition.CloseProfitByTrade,
            }
            print(pInvestorPosition)
        except Exception as e:
            print(e)

结果展示:

返回所有持仓。

写if bIsLast:的写法

    def OnRspQryInvestorPosition(self, pInvestorPosition: 'CThostFtdcInvestorPositionField', pRspInfo: 'CThostFtdcRspInfoField', nRequestID: 'int', bIsLast: 'bool') -> "void":
        try:
            pInvestorPosition = {
                '交易所代码': pInvestorPosition.ExchangeID,
                '合约代码': pInvestorPosition.InstrumentID,
                '持仓多空方向': pInvestorPosition.PosiDirection,
                '当前持仓数量': pInvestorPosition.Position,
                '持仓成本': pInvestorPosition.PositionCost,
                '手续费': pInvestorPosition.Commission,
                '平仓盈亏': pInvestorPosition.CloseProfit,
                '持仓盈亏': pInvestorPosition.PositionProfit,
                '开仓成本': pInvestorPosition.OpenCost,
                '交易所保证金': pInvestorPosition.ExchangeMargin,
                '逐日盯市平仓盈亏': pInvestorPosition.CloseProfitByDate,
                '逐笔对冲平仓盈亏': pInvestorPosition.CloseProfitByTrade,
            }
            # print(pInvestorPosition)
            if bIsLast:
                print(pInvestorPosition)
        except Exception as e:
            print(e)

结果展示:

返回最后一个持仓,这里要特别注意!一般用第一种写法。

查询某一合约持仓:

 ctp_api.reqQryInvestorPosition(InstrumentID='FG401')  # 查询单独持仓

完整代码:

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.CThostFtdcTra

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

成为学员

上传的附件:

最新回复 ( 0条评论 )


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

官方微信