Skip to content
HN On Hacker News ↗

Building a High-Performance C++ Backtesting Framework with an Order Matching Simulator Plugin<!-- -->

▲ 14 points 2 comments by CrazyTomato 2mo ago HN discussion ↗

Pangram verdict · v3.3

We believe that this document is fully human-written

4 %

AI likelihood · overall

Human
100% human-written 0% AI-generated
SEGMENTS · HUMAN 4 of 4
SEGMENTS · AI 0 of 4
WORD COUNT 1,290
PEAK AI % 6% · §1
Analyzed
Jul 14
backend: pangram/v3.3
Segments scanned
4 windows
avg 323 words each
Distribution
100 / 0%
human / AI fraction
Verdict
Human
Pangram v3.3

Article text · 1,290 words · 4 segments analyzed

Human AI-generated
§1 Human · 6%

For institutions that have already built a C++ backtesting framework, rebuilding the entire system to support more realistic order matching and high-performance market replay is often impractical. DolphinDB’s high-performance market data replay and Order Matching Simulator Plugin provides a low-latency, high-throughput solution for strategy validation. By integrating the plugin directly into an existing C++ backtesting framework, institutions can reuse their current infrastructure while taking advantage of DolphinDB’s ultra-fast computing capabilities, making it an ideal solution for high-frequency strategy simulation.This tutorial is intended for quantitative engineers who are familiar with C++ development and have a basic understanding of DolphinDB. It focuses on how to seamlessly integrate the DolphinDB order matching simulator plugin into a standalone C++ trading system by using either the Swordfish library or the C++ API. Compared with backtesting in the DolphinDB scripting language, this approach delivers a low-latency, highly concurrent backtesting workflow and is particularly well suited to time-sensitive simulation scenarios such as algorithmic trading and market-making strategies.1. BackgroundBacktesting is a critical part of quantitative trading research and development. Before a quantitative strategy is applied to live trading, its performance on historical data must be evaluated through backtesting. In medium- and high-frequency strategy backtesting, you cannot simply assume that every order is filled in full at the current price or the end-of-day price. Instead, you need an order matching simulator to model the actual trading process, including whether an order can be filled, the execution price, trading volume, and market impact. DolphinDB provides an order matching simulator plugin that supports Level-2 tick-by-tick and snapshot market data from the Shanghai and Shenzhen stock exchanges. It delivers high-precision order matching consistent with exchange rules under the “price priority, time priority” principle, supports matching modes based on multiple types of market data, and offers extensive order matching configuration options to simulate real-world trading conditions. It also supports medium- and high-frequency quantitative trading strategy development and testing in the DolphinDB scripting language, Python, and C++, providing a high-performance and highly extensible backtesting solution.Swordfish is a high-performance analytical computing library designed specifically for the financial industry, with excellent in-memory processing capabilities and optimized computational performance. In addition to a wide range of general-purpose computing functions, it supports real-time streaming data processing and user-defined functions to meet the needs of complex analytics and low-latency stream processing.

§2 Human · 4%

Swordfish runs on any platform that supports C++, and users can invoke its APIs directly in C++ code for efficient computation.The DolphinDB C++API can connect to the DolphinDB server and a C++ client, enabling bidirectional data transfer and remote script execution. It lets you conveniently use DolphinDB in C++ programs for data processing, analysis, modeling, and more, helping you accelerate these workloads with DolphinDB’s excellent computing performance and powerful storage capabilities.This tutorial explains how to encapsulate the order matching simulator plugin in C++ to build a simple, extensible backtesting framework with high-precision order matching, which can be easily integrated into an existing C++ backtesting or simulation system. This tutorial defines a unified event-driven interface and provides two implementations: Swordfish and C++API. The applicable scenarios for the two approaches are as follows:Table 1–1 Scenario comparison between the Swordfish and C++API backtesting framework implementations2. System DesignImplementing a medium- and high-frequency quantitative trading strategy backtesting platform mainly involvesthe following three key parts:Market data replayOrder matching simulationStrategy development and backtest performance evaluationBased on this design, you can define the overall workflow for backtesting in C++ with DolphinDB as the data source:Create and subscribe to a remote market data stream table.Replay market data into the stream table.Traverse and parse the data in the subscription callback, then write the parsed market data to the order matching simulator.Trigger the market data callback, implement strategy logic in the callback, and place orders or perform other actions.The order matching simulator outputs execution details and other information, triggers the relevant business callbacks, and lets you implement strategy logic in those callbacks.Output performance metrics after the backtest ends.The following diagram shows the system architecture for implementing user-defined backtesting with Swordfish:Figure 1–1 Architecture for user-defined backtesting with SwordfishThe following diagram shows the system architecture for implementing user-defined backtesting with C++ API:Figure 1–2 Architecture for user-defined backtesting with C++ API3. Interface DesignBased on the capabilities provided by DolphinDB’s order matching simulator interfaces, this tutorial defines market data and trading interfaces. The design mainly includes the following functional modules:Configuration module:Handles settings for backtesting framework instances, including remote connection settings, market data type settings, and concurrency-related settings.

