Skip to content

Architecture

Loren1166 edited this page Sep 23, 2024 · 4 revisions

Architecture

架构

Welcome to the architectural overview of NautilusTrader. 欢迎来到 NautilusTrader 的架构概述。

This guide dives deep into the foundational principles, structures, and designs that underpin the platform. Whether you're a developer, system architect, or just curious about the inner workings of NautilusTrader, this section covers: 本指南深入探讨支撑该平台的基本原则、结构和设计。无论您是开发者、系统架构师,还是对 NautilusTrader 的内部工作原理感到好奇,这一部分都涵盖了:

  • The design philosophy that drives decisions and shapes the system's evolution
  • 驱动决策并塑造系统演变的设计理念
  • The overarching system architecture providing a bird's-eye view of the entire system framework
  • 提供整个系统框架鸟瞰图的总体系统架构
  • How the framework is organized to facilitate modularity and maintainability
  • 框架如何组织以促进模块化和可维护性
  • The code structure that ensures readability and scalability
  • 确保可读性和可扩展性的代码结构
  • A breakdown of component organization and interaction to understand how different parts communicate and collaborate
  • 组件组织和交互的细分,以理解不同部分如何沟通和协作
  • And finally, the implementation techniques that are crucial for performance, reliability, and robustness
  • 最后,实施技术对于性能、可靠性和健壮性至关重要

Note
Throughout the documentation, the term "Nautilus system boundary" refers to operations within the runtime of a single Nautilus node (also known as a "trader instance").

注意
在整个文档中,“Nautilus 系统边界”一词指的是单个 Nautilus 节点(也称为“交易实例”)的运行时内的操作。

Design philosophy

设计理念

The major architectural techniques and design patterns employed by NautilusTrader are: NautilusTrader 采用的主要架构技术和设计模式包括:

  • Domain driven design (DDD)
  • 领域驱动设计(DDD)
  • Event-driven architecture
  • 事件驱动架构
  • Messaging patterns (Pub/Sub, Req/Rep, point-to-point)
  • 消息传递模式(发布/订阅、请求/响应、点对点)
  • Ports and adapters
  • 端口和适配器
  • Crash-only design
  • 仅崩溃设计

These techniques have been utilized to assist in achieving certain architectural quality attributes. 这些技术已被用来帮助实现某些架构质量属性。

Quality attributes

质量属性

Architectural decisions are often a trade-off between competing priorities. The below is a list of some of the most important quality attributes which are considered when making design and architectural decisions, roughly in order of 'weighting'. 架构决策通常是在竞争优先级之间的权衡。以下是进行设计和架构决策时考虑的一些最重要的质量属性的列表,按“权重”大致排序。

  • Reliability
  • 可靠性
  • Performance
  • 性能
  • Modularity
  • 模块化
  • Testability
  • 可测试性
  • Maintainability
  • 可维护性
  • Deployability
  • 可部署性

System architecture

系统架构

The NautilusTrader codebase is actually both a framework for composing trading systems, and a set of default system implementations which can operate in various environment contexts. NautilusTrader 的代码库实际上是一个组合交易系统的框架,以及一组可以在各种环境上下文中运行的默认系统实现。 image

Environment contexts

环境上下文

An environment context in NautilusTrader defines the type of data and trading venue you are working with. Understanding these contexts is crucial for effective backtesting, development, and live trading. NautilusTrader 中的环境上下文定义了您正在使用的数据和交易平台的类型。理解这些上下文对于有效的回测、开发和实时交易至关重要。

Here are the available environments you can work with: 以下是您可以使用的可用环境:

  • Backtest: Historical data with simulated venues
  • 回测:带有模拟平台的历史数据
  • Sandbox: Real-time data with simulated venues
  • 沙盒:带有模拟平台的实时数据
  • Live: Real-time data with live venues (paper trading or real accounts)
  • 实盘:带有真实平台的实时数据(模拟交易或真实账户)

Common core

公共核心

The platform has been designed to share as much common code between backtest, sandbox and live trading systems as possible. This is formalized in the system subpackage, where you will find the NautilusKernel class, providing a common core system 'kernel'. 该平台设计为尽可能共享回测、沙盒和实盘交易系统之间的公共代码。这在系统子包中得到了正式化,您将找到 NautilusKernel 类,提供一个公共核心系统“内核”。

