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 add the function of database recording transaction data to VNPY

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you how to add the function of recording transaction data in the database for VNPY. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

In VNPY, the actual transaction database recording function is not provided, although it can be read through the third-party transaction software, but it is still not convenient to verify the transaction effect of a single strategy. Here is the following to add database recording transaction data function for VNPY.

In fact, the main thing is to call the functions already provided by VNPY.

1. Add a new database to vnpy.trader.app.ctaStrategy.ctaBase.

Give names according to your habits, such as DEAL_DB_NAME = 'VnTrader_DEAL_Db'

two。 In vnpy.trader.app.ctaStrategy.ctaTemplate, add a new function.

Def insertData (self, dbName, collectionName, data): if self.trading: self.ctaEngine.insertData (dbName, collectionName, data)

3. Add the following code to the policy onTrade function and use the policy name as the collection name

Def onTrade (self, trade): # send status update event "receive transaction push (must be inherited by the user)" # for policies that do not require fine-grained delegation control, you can ignore onOrder t = trade self.insertData (DEAL_DB_NAME,self.name,t)

In the policy code, if there is no reference to DEAL_DB_NAME, it will not succeed, please add a reference to the policy

From vnpy.trader.app.ctaStrategy.ctaBase import DEAL_DB_NAME

Or in

Self.insertData (DEAL_DB_NAME,self.name,t)

Just write death, "DEAL_DB_NAME."

It is found that the transaction time tradeTime for writing data has only time and no date, so it is not convenient for analysis. Open the source code, it is relatively simple to modify vnpy.trader.app.ctaStrategy.ctaTemplate.

If the inserted data is found to be a VtTradeData object, modify the tradeTime

From vnpy.trader.vtObject import VtTradeDatafrom datetime import datetimedef insertData (self, dbName, collectionName, data): if self.trading: if isinstance (data,VtTradeData): data.tradeTime = datetime.now (). Strftime ('% Y-%m-%d% HV% MV% S') self.ctaEngine.insertData (dbName, collectionName, data) the above is how to add database transaction data recording function to VNPY. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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