因为需要实时获取某个合约的保证金率来计算建仓仓位,在CTP的接口文档中发现了reqQryInstrumentMarginRate()这个接口可以使用,就在VNPY进行了封装,具体修改内容如下:
1./vnpy_ctp/ctp_gateway.py/CtpTdApi中添加了
def onRspQryInstrumentMarginRate(self, data: dict, error: dict, reqid: int, last: bool): # liumengqing add
"""
查询保证金率
"""
print(f"MarginRate {data}")
print(f"error {error}")
if data:
margin = MarginData(
symbol = data['InstrumentID'],
exchange = data["ExchangeID"],
long_margin_rate=data["LongMarginRatioByMoney"],
long_margin_perlot=data["LongMarginRatioByVolume"],
short_margin_rate=data["ShortMarginRatioByMoney"],
short_margin_perlot=data["ShortMarginRatioByVolume"],
is_ralative=data['IsRelative'],
gateway_name=self.gateway_name
)
self.gateway.on_margin(margin)
def query_margin_ratio(self,req:MarginRequest): # liumngqing add
""" 保证金率查询 """
#保证金率查询字典
margin_ratio_req = {}
margin_ratio_req['BrokerID'] = self.brokerid
margin_ratio_req['InvestorID'] = self.userid
margin_ratio_req['InstrumentID'] = req.symbol
margin_ratio_req['ExchangeID'] = req.exchange.value
margin_ratio_req['HedgeFlag'] = THOST_FTDC_HF_Speculation
self.reqid += 1
#请求查询保证金率
count = 10
while self.reqQryInstrumentMarginRate(margin_ratio_req,self.reqid) != 0:
count -= 1
if count > 0:
sleep(0.100)
else:
break
2.在/vnpy_ctp/ctp_gateway.py/CtpGateway中增加
def query_margin_ratio(self,req:MarginRequest): # liumenqing add
"""查询保证金率数据"""
self.td_api.query_margin_ratio(req)
在CTP上发送请求状态是发送成功的,但一直没有回调,但在TTS上是能被回调的,是不是还有其它操作没做,还是说我参数传错了?