The ports and adapters architectural style enables modular components to be integrated into the core system, providing various hooks for user-defined or custom component implementations. 端口和适配器架构风格使得模块化组件可以集成到核心系统中,为用户定义或自定义组件实现提供了各种钩子。

Messaging

消息传递

To facilitate modularity and loose coupling, an extremely efficient MessageBus passes messages (data, commands and events) between components. 为了促进模块化和松耦合,一个极其高效的消息总线(MessageBus)在组件之间传递消息(数据、命令和事件)。

From a high level architectural view, it's important to understand that the platform has been designed to run efficiently on a single thread, for both backtesting and live trading. Much research and testing resulted in arriving at this design, as it was found the overhead of context switching between threads didn't actually result in improved performance. 从高层架构的角度来看,重要的是要理解该平台被设计为能够高效地在单线程上运行,无论是回测还是实盘交易。大量的研究和测试促成了这一设计,因为发现线程之间的上下文切换开销并没有实际提高性能。

When considering the logic of how your algo trading will work within the system boundary, you can expect each component to consume messages in a deterministic synchronous way (similar to the actor model). 在考虑您的算法交易将在系统边界内如何工作的逻辑时,您可以预期每个组件以确定性同步的方式处理消息(类似于演员模型)。

Note
Of interest is the LMAX exchange architecture, which achieves award winning performance running on a single thread. You can read about their disruptor pattern based architecture in this interesting article by Martin Fowler.

注意
值得注意的是 LMAX 交易所的架构,它在单线程上实现了获奖性能。您可以在 Martin Fowler 的这篇有趣文章中阅读他们的破坏者模式基础架构。

Framework organization

框架组织

The codebase is organized with a layering of abstraction levels, and generally grouped into logical subpackages of cohesive concepts. You can navigate to the documentation for each of these subpackages from the left nav menu. 代码库按照抽象层次进行组织,并通常分组为逻辑子包,形成内聚的概念。您可以从左侧导航菜单访问每个子包的文档。

Core / low-Level

核心 / 低级

  • core: Constants, functions and low-level components used throughout the framework
  • core:在整个框架中使用的常量、函数和低级组件
  • common: Common parts for assembling the frameworks various components
  • common:用于组装框架各种组件的公共部分
  • network: Low-level base components for networking clients
  • network:网络客户端的低级基础组件
  • serialization: Serialization base components and serializer implementations
  • serialization:序列化基础组件和序列化实现
  • model: Defines a rich trading domain model
  • model:定义丰富的交易领域模型

Components

组件

  • accounting: Different account types and account management machinery
  • accounting:不同账户类型和账户管理机制
  • adapters: Integration adapters for the platform including brokers and exchanges
  • adapters:平台的集成适配器,包括经纪商和交易所
  • analysis: Components relating to trading performance statistics and analysis
  • analysis:与交易绩效统计和分析相关的组件
  • cache: Provides common caching infrastructure
  • cache:提供通用缓存基础设施
  • data: The data stack and data tooling for the platform
  • data:平台的数据堆栈和数据工具
  • execution: The execution stack for the platform
  • execution:平台的执行堆栈
  • indicators: A set of efficient indicators and analyzers
  • indicators:一组高效的指标和分析器
  • infrastructure: Technology specific infrastructure implementations
  • infrastructure:技术特定的基础设施实现
  • msgbus: A universal message bus for

connecting system components

  • msgbus:连接系统组件的通用消息总线
  • persistence: Data storage, cataloging and retrieval, mainly to support backtesting
  • persistence:数据存储、目录和检索,主要支持回测
  • portfolio: Portfolio management functionality
  • portfolio:投资组合管理功能
  • risk: Risk specific components and tooling
  • risk:特定于风险的组件和工具
  • trading: Trading domain specific components and tooling
  • trading:特定于交易领域的组件和工具

System implementations

