Policy backtesting method, device and equipment and storage medium
By implementing the strategy backtest engine as a database plug-in in the DolphinDB database, and combining the responsive state engine and simulation matching plug-in, the problem of low strategy backtesting efficiency in high-frequency trading scenarios is solved, and more efficient trading strategy development and optimization is achieved.
Patent Information
- Application Number
- CN202411890411.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-20
- Publication Date
- 2025-04-11
AI Technical Summary
When conducting strategy backtesting in high-frequency trading scenarios, there are problems of low efficiency and low efficiency in trading strategy development and optimization.
Implement the policy backtest engine as a database plug-in in the DolphinDB database, and combine it with the responsive state engine to perform factor calculations. Use the DolphinDB scripting language to write transaction strategy codes, and call the responsive state engine and simulated matching plug-in through the policy backtest plug-in.
It reduces the complexity of use, reduces data transmission overhead, improves the efficiency of trading strategy writing and debugging, and improves the efficiency of strategy backtesting and processing in high-frequency trading scenarios.
Smart Images

Figure CN119806500A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a strategy backtesting method, device, equipment and storage medium. Background Art
[0002] Strategy backtesting engine is an important technical tool that can be applied in a variety of technical fields. For example, in the financial field, the effectiveness of trading strategies can be tested and verified through historical market data based on strategy backtesting engine.
[0003] In the prior art, users generally write specialized trading strategy codes and create a strategy backtesting engine using a programming language, then input historical market data into the strategy backtesting engine, and test and verify the trading strategy through the strategy backtesting engine, observing the profit indicators of the trading strategy developed under past market conditions, thereby evaluating the performance and risk of the trading strategy.
[0004] However, in the context of high-frequency trading, the existing strategy backtesting engine needs to process a large amount of data and perform a large amount of calculations, and the use of existing technologies for strategy backtesting has the problem of low efficiency. In addition, when using existing strategy backtesting software, the user writes the trading strategy code, and the user needs to have certain programming and debugging capabilities, which also reduces the efficiency of trading strategy development and optimization. Summary of the invention
[0005] The purpose of this application is to provide a strategy backtesting method, device, equipment and storage medium to address the deficiencies in the above-mentioned prior art, so as to solve the problems of low efficiency in strategy backtesting and low efficiency in trading strategy development and optimization in the prior art.
[0006] To achieve the above objectives, the technical solutions adopted in this application are as follows:
[0007] In a first aspect, the present application provides a strategy backtesting method, which is applied to a DolphinDB database, wherein the DolphinDB database includes: a strategy backtesting plug-in, a responsive state engine, and a simulated matching plug-in, and the method includes:
[0008] Obtain the strategy backtest configuration information and the trading strategy code of the current trading strategy, wherein the strategy backtest configuration information includes: backtest type and backtest frequency information, and the trading strategy code is written based on the DolphinDB scripting language;
[0009] Create a strategy backtesting plug-in for the current trading strategy based on the strategy backtesting configuration information and the trading strategy code;
[0010] Acquire multiple backtest data, and input the backtest data into the strategy backtest plug-in in sequence, and the strategy backtest plug-in calls the responsive state engine and the simulated matching plug-in in sequence according to each backtest data, and triggers the callback function of the strategy backtest plug-in to update the current order information, wherein the backtest data includes: historical market data;
[0011] When the input of the plurality of backtest data is completed, the strategy backtest result of the current trading strategy is determined according to the updated current order information.
[0012] Optionally, the strategy backtesting plug-in sequentially calls the responsive state engine and the simulated matching plug-in according to each of the backtesting data, and triggers the execution of the callback function of the strategy backtesting plug-in to update the current order information, including:
[0013] The strategy backtest plug-in calls the responsive state engine according to the backtest data, processes the backtest data based on the responsive state engine, and obtains indicator data of the backtest data;
[0014] The strategy backtest plug-in calls the simulated matching plug-in according to the backtest data, performs order trading based on the simulated matching plug-in, obtains order trading results, and updates the current order information according to the order trading results;
[0015] The callback function is executed by triggering the indicator data of the backtest data, the current order information and the backtest data, the callback function generates order information or order cancellation information, and the current order information is updated based on the order information or order cancellation information.
[0016] Optionally, the strategy backtest plug-in calls the responsive state engine according to the backtest data, processes the backtest data based on the responsive state engine, and obtains indicator data of the backtest data, including:
[0017] If the backtest data includes market data to be calculated, the responsive state engine is called, and the market data to be calculated is processed according to the responsive state engine to obtain indicator data of the market data to be calculated.
[0018] Optionally, the strategy backtest plug-in calls the simulated matching plug-in according to the backtest data, performs order trading based on the simulated matching plug-in, and obtains the order trading result, including:
[0019] If the backtest data includes order information, the simulated matching plug-in is called, and the simulated matching plug-in performs order transactions based on the order information, the backtest type, and the backtest frequency information to obtain an order transaction result of the order information.
[0020] Optionally, the callback function includes: a market callback function, an order callback function and a trading day callback function;
[0021] The indicator data of the backtest data includes: order information, trading day information and market data;
[0022] The triggering and executing of the callback function by the indicator data of the backtest data, the current order information and the backtest data, generating order information or order cancellation information by the callback function, and updating the current order information based on the order information or order cancellation information, includes:
[0023] When the order information or the current order information triggers the order callback function, the order callback function generates order information or order cancellation information based on the order information or the current order information;
[0024] When the trading day information triggers the trading day callback function, the trading day callback function generates the order information or order cancellation information based on the backtest data;
[0025] When the market data triggers the market callback function, the market callback function generates the order information or order cancellation information based on the market data.
[0026] Optionally, the callback function further includes: an initialization callback function, which is triggered when the backtest data starts to be input and initializes the custom variables.
[0027] Optionally, the strategy backtesting plug-in includes: at least one data acquisition interface;
[0028] The obtaining of backtest data includes:
[0029] The backtest data is obtained through the data acquisition interface, and the backtest data includes: position information, account equity indicators, transaction order information, available funds information and market price information.
[0030] In a second aspect, the present application provides a strategy backtesting device, the device comprising:
[0031] An acquisition module is used to acquire the strategy backtest configuration information and the trading strategy code of the current trading strategy. The strategy backtest configuration information includes: backtest type and backtest frequency information. The trading strategy code is written based on the DolphinDB scripting language.
[0032] A creation module, used to create a strategy backtesting plug-in for the current trading strategy based on the strategy backtesting configuration information and the trading strategy code;
[0033] A backtesting module, used to obtain a plurality of backtesting data, and sequentially input the backtesting data into the strategy backtesting plug-in, wherein the strategy backtesting plug-in sequentially calls the responsive state engine and the simulated matching plug-in according to each of the backtesting data, and triggers the callback function of the strategy backtesting plug-in to update the current order information, wherein the backtesting data includes: historical market data;
[0034] A determination module is used to determine the strategy backtesting result of the current trading strategy according to the updated current order information when the input of the multiple backtesting data is completed.
[0035] Optionally, the backtest module is specifically used for:
[0036] The strategy backtest plug-in calls the responsive state engine according to the backtest data, processes the backtest data based on the responsive state engine, and obtains indicator data of the backtest data;
[0037] The strategy backtest plug-in calls the simulated matching plug-in according to the backtest data, performs order trading based on the simulated matching plug-in, obtains order trading results, and updates the current order information according to the order trading results;
[0038] The callback function is executed by triggering the indicator data of the backtest data, the current order information and the backtest data, the callback function generates order information or order cancellation information, and the current order information is updated based on the order information or order cancellation information.
[0039] Optionally, the backtest module is specifically used for:
[0040] If the backtest data includes market data to be calculated, the responsive state engine is called, and the market data to be calculated is processed according to the responsive state engine to obtain indicator data of the market data to be calculated.
[0041] Optionally, the backtest module is specifically used for:
[0042] If the backtest data includes order information, the simulated matching plug-in is called, and the simulated matching plug-in performs order transactions based on the order information, the backtest type, and the backtest frequency information to obtain an order transaction result of the order information.
[0043] Optionally, the callback function includes: a market callback function, an order callback function and a trading day callback function;
[0044] The indicator data of the backtest data includes: order information, trading day information and market data;
[0045] The backtest module is specifically used for:
[0046] When the order information or the current order information triggers the order callback function, the order callback function generates order information or order cancellation information based on the order information or the current order information;
[0047] When the trading day information triggers the trading day callback function, the trading day callback function generates the order information or order cancellation information based on the backtest data;
[0048] When the market data triggers the market callback function, the market callback function generates the order information or order cancellation information based on the market data.
[0049] Optionally, the callback function further includes: an initialization callback function, which is triggered when the backtest data starts to be input and initializes the custom variables.
[0050] Optionally, the strategy backtesting plug-in includes: at least one data acquisition interface;
[0051] The backtest module is specifically used for:
[0052] The backtest data is obtained through the data acquisition interface, and the backtest data includes: position information, account equity indicators, transaction order information, available funds information and market price information.
[0053] In a third aspect, an embodiment of the present application further provides an electronic device, comprising: a processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor communicates with the storage medium through the bus, and the processor executes the machine-readable instructions to perform the steps of the strategy backtesting method as described in any one of the first aspects.
[0054] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the strategy backtesting method as described in any one of the first aspects are executed.
[0055] The beneficial effects of the present application are: the strategy backtesting engine is implemented in the database in the form of a plug-in and runs in the same process as the database, which reduces the complexity of use and reduces the overhead of data transmission. Since the DolphinDB scripting language is more intuitive and easy to understand, it reduces the difficulty and development cost of users writing trading strategy codes. At the same time, the scripting language in DolphinDB can achieve instant compilation, which also improves the operating efficiency of the scripting language. By calling the responsive state engine and the simulated matching plug-in through the strategy backtesting plug-in, calculations can be quickly triggered and calculation results can be obtained in the scenario of high-frequency trading, which improves the processing efficiency of the strategy backtesting plug-in.
[0056] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0057] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0058] Figure 1 A schematic diagram of the architecture of a DolphinDB database provided in an embodiment of the present application is shown;
[0059] Figure 2 A flow chart of a strategy backtesting method provided by an embodiment of the present application is shown;
[0060] Figure 3 A flow chart showing data processing inside a strategy backtesting plug-in provided in an embodiment of the present application is shown;
[0061] Figure 4 A flow chart of a strategy backtesting plug-in calling a callback function provided by an embodiment of the present application is shown;
[0062] Figure 5 An overall flow chart of a strategy backtesting method provided by an embodiment of the present application is shown;
[0063] Figure 6 A schematic diagram of the structure of a strategy backtesting device provided in an embodiment of the present application is shown;
[0064] Figure 7 A schematic structural diagram of an electronic device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0065] To make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments. The components of the embodiments of the present application generally described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present application.
[0066] It should be noted that the term "comprising" will be used in the embodiments of the present application to indicate the existence of the features declared thereafter, but does not exclude the addition of other features.
[0067] When conducting strategy backtesting, historical market data can be input into the strategy backtesting engine. The strategy backtesting engine can simulate the performance of the trading strategy in the historical period based on the historical market data, evaluate the pros and cons of the trading strategy according to the final test results, identify potential problems in the trading strategy and make adjustments before actual trading.
[0068] In order to test and verify trading strategies, in the prior art, users generally write special trading strategy codes and create a strategy backtesting engine using a programming language, then input historical market data into the strategy backtesting engine, and test and verify the trading strategies through the strategy backtesting engine, observing the profit indicators of the trading strategies they have formulated under past market conditions, thereby evaluating the performance and risks of the trading strategies.
[0069] However, writing trading strategy codes requires users to have certain programming and debugging capabilities, which results in a high threshold for using the backtesting engine and limits its application to a wider user group. If the writing process of trading strategies cannot be simplified, it will hinder user use and affect the development and optimization efficiency of trading strategies.
[0070] On the other hand, backtesting a trading strategy may involve processing a large amount of trading data. Strategy backtesting software requires a lot of computing resources, especially in high-frequency trading strategies. A large amount of data and calculations will directly lead to a longer backtesting time, which not only delays the verification of trading strategies, but also increases computing costs. Therefore, in the context of high-frequency trading, how to improve backtesting efficiency has become an urgent problem to be solved.
[0071] Based on this, this application proposes a strategy backtesting method, which implements a strategy backtesting engine in the form of a database plug-in in the DolphinDB database, and combines the responsive state engine to perform factor calculations on the data, which can improve the efficiency of strategy backtesting in high-frequency trading scenarios. In addition, the trading strategy is implemented based on the DolphinDB scripting language, which can reduce the difficulty of users writing trading strategy codes and improve the usability of the strategy backtesting plug-in.
[0072] like Figure 1 , which is a schematic diagram of the architecture of a DolphinDB database provided in this application. Figure 1 The DolphinDB database includes: strategy backtesting plug-in, responsive state engine, and simulated matching plug-in. After receiving the historical market data, the strategy backtesting plug-in can call the responsive state engine and simulated matching components based on the historical market data to perform testing and transaction verification, and obtain the final transaction strategy verification results.
[0073] Next, combine Figure 2 , the strategy backtesting method of this application is explained, and this method can be applied to Figure 1 In the DolphinDB database shown. Figure 2 , the method comprising:
[0074] S201. Obtain strategy backtesting configuration information and trading strategy code of the current trading strategy. The strategy backtesting configuration information includes: backtesting type and backtesting frequency information. The trading strategy code is written based on the DolphinDB scripting language.
[0075] Trading strategies refer to a series of plans and methods developed by traders to achieve profit goals. There are many types of trading strategies for different usage scenarios, such as trend tracking strategies, arbitrage strategies, day trading strategies, and volatility trading strategies.
[0076] Users can write trading strategy codes and strategy backtesting configuration information based on the DolphinDB scripting language. Among them, the strategy backtesting configuration information can also be provided by the graphical user interface with configuration options, and users can generate the strategy backtesting configuration information of the current trading strategy by selecting the configuration options. The DolphinDB scripting language is highly readable, concise and flexible, which can improve the efficiency of writing and debugging trading strategies.
[0077] The backtest type can be the type of transaction to be backtested, including stocks, margin trading, futures and options, etc. The backtest frequency information is used to indicate the frequency of the backtest, including high-frequency backtest mode, medium-frequency backtest mode and low-frequency backtest mode. The backtest frequency can indicate the frequency of backtest data input and data calculation during the backtest process.
[0078] The trading strategy code is written in the form of a function, and the trading strategy code includes multiple callback functions. The input parameters of the callback function can be the data input into the strategy backtesting plug-in, as well as the intermediate data generated by the strategy backtesting plug-in during the backtesting process. The output parameter of the backtesting function can be order information. Each callback function is used to be triggered and executed based on the input data and generate order information.
[0079] S202. Create a strategy backtesting plug-in for the current trading strategy based on the strategy backtesting configuration information and the trading strategy code.
[0080] Among them, the strategy backtesting plug-in and the database run in the same process.
[0081] Optionally, based on the strategy backtesting configuration information and the trading strategy code, a strategy backtesting plug-in for the current trading strategy can be created using a preset interface to perform strategy backtesting on the current trading strategy through the created strategy backtesting plug-in.
[0082] It is worth noting that when backtesting each trading strategy, the strategy backtesting configuration information and trading strategy code may be different. When it is necessary to backtest multiple trading strategies, you can call the preset interface, create a strategy backtesting plug-in corresponding to each trading strategy, and backtest each trading strategy based on each strategy backtesting plug-in.
[0083] In the DolphinDB database, the preset interface can be the createBacktestEngine interface. After creating a strategy backtesting plug-in based on the createBacktestEngine interface, you can call the subscribeIndicator interface, pass in an expression or function as a parameter, and start backtesting the trading strategy.
[0084] S203, obtaining multiple backtest data, and inputting the backtest data into the strategy backtest plug-in in turn, the strategy backtest plug-in calls the responsive state engine and the simulation matching plug-in in turn according to each backtest data, and triggers the callback function of the execution strategy backtest plug-in to update the current order information. The backtest data includes: historical market data.
[0085] The backtest data can be historical market data, which is market data over a period of time in the past. After the historical market data is input into the strategy backtest plug-in in sequence, the strategy backtest plug-in can simulate the trading process based on the input historical market data. After the historical market data is input, the trading results can be used as the strategy backtest results, reflecting the performance of the trading strategy under the historical market data. The performance of the trading strategy can be verified and evaluated based on the backtest results.
[0086] The responsive state engine includes multiple state operators, such as sliding window functions, cumulative functions, sequence-related functions, and topN-related functions. After receiving historical market data, the responsive state engine can quickly trigger the internal state operators to perform streaming calculations and obtain market indicators of historical market data. This greatly improves the efficiency of factor calculations, so that when receiving market data, the strategy backtesting engine can quickly trigger calculations and obtain results, improving the computing power and performance of the entire system.
[0087] When inputting backtest data into the strategy backtest plug-in, the backtest data can be input based on the backtest frequency information and the backtest type. Specifically, if the backtest type is stock and the backtest frequency information is high-frequency tick-by-tick mode, the historical market data of the stock can be used as the backtest data, and the backtest data can be input into the strategy backtest plug-in at a high frequency.
[0088] In the first implementation, historical market data can be replayed based on the backtest frequency information and the backtest type. Specifically, static historical market data can be written into the strategy backtest plug-in in batches and continuously according to the event time sequence to simulate the historical market data.
[0089] In the second implementation method, historical market data can be inserted into the market data table, and the strategy backtesting engine itself reads data from the market data table according to the backtesting frequency information and the backtesting type to realize data simulation of the historical market data.
[0090] After receiving the backtest data, the strategy backtest plug-in can call the responsive state engine to perform streaming calculations on the backtest data, call the simulated matching plug-in to trade the orders in the backtest data, trigger the execution of the callback function to generate or cancel orders, and update the current order information according to the processing results of the callback function.
[0091] Among them, the current order information can record the basic information and order status of all orders. The basic information of the order includes the transaction volume and transaction price of the order, and the order status is used to indicate whether the order is executed. For the executed orders, the revenue of the order and the transaction details of the order can be recorded, such as the transaction volume, transaction price, transaction time, etc. For the unexecuted orders, the expected transaction volume and expected transaction price of the order can be recorded.
[0092] As a possible implementation method, the strategy backtesting plug-in can also call a third-party platform to perform order transactions, that is, the function of the above-mentioned simulation matching component can be replaced by a third-party platform.
[0093] S204. When the input of multiple backtest data is completed, determine the strategy backtest result of the current trading strategy according to the updated current order information.
[0094] Optionally, after all backtest data input is completed, an end flag can be input into the strategy backtest plug-in to notify the strategy backtest plug-in that the backtest is finished.
[0095] The updated current order information includes all order information after the simulated transaction of the backtest data. After all the backtest data are input into the strategy backtest engine and the calculation is completed, the strategy backtest result of the current trading strategy can be determined based on the updated current order information.
[0096] As a possible implementation method, the order transaction details can be recorded according to the updated current order information, and the equity, position data, profit overview, etc. of each account in the backtest data can be updated, and the order transaction details, the equity, position data, and profit overview of each account in the backtest data can be used as the strategy backtesting results of the current trading strategy.
[0097] It is worth noting that users can determine the effectiveness of the current trading strategy based on the strategy backtesting results and expected results, and optimize the current trading strategy.
[0098] In an embodiment of the present application, strategy backtesting configuration information and trading strategy code of the current trading strategy are obtained, the strategy backtesting configuration information includes: backtesting type, backtesting frequency information, the trading strategy code is written based on the DolphinDB scripting language, a strategy backtesting plug-in of the current trading strategy is created based on the strategy backtesting configuration information and the trading strategy code, a plurality of backtesting data are obtained, and the backtesting data are sequentially input into the strategy backtesting plug-in, the strategy backtesting plug-in sequentially calls the responsive state engine and the simulation matching plug-in according to each backtesting data, and triggers the execution of the callback function of the strategy backtesting plug-in to update the current order information, the backtesting data includes: historical market data, and when the input of the plurality of backtesting data is completed, the strategy backtesting result of the current trading strategy is determined according to the updated current order information.
[0099] The strategy backtesting engine is implemented in the database as a plug-in and runs in the same process as the database, which reduces the complexity of use and reduces the overhead of data transmission. Since the DolphinDB scripting language is more intuitive and easy to understand, it reduces the difficulty and development cost of users writing trading strategy codes. At the same time, the scripting language in DolphinDB can be compiled in real time, which also improves the running efficiency of the scripting language. By calling the responsive state engine and the simulated matching plug-in through the strategy backtesting plug-in, calculations can be quickly triggered and calculation results can be obtained in high-frequency trading scenarios, which improves the processing efficiency of the strategy backtesting plug-in.
[0100] The following is a further explanation of the above-mentioned strategy backtest plug-in calling the responsive state engine and the simulated matching plug-in in turn according to each backtest data, and triggering the callback function of the execution strategy backtest plug-in to update the current order information. Figure 3 As shown, the above step S203 includes:
[0101] S301. The strategy backtesting plug-in calls the responsive state engine according to the backtesting data, processes the backtesting data based on the responsive state engine, and obtains the indicator data of the backtesting data.
[0102] It is worth noting that when creating a strategy backtesting plug-in, you can first determine whether the strategy backtesting plug-in has subscribed to the market indicator. If the strategy backtesting plug-in has subscribed to the market indicator data, then when the backtesting data is input into the strategy backtesting plug-in, the backtesting data can be processed based on the responsive state engine to obtain the indicator data of the market indicator, and the indicator data is returned to the strategy backtesting plug-in to trigger the callback function inside the strategy backtesting plug-in.
[0103] S302: The strategy backtesting plug-in calls the simulated matching plug-in according to the backtesting data, performs order trading based on the simulated matching plug-in, obtains the order trading result, and updates the current order information according to the order trading result.
[0104] The strategy backtesting plug-in can transmit the backtesting data to the simulation matching plug-in. After receiving the backtesting data, the simulation matching plug-in can perform order transactions based on the backtesting data and return the order information of the completed order to the strategy backtesting plug-in. The strategy backtesting plug-in will update the current order information according to the order transaction results.
[0105] It should be noted that the strategy backtesting plug-in can also call the simulated matching plug-in according to the updated order information. Specifically, if the updated order information includes newly generated untraded orders, the strategy backtesting plug-in can call the simulated matching plug-in and entrust the untraded orders to the simulated matching plug-in for trading.
[0106] S303, triggering the execution of a callback function through the indicator data of the backtest data, the current order information and the backtest data, generating order information or order cancellation information by the callback function, and updating the current order information based on the order information or order cancellation information.
[0107] Optionally, the order information may be an order that needs to be traded, generated by the callback function based on the indicator data, backtest data or the current order information. The order cancellation information may be an order that needs to be cancelled, generated by the callback function based on the indicator data, backtest data or the current order information. The callback function may add an order that needs to be traded to the current order information based on the order information, and delete an order that needs to be cancelled from the current order information based on the order cancellation information, thereby updating the current order information.
[0108] It should be understood that there is no strict sequence relationship between the above steps S301-S303. After the backtest data is input into the strategy backtest engine, the callback function can be triggered to execute and / or the responsive state engine and / or the simulation matching engine can be called, which depends on the content in the backtest data. The execution order between steps S301-S303 should not be limited by the above description.
[0109] Furthermore, the process of the strategy backtesting plug-in calling the responsive state engine according to the backtesting data, processing the backtesting data based on the responsive state engine, and obtaining the indicator data of the backtesting data includes:
[0110] If the backtest data includes market data to be calculated, the responsive state engine is called, and the market data to be calculated is processed according to the responsive state engine to obtain the indicator data of the market data to be calculated.
[0111] If the strategy backtesting engine subscribes to market indicators, that is, the input parameters of the callback function of the strategy backtesting engine include indicator data, a responsive state engine can be created. After inputting historical market data, the strategy backtesting plug-in calls the responsive state engine to perform streaming calculations on the market data to be calculated in the historical market data to obtain the indicator data of the market data to be calculated.
[0112] The market data to be calculated may be market data from which the indicator data is calculated. The responsive state engine includes multiple state factors, which may use the market data as input parameters, calculate the indicator data based on the market data, and return the indicator data to the strategy backtesting plug-in.
[0113] In the embodiment of the present application, the market data is processed by calling the responsive state engine to obtain the index data. Even in the high-frequency trading scenario, the data can be quickly calculated by the responsive state engine to improve the efficiency of data processing.
[0114] The process of the strategy backtest plug-in calling the simulated matching plug-in according to the backtest data, performing order transactions based on the simulated matching plug-in, and obtaining the order transaction results includes:
[0115] If the backtest data includes order information, the simulated matching plug-in is called, and the simulated matching plug-in performs order transactions based on the order information, backtest type, and backtest frequency information to obtain the order transaction results of the order information.
[0116] If the backtest data contains order information, the strategy backtest plug-in can send the order information to the simulated matching plug-in, which will perform order transactions. Order information may include: order ID, type, transaction status, quantity and price of the order, etc.
[0117] It is worth mentioning that the strategy backtesting plug-in can also send the market data, backtesting type and backtesting frequency information related to the order information to the simulation matching plug-in, which will match the order according to the order information, market data and the type and mode of backtesting, and return the transaction results to the strategy backtesting plug-in.
[0118] As another possible implementation method, the strategy backtesting plug-in can parse the order placement and cancellation information obtained by the callback function, and maintain the order status inside the strategy backtesting plug-in based on the order placement and cancellation information, that is, update the current order information, and call the simulation matching plug-in to perform order transactions based on the current order information.
[0119] Optionally, the callback function includes: a market callback function, an order callback function and a trading day callback function.
[0120] The indicator data of the backtest data include: order information, trading day information and market data.
[0121] The above process triggers the execution of the callback function through the indicator data of the backtest data, the current order information and the backtest data, generates the order information or the order cancellation information by the callback function, and updates the current order information based on the order information or the order cancellation information, such as Figure 4 As shown, including:
[0122] S401. When the order information or the current order information triggers the order callback function, the order callback function generates order information or order cancellation information based on the order information or the current order information.
[0123] The order callback function is triggered when order-related events occur, including the onOrder function and the onTrade function. The onOrder function is triggered when the order status changes, and the onTrade function is triggered when the order is rejected or completed.
[0124] For example, when the order status of order A changes from unfinished to completed, the onOrder function is triggered. At this time, the onOrder function can generate order information and update the order status of order A in the current order information maintained in the strategy backtesting plug-in based on the order information. The onTrade function is triggered when order B is rejected. The onTrade function can generate cancellation information and update the current order information maintained in the strategy backtesting plug-in based on the cancellation information, such as deleting order B.
[0125] S402: When the trading day information triggers the trading day callback function, the trading day callback function generates order information or order cancellation information based on the backtest data.
[0126] The callback function of the trading day is triggered by the trading time, including: beforeTrade function and afterTrade function. BeforeTrade is triggered before the market opens on each trading day, and afterTrade is triggered after the market opens on each trading day.
[0127] The historical market data includes time information of the trading day, indicating the opening time of the trading day. In one possible implementation, the market data of the trading day includes a specific identifier. After the historical market data is input into the strategy backtesting plug-in, the strategy backtesting plug-in can distinguish the market data of the trading day based on the specific identifier contained in the market data and trigger the callback function of the trading day.
[0128] As an example, the trading day callback function can be triggered before or after the trading day to generate order information or order cancellation information based on the currently input market data and the current trading strategy.
[0129] S403: When the market data triggers the market callback function, the market callback function generates order information or order cancellation information based on the market data.
[0130] The market callback function is triggered when the market data is received. Different backtest types can correspond to different market callback functions, including: onTick function and onSnapshot function. Among them, onTick is triggered when a new tick-by-tick market is received, and onSnapshot is triggered when a new snapshot market is received.
[0131] It should be noted that there is no order in which the above-mentioned order callback functions, trading day callback functions and market callback functions are triggered. As long as there is a corresponding data input strategy backtesting plug-in, the corresponding callback function can be triggered.
[0132] Optionally, the callback function further includes: an initialization callback function, which is triggered when the backtest data starts to be input, and initializes the custom variables. The custom variables are user-defined variables.
[0133] In the present application, the strategy backtesting plug-in can also call an interface to obtain data or return data to the user. The strategy backtesting plug-in includes: at least one data acquisition interface;
[0134] The above process of obtaining backtest data includes:
[0135] The backtesting data is obtained through the data acquisition interface. The backtesting data includes: position information, account equity indicators, transaction order information, available funds information and market price information.
[0136] Among them, each data acquisition interface can be used to acquire a type of data or perform an operation respectively.
[0137] For example, getPosition interface and getDailyPosition interface obtain the current and the position information at the close of each trading day, getTotalPortfolios interface and getDailyTotalPortfolios interface obtain the account equity index at the close of each trading day, getTradeDetails interface obtains the detailed information of the transaction order, getAvailableCash interface obtains the current available funds, getLastPrice interface obtains the latest market price, etc. The submitOrder interface and cancelOrder interface are used to place and cancel orders.
[0138] It should be noted that the strategy backtesting engine is implemented in the database in the form of a plug-in. Before creating a strategy backtesting plug-in based on the strategy backtesting configuration information and trading strategy code, the strategy backtesting engine already contains multiple data acquisition interfaces. Users can call the interface provided by the strategy backtesting engine by writing trading strategy codes to obtain data or perform the above-mentioned order placement and withdrawal operations. Therefore, the difficulty of users writing trading strategy codes is greatly reduced and the efficiency of code writing is improved.
[0139] After the backtest data input is completed, you can use the getReturnSummary interface to obtain a summary of the trading strategy returns during the entire backtest process. After all backtest work is completed, you can call the dropBacktestEngine interface to delete the strategy backtest plugin of the current trading strategy and release resources.
[0140] Next, combine Figure 5 , the overall process of the strategy backtesting method of this application is described. Figure 5 After the user writes the trading strategy code and strategy backtesting configuration information, a strategy backtesting plug-in for the current trading strategy is created. At this time, the database can determine whether the strategy backtesting plug-in has subscribed to the market indicator based on the strategy backtesting configuration information. If so, a responsive state engine for the current trading strategy is created and backtesting data is obtained.
[0141] When inputting backtest data into the strategy backtest plug-in, you can use the data playback method, or insert the backtest data into the market data table, and the strategy backtest plug-in reads the backtest data. After the backtest data enters and exits the strategy backtest plug-in, the callback function inside the strategy backtest plug-in can be triggered to generate order placement or order cancellation information. Or trigger the responsive state engine to perform real-time calculations to obtain indicator data, and the indicator data can also trigger the callback function to generate order placement or order cancellation information.
[0142] When the backtest data includes order information, the strategy backtest plug-in can also call the simulated matching plug-in to perform order transactions. After the transaction is completed, the simulated matching plug-in can return the transaction results to the strategy backtest plug-in, and the strategy backtest plug-in will update the current order information based on the transaction results. The strategy backtest plug-in can also update the current order information based on order placement or cancellation information.
[0143] When all backtesting data input is completed, the strategy backtesting plug-in can call the preset interface to record the order transaction details, obtain an overview of the profits during the entire backtesting process, and obtain the strategy backtesting results.
[0144] Based on the same inventive concept, a strategy backtesting device corresponding to the strategy backtesting method is also provided in the embodiment of the present application. Since the principle of solving the problem by the device in the embodiment of the present application is similar to the above-mentioned strategy backtesting method in the embodiment of the present application, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.
[0145] Figure 6 A schematic diagram of the structure of a strategy backtesting device provided in an embodiment of the present application is shown, and the device includes an acquisition module 601, a creation module 602, a backtesting module 603 and a determination module 604.
[0146] The acquisition module 601 is used to obtain the strategy backtest configuration information and the trading strategy code of the current trading strategy. The strategy backtest configuration information includes: backtest type and backtest frequency information. The trading strategy code is written based on the DolphinDB scripting language.
[0147] A creation module 602 is used to create a strategy backtesting plug-in for the current trading strategy based on the strategy backtesting configuration information and the trading strategy code;
[0148] The backtest module 603 is used to obtain multiple backtest data and input the backtest data into the strategy backtest plug-in in sequence. The strategy backtest plug-in calls the responsive state engine and the simulation matching plug-in in sequence according to each backtest data, and triggers the callback function of the execution strategy backtest plug-in to update the current order information. The backtest data includes: historical market data;
[0149] The determination module 604 is used to determine the strategy backtesting result of the current trading strategy according to the updated current order information when the input of multiple backtesting data is completed.
[0150] Optionally, the backtest module 603 is specifically used for:
[0151] The strategy backtesting plug-in calls the responsive state engine according to the backtesting data, processes the backtesting data based on the responsive state engine, and obtains the indicator data of the backtesting data;
[0152] The strategy backtesting plug-in calls the simulated matching plug-in according to the backtesting data, performs order transactions based on the simulated matching plug-in, obtains the order transaction results, and updates the current order information according to the order transaction results;
[0153] The callback function is triggered by the indicator data of the backtest data, the current order information and the backtest data. The callback function generates order information or order cancellation information, and updates the current order information based on the order information or order cancellation information.
[0154] Optionally, the backtest module 603 is specifically used for:
[0155] If the backtest data includes market data to be calculated, the responsive state engine is called, and the market data to be calculated is processed according to the responsive state engine to obtain the indicator data of the market data to be calculated.
[0156] Optionally, the backtest module 603 is specifically used for:
[0157] If the backtest data includes order information, the simulated matching plug-in is called, and the simulated matching plug-in performs order transactions based on the order information, backtest type, and backtest frequency information to obtain the order transaction results of the order information.
[0158] Optionally, the callback function includes: a market callback function, an order callback function and a trading day callback function;
[0159] The indicator data of the backtest data include: order information, trading day information and market data;
[0160] The backtest module 603 is specifically used for:
[0161] When the order information or the current order information triggers the order callback function, the order callback function generates order information or order cancellation information based on the order information or the current order information;
[0162] When the trading day information triggers the trading day callback function, the trading day callback function generates order information or order cancellation information based on the backtest data;
[0163] When the market data triggers the market callback function, the market callback function generates order information or order cancellation information based on the market data.
[0164] Optionally, the callback function also includes: an initialization callback function, which is triggered when the backtest data starts to be input and initializes the custom variables.
[0165] Optionally, the strategy backtesting plug-in includes: at least one data acquisition interface;
[0166] The backtest module 603 is specifically used for:
[0167] The backtesting data is obtained through the data acquisition interface. The backtesting data includes: position information, account equity indicators, transaction order information, available funds information and market price information.
[0168] In the embodiment of the present application, the strategy backtesting engine is implemented in the database in the form of a plug-in and runs in the same process as the database, which reduces the complexity of use and reduces the overhead of data transmission. Since the DolphinDB scripting language is more intuitive and easy to understand, it reduces the difficulty and development cost of users writing trading strategy codes. At the same time, the scripting language in DolphinDB can achieve instant compilation, which also improves the operating efficiency of the scripting language. By calling the responsive state engine and the simulated matching plug-in through the strategy backtesting plug-in, calculations can be quickly triggered and calculation results can be obtained in the scenario of high-frequency trading, which improves the processing efficiency of the strategy backtesting plug-in.
[0169] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application is shown, including: a processor 701, a storage medium 702 and a bus 703, wherein the storage medium 702 stores machine-readable instructions executable by the processor 701, and when the electronic device runs a strategy backtesting method such as in the embodiment, the processor 701 communicates with the storage medium 702 via the bus 703, and the processor 701 executes the machine-readable instructions, and the processor 701 executes the preamble of the method item to execute the steps in the above-mentioned strategy backtesting method.
[0170] An embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. The computer program is executed when a processor is running, and the processor executes the steps in the above-mentioned strategy backtesting method.
[0171] In the embodiment of the present application, the computer program can also execute other machine-readable instructions when run by the processor to execute other methods described in the embodiment. For the specific execution method steps and principles, please refer to the description of the embodiment, which will not be repeated here.
[0172] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of the devices or units can be electrical, mechanical or other forms.
[0173] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0174] In addition, each functional unit in the embodiments provided in the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0175] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0176] It should be noted that similar numbers and letters represent similar items in the following figures. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures. In addition, the terms "first", "second", "third", etc. are only used to distinguish the description and are not to be understood as indicating or implying relative importance.
[0177] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present application, which are used to illustrate the technical solutions of the present application, rather than to limit them. The protection scope of the present application is not limited thereto. Although the present application is described in detail with reference to the above-mentioned embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-mentioned embodiments within the technical scope disclosed in the present application, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application. They should all be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.
Claims
1. A strategy backtesting method, characterized in that: Applied to DolphinDB database, the DolphinDB database includes: strategy backtesting plug-in, responsive state engine, simulation matching plug-in, the method includes: Obtain the strategy backtest configuration information and the trading strategy code of the current trading strategy, wherein the strategy backtest configuration information includes: backtest type and backtest frequency information, and the trading strategy code is written based on the DolphinDB scripting language; Create a strategy backtesting plug-in for the current trading strategy based on the strategy backtesting configuration information and the trading strategy code; Acquire multiple backtest data, and input the backtest data into the strategy backtest plug-in in sequence, and the strategy backtest plug-in calls the responsive state engine and the simulated matching plug-in in sequence according to each backtest data, and triggers the callback function of the strategy backtest plug-in to update the current order information, wherein the backtest data includes: historical market data; When the input of the plurality of backtest data is completed, the strategy backtest result of the current trading strategy is determined according to the updated current order information.
2. The method according to claim 1, characterized in that The strategy backtesting plug-in calls the responsive state engine and the simulated matching plug-in in turn according to each of the backtesting data, and triggers the callback function of the strategy backtesting plug-in to update the current order information, including: The strategy backtest plug-in calls the responsive state engine according to the backtest data, processes the backtest data based on the responsive state engine, and obtains indicator data of the backtest data; The strategy backtest plug-in calls the simulated matching plug-in according to the backtest data, performs order trading based on the simulated matching plug-in, obtains order trading results, and updates the current order information according to the order trading results; The callback function is executed by triggering the indicator data of the backtest data, the current order information and the backtest data, the callback function generates order information or order cancellation information, and the current order information is updated based on the order information or order cancellation information.
3. The method according to claim 2, characterized in that The strategy backtest plug-in calls the responsive state engine according to the backtest data, processes the backtest data based on the responsive state engine, and obtains indicator data of the backtest data, including: If the backtest data includes market data to be calculated, the responsive state engine is called, and the market data to be calculated is processed according to the responsive state engine to obtain indicator data of the market data to be calculated.
4. The method according to claim 2, characterized in that: The strategy backtest plug-in calls the simulated matching plug-in according to the backtest data, performs order trading based on the simulated matching plug-in, and obtains the order trading result, including: If the backtest data includes order information, the simulated matching plug-in is called, and the simulated matching plug-in performs order transactions based on the order information, the backtest type, and the backtest frequency information to obtain an order transaction result of the order information.
5. The method according to claim 2, characterized in that: The callback functions include: market callback function, order callback function and trading day callback function; The indicator data of the backtest data includes: order information, trading day information and market data; The triggering and executing of the callback function by the indicator data of the backtest data, the current order information and the backtest data, generating order information or order cancellation information by the callback function, and updating the current order information based on the order information or order cancellation information, includes: When the order information or the current order information triggers the order callback function, the order callback function generates order information or order cancellation information based on the order information or the current order information; When the trading day information triggers the trading day callback function, the trading day callback function generates the order information or order cancellation information based on the backtest data; When the market data triggers the market callback function, the market callback function generates the order information or order cancellation information based on the market data.
6. The method according to claim 5, characterized in that The callback function also includes: an initialization callback function, which is triggered when the backtest data starts to be input and initializes the custom variables.
7. The method according to claim 1, characterized in that The strategy backtesting plug-in includes: at least one data acquisition interface; The obtaining of backtest data includes: The backtest data is obtained through the data acquisition interface, and the backtest data includes: position information, account equity indicators, transaction order information, available funds information and market price information.
8. A strategy backtesting device, characterized in that: include: An acquisition module is used to acquire the strategy backtest configuration information and the trading strategy code of the current trading strategy. The strategy backtest configuration information includes: backtest type and backtest frequency information. The trading strategy code is written based on the DolphinDB scripting language. A creation module, used to create a strategy backtesting plug-in for the current trading strategy based on the strategy backtesting configuration information and the trading strategy code; A backtesting module, used to obtain multiple backtesting data, and input the backtesting data into the strategy backtesting plug-in in sequence, and the strategy backtesting plug-in calls the responsive state engine and the simulation matching plug-in in sequence according to each backtesting data, and triggers the callback function of the strategy backtesting plug-in to update the current order information, wherein the backtesting data includes: historical market data; A determination module is used to determine the strategy backtesting result of the current trading strategy according to the updated current order information when the input of the multiple backtesting data is completed.
9. An electronic device, characterized in that: include: A processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor. When the electronic device is running, the processor and the storage medium communicate via the bus, and the processor executes the machine-readable instructions to perform the steps of the strategy backtesting method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the strategy backtesting method according to any one of claims 1 to 7 are executed.
Citation Information
Patent Citations
Quantitative transaction data processing system and method based on cloud platform
CN110428331A
Code generation method and device for quantifying transaction strategies
CN110688107A
Transaction strategy back testing method and device and storage medium
CN115039119A
Quantitative transaction strategy simulation backtest framework and system based on event driving
CN118982429A
Trading platform system and operating method thereof
KR1020170140007A