Backtesting - Loren1166/NautilusTrader- GitHub Wiki

Backtesting 回测

info 信息

We are currently working on this article.

我们目前正在撰写本文。

Backtesting with NautilusTrader is a methodical simulation process that replicates trading activities using a specific system implementation. This system is composed of various components including Actors, Strategies, Execution Algorithms, and other user-defined modules. The entire trading simulation is predicated on a stream of historical data processed by a BacktestEngine. Once this data stream is exhausted, the engine concludes its operation, producing detailed results and performance metrics for in-depth analysis.

使用 NautilusTrader 进行回测是一个有条不紊的模拟过程,它使用特定的系统实现来复制交易活动。该系统由各种组件组成,包括参与者、策略、执行算法和其他用户定义的模块。整个交易模拟都基于由 BacktestEngine 处理的历史数据流。一旦该数据流耗尽,引擎将结束其操作,生成详细的结果和性能指标以进行深入分析。

It's paramount to recognize that NautilusTrader offers two distinct API levels for setting up and conducting backtests: high-level and low-level.

至关重要的是要认识到 NautilusTrader 提供了两个不同的 API 级别来设置和执行回测:高级和低级。

Choosing an API level 选择 API 级别

Consider the low-level API when:

以下情况下考虑使用低级 API:

  • The entirety of your data stream can be comfortably accommodated within available memory. 您的整个数据流可以舒适地容纳在可用内存中。
  • You choose to avoid storing data in the Nautilus-specific Parquet format. 您选择避免以 Nautilus 特定的 Parquet 格式存储数据。
  • Or, you have a specific need/preference for retaining raw data in its innate format, such as CSV, Binary, etc. 或者,您有特定的需求/偏好,希望以其固有格式保留原始数据,例如 CSV、二进制文件等。
  • You seek granular control over the BacktestEngine, enabling functionalities such as re-running backtests on identical data while interchanging components (like actors or strategies) or tweaking parameter settings. 您寻求对 BacktestEngine 的精细控制,从而实现诸如在交换组件(如参与者或策略)或调整参数设置的同时在相同数据上重新运行回测等功能。

Consider the high-level API when:

以下情况下考虑使用高级 API:

  • Your data stream's size exceeds available memory, necessitating streaming data in batches. 您的数据流大小超过可用内存,需要分批流式传输数据。
  • You want to harness the performance capabilities and convenience of the ParquetDataCatalog and persist your data in the Nautilus-specific Parquet format. 您希望利用 ParquetDataCatalog 的性能和便利性,并以 Nautilus 特定的 Parquet 格式持久化数据。
  • You value the flexibility and advanced functionalities offered by passing configuration objects, which can define diverse backtest runs across many engines at once. 您重视通过传递配置对象提供的灵活性和高级功能,这些对象可以一次定义跨多个引擎的不同回测运行。

Low-level API 低级 API

The low-level API revolves around a single BacktestEngine, with inputs initialized and added 'manually' via a Python script. An instantiated BacktestEngine can accept:

低级 API 围绕单个 BacktestEngine 展开,输入通过 Python 脚本“手动”初始化和添加。实例化的 BacktestEngine 可以接受:

  • Lists of Data objects which will be automatically sorted into monotonic order by ts_init. Data 对象列表,这些对象将按 ts_init 自动排序为单调顺序。
  • Multiple venues (manually initialized and added). 多个交易平台(手动初始化和添加)。
  • Multiple actors (manually initialized and added). 多个参与者(手动初始化和添加)。
  • Multiple execution algorithms (manually initialized and added). 多个执行算法(手动初始化和添加)。

High-level API 高级 API

The high-level API revolves around a single BacktestNode, which will orchestrate the management of individual BacktestEngines, each defined by a BacktestRunConfig. Multiple configurations can be bundled into a list and fed to the node to be run.

高级 API 围绕单个 BacktestNode 展开,它将协调各个 BacktestEngines 的管理,每个引擎都由 BacktestRunConfig 定义。多个配置可以捆绑到一个列表中,并馈送到节点以运行。

Each of these BacktestRunConfig objects in turn is made up of:

反过来,这些 BacktestRunConfig 对象中的每一个都由以下组成:

  • A list of BacktestDataConfig objects. BacktestDataConfig 对象列表。
  • A list of BacktestVenueConfig objects. BacktestVenueConfig 对象列表。
  • A list of ImportableActorConfig objects. ImportableActorConfig 对象列表。
  • A list of ImportableStrategyConfig objects. ImportableStrategyConfig 对象列表。
  • A list of ImportableExecAlgorithmConfig objects. ImportableExecAlgorithmConfig 对象列表。
  • An optional ImportableControllerConfig object. 可选的 ImportableControllerConfig 对象。
  • An optional BacktestEngineConfig object (otherwise will be the default). 可选的 BacktestEngineConfig 对象(否则将使用默认值)。