系统实现

  • backtest: Backtesting componentry as well as a backtest engine and node implementations
  • backtest:回测组件以及回测引擎和节点实现
  • live: Live engine and client implementations as well as a node for live trading
  • live:实时引擎和客户端实现,以及用于实时交易的节点
  • system: The core system kernel common between backtest, sandbox, live environment contexts
  • system:回测、沙盒和实时环境上下文之间的核心系统内核

Code structure

代码结构

The foundation of the codebase is the nautilus_core directory, containing a collection of core Rust crates including a C foreign function interface (FFI) generated by cbindgen. 代码库的基础是 nautilus_core 目录,其中包含一组核心 Rust crate,包括由 cbindgen 生成的 C 外部函数接口(FFI)。

The bulk of the production code resides in the nautilus_trader directory, which contains a collection of Python/Cython subpackages and modules. 大部分生产代码位于 nautilus_trader 目录中,该目录包含一组 Python/Cython 子包和模块。

Python bindings for the Rust core are provided by statically linking the Rust libraries to the C extension modules generated by Cython at compile time (effectively extending the CPython API). Rust 核心的 Python 绑定是通过在编译时将 Rust 库静态链接到 Cython 生成的 C 扩展模块来提供的(有效地扩展 CPython API)。

Dependency flow

依赖流

┌─────────────────────────┐
│                         │
│                         │
│     nautilus_trader     │
│                         │
│     Python / Cython     │
│                         │
│                         │
└────────────┬────────────┘
 C API       │
             │
             │
             │
 C API       ▼
┌─────────────────────────┐
│                         │
│                         │
│      nautilus_core      │
│                         │
│          Rust           │
│                         │
│                         │
└─────────────────────────┘

Note
Both Rust and Cython are build dependencies. The binary wheels produced from a build do not require Rust or Cython to be installed at runtime.

注意
Rust 和 Cython 都是构建依赖项。从构建生成的二进制轮文件在运行时不需要安装 Rust 或 Cython。

Type safety

类型安全

The design of the platform holds software correctness and safety at the highest level. 平台的设计将软件的正确性和安全性置于最高水平。

The Rust codebase in nautilus_core is always type safe and memory safe as guaranteed by the rustc compiler, and so is correct by construction (unless explicitly marked unsafe, see the Rust section of the Developer Guide). nautilus_core 中的 Rust 代码库始终是类型安全和内存安全的,由 rustc 编译器保证,因此是通过构造正确的(除非明确标记为不安全,请参阅开发者指南中的 Rust 部分)。

Cython provides type safety at the C level at both compile time, and runtime: Cython 在编译时和运行时提供 C 级别的类型安全:

Info
If you pass an argument with an invalid type to a Cython implemented module with typed parameters, then you will receive a TypeError at runtime.

信息
如果您将无效类型的参数传递给带有类型参数的 Cython 实现模块,则在运行时将收到 TypeError。

If a function or method's parameter is not explicitly typed to accept None, passing None as an argument will result in a ValueError at runtime.

如果函数或方法的参数没有明确定义为接受 None,则将 None 作为参数传递将导致运行时 ValueError。

Warning
The above exceptions are not explicitly documented to prevent excessive bloating of the docstrings.

警告
上述异常未被明确记录,以防止文档字符串的过度膨胀。

Errors and exceptions

错误和异常

Every attempt has been made to accurately document the possible exceptions which can be raised from NautilusTrader code, and the conditions which will trigger them. 已经尽力准确记录可能从 NautilusTrader 代码中引发的异常以及触发它们的条件。

Warning
There may be other undocumented exceptions which can be raised by Python's standard library, or from third party library dependencies.

警告
可能还有其他未记录的异常,这些异常可能由 Python 的标准库或第三方库依赖引发。

Processes and threads

进程和线程

Tip
For optimal performance and to prevent potential issues related to Python's memory model and equality, it is highly recommended to run each trader instance in a separate process.

提示
为了获得最佳性能,并防止与 Python 的内存模型和相等性相关的潜在问题,强烈建议在单独的进程中运行每个交易实例。


文档 (Documentation)
入门 (Getting Started)
概念 (Concepts)
教程 (Tutorials)
集成 (Integrations)
Python API
Rust API[未翻译]
开发者指南 (Developer Guide)
Clone this wiki locally