Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of insufficient position in CTP of VNPY

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain "how to solve the problem of insufficient position in CTP of VNPY". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to solve the problem of insufficient positions in VNPY's CTP yesterday".

In fact, I am still using the VNPY1.92 version, this "CTP: yesterday's insufficient position" appears in the VNPY1.92 version, and only occurs in the use of CtaTrading unattended program, but in the use of vnTrader this GUI version does not have this problem. Just take advantage of this problem to analyze the operation of VNPY yesterday.

Then we began to analyze the problem, uh, this is the issue of closing positions, and the distinction between today and yesterday is on several exchanges, such as the Shanghai Stock Exchange and Energy. So it's time to start debug, this debug is still very troublesome, only at noon on the trading day is more appropriate, the exchange to receive CTP requests will not generate transactions.

First of all, the breakpoint in the CtaEngine.sendOrder function, this convertOrderReq function is to convert general transaction requests.

# delegate conversion reqList = self.mainEngine.convertOrderReq (req)

two。 Then trace it all the way, and finally find that the PositionDetail.convertOrderReq method is finally called, and the PositionDetail class records the position information for a single position type. For example, if your account holds 3 varieties, there will be 3 PositionDetail instances.

The important thing in this is the self.mode attribute, which has three values, as shown below.

MODE_NORMAL = 'normal' # normal mode

MODE_SHFE = 'shfe' # the stock exchange closed its positions today and yesterday.

MODE_TDPENALTY = 'tdpenalty' # current punishment

Corresponding to the transaction request, including variety type, quantity, amount, Kaiping type attribute, Pingjinping yesterday is mainly different in Kaiping type.

As you can see from the following code, if self.mode is in normal mode, it directly returns the original incoming transaction request.

The opening type in the transaction request is the default OFFSET_CLOSE;. If it is MODE_SHFE, the two request will be split according to the position situation when the position is closed. Today's position closing type will use OFFSET_CLOSETODAY, and yesterday's position closing type will use OFFSET_CLOSEYESTERDAY.

Def convertOrderReq (self, req): "" conversion delegation request "" # normal mode does not need to convert if self.mode is self.MODE_NORMAL: return [req] # the mode split of the previous period was today and yesterday Priority today elif self.mode is self.MODE_SHFE: # No need to convert if req.offset is OFFSET_OPEN: return [req] # long if req.direction is DIRECTION_LONG: posAvailable = self.shortPos-self.shortPosFrozen tdAvailable = self.shortTd- self.shortTdFrozen ydAvailable = self.shortYd-self.shortYdFrozen # short else: posAvailable = self.longPos-self.longPosFrozen tdAvailable = self.longTd-self.longTdFrozen ydAvailable = self.longYd-self.longYdFrozen # closing exceeds the total available Reject, return empty list if req.volume > posAvailable: return [] # closing volume is less than available today All elif req.volume 0: reqTd = copy (req) reqTd.offset = OFFSET_CLOSETODAY reqTd.volume = tdAvailable l.append (reqTd) reqYd = copy (req) reqYd.offset = OFFSET_CLOSEYESTERDAY reqYd.volume = req.volume-tdAvailable L.append (reqYd) return lump.

3. Debug found that the variety PositionDetail.mode in the last period was MODE_NORMAL rather than MODE_SHFE. VNPY calls OFFSET_CLOSE, and at this time the CTP API returns the error message "CTP: insufficient position yesterday", which should be changed by default to yesterday.

4. Why is it wrong? continue debug and find that although the strategy position information is saved independently in the database, vnpy will also go to the exchange to inquire about the account position information at startup. This position information will be displayed in vnTrader and will also be used yesterday.

In 5.runCtaTrading.py, there is the following code to read and save the account position information. But this paragraph was accidentally deleted by me, because I did not adjust the code at night and accidentally deleted it. In this way, there is no initial account position information, and at this time the default configuration will be used, which is considered to be a general exchange. After recovery, OKay.

Sleep (10) # wait for the CTP interface to initialize me.dataEngine.saveContracts () # save the contract information to the file here. I believe you have a deeper understanding of "how to solve the shortage of CTP positions in VNPY". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report