Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add the function of database recording transaction data to VNPY

Shulou Source: shulou.com Published: 2022-06-02 08:06:42 10月03日 Update

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.

Tags: Data transaction strategy function database name code content skills time knowledge or China plus concise successful concise event single only can be passed Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS Shulou Technology MariaDB Xiaomi