§3 Human · 4%

Active call interface module: Provides interfaces that you can call directly in code, including interfaces to create order matching simulator instances, replay data, place orders, and cancel orders.Callback interface module: Provides callback-based interfaces, including market data callbacks, order acknowledgments, and execution notifications.Note:Unless otherwise specified, the configuration items and interfaces introduced in this section can be used in both Swordfish and the C++ API.3.1 Configuration ItemsThe configuration items for a backtesting framework instance are shown in the following table. Each instance can be configured independently, which allows users to run concurrent backtests with multiple instances.3.2 InterfacesThe backtesting framework presented in this tutorial provides direct-call interfaces for creating order matching simulator instances, replaying data, submitting orders, and canceling orders, as well as callback interfaces for market data, order submission responses, and trade notifications. The following sections describe the design and usage of the core interfaces. For more detailed information about other interfaces, see the comments in the attached source code.3.2.1 Create an Order Matching SimulatorFirst, use the createMatchEngine interface to create an order matching simulator instance. It provides two implementations: Copied to clipboarddolphindb::SmartPointer<MatchingEngineSimulatorWrapper> createMatchEngine(         dolphindb::ConstantSP name, dolphindb::ConstantSP exchange, dolphindb::DictionarySP config,         dolphindb::TableSP dummyQuoteTable, dolphindb::DictionarySP quoteColMap, dolphindb::TableSP dummyUserOrderTable,         dolphindb::DictionarySP userOrderColMap, dolphindb::TableSP dummyOrderDetailsOutput,         dolphindb::ConstantSP orderDetailsOutputStreamTableName=nullptr)

dolphindb::SmartPointer<MatchingEngineSimulatorWrapper> createMatchEngine( std::vector<dolphindb::ConstantSP> args)The first uses an expanded parameter list, and the second packages all parameters from the first list into a single array. The parameter requirements for this interface are largely the same as those of the original createMatchEngine interface.

§4 Human · 4%

Only the parameters that differ are described here:dummyOrderDetailsOutput: A pointer to a Table object that defines the actual schema of the trade details output table used as the reference schema for Swordfish/DolphinDB when creating the output stream table.orderDetailsOutputStreamTableName: A pointer to a String object that specifies the stream table name of the trade details output table. The default is “orderDetailsOutputStream”. A parameter specific to the C++ API.The return value is a pointer to a MatchingEngineSimulatorWrapper object. This engine instance supports feeding market data, submitting orders, retrieving trade details, and performing related operations.Note:In the C++ API implementation, the order matching simulator instance resides in DolphinDB. Each call to createMatchEngine creates and binds a new remote DolphinDB connection. Therefore, frequent calls to this interface should be avoided; instead, the returned instance should be retained and reused.3.2.2 Replay Market DataAfter the order matching simulator is created, the replayQuoteToMatchEngine interface can be used to replay market data for a specified stock symbol and number of days to a specified engine. The replay speed can also be configured.void Interface::replayQuoteToMatchEngine(VectorSP codes, ConstantSP startDate, ConstantSP endDate, SmartPointer<MatchingEngineSimulatorWrapper> engine, int replayRate)Note:This tutorial provides an example of replaying Level-2 stock snapshot data. The code can be used as a reference for implementing replay for other types of market data.3.2.3 Market Data CallbacksAfter market data starts flowing into the engine, it triggers the onQuote callback. The callback parameter is a dictionary representing one market data record. You can read fields by calling getMember(“key”), for example, to read the stock symbol:virtual void onQuote(const ConstantSP "e){ string symbol = quote->getMember("symbol")->getString(); }3.2.4 Implement a StrategyWhen implementing a strategy, the getMatchEngine interface can be used to obtain the order matching simulator with the specified name, which can then be used to submit or cancel orders.SmartPointer<MatchingEngineSimulatorWrapper> Interface::getMatchEngine(ConstantSP name)The order submission interface is submitOrder, and the order cancellation interface is cancelOrder. Both take the engine instance and order data as parameters.