nautilus-serialization
Data serialization and format conversion for NautilusTrader.
The nautilus-serialization crate provides comprehensive data serialization capabilities for converting
trading data between different formats including Apache Arrow, Parquet, and Cap'n Proto.
This enables efficient data storage, retrieval, and interoperability across different systems:
- Apache Arrow integration: Schema definitions and encoding/decoding for market data types.
- Parquet file operations: High-performance columnar storage for historical data analysis.
- Record batch processing: Efficient batch operations for time-series data.
- Schema management: Type-safe schema definitions with metadata preservation.
- Cross-format conversion: Seamless data interchange between Arrow, Parquet, and native types.
- Cap'n Proto serialization: Zero-copy, schema-based serialization for efficient data interchange.
Platform
NautilusTrader is an open-source, high-performance, production-grade algorithmic trading platform, providing quantitative traders with the ability to backtest portfolios of automated trading strategies on historical data with an event-driven engine, and also deploy those same strategies live, with no code changes.
NautilusTrader's design, architecture, and implementation philosophy prioritizes software correctness and safety at the highest level, with the aim of supporting mission-critical, trading system backtesting and live deployment workloads.
Feature flags
This crate provides feature flags to control source code inclusion during compilation, depending on the intended use case, i.e. whether to provide Python bindings for the nautilus_trader Python package, or as part of a Rust only build.
python: Enables Python bindings from PyO3.extension-module: Builds as a Python extension module (used withpython).high-precision: Enables high-precision mode to use 128-bit value types.capnp: Enables Cap'n Proto serialization support.
Building with Cap'n Proto support
To build with Cap'n Proto serialization enabled:
The Cap'n Proto compiler is required. See the Environment Setup guide for installation instructions. The required version is specified in the capnp-version file in the repository root.
Cap'n Proto schemas
When the capnp feature is enabled, this crate provides zero-copy serialization using Cap'n Proto schemas.
Schema location
Cap'n Proto schemas are bundled with the crate in schemas/capnp/:
common/identifiers.capnp- Identifier types (TraderId, InstrumentId, etc.)common/types.capnp- Value types (Price, Quantity, Money, etc.)common/enums.capnp- Trading enumerationscommands/trading.capnp- Trading commandscommands/data.capnp- Data subscription/request commandsevents/order.capnp- Order eventsevents/position.capnp- Position eventsevents/account.capnp- Account eventsdata/market.capnp- Market data types (quotes, trades, bars, order books)
Generated modules
During build, schemas are compiled to Rust code and made available as:
nautilus_serialization::identifiers_capnpnautilus_serialization::types_capnpnautilus_serialization::enums_capnpnautilus_serialization::trading_capnpnautilus_serialization::data_capnpnautilus_serialization::order_capnpnautilus_serialization::position_capnpnautilus_serialization::account_capnpnautilus_serialization::market_capnp
Usage example
use Price;
use ;
// Serialize a Price
let price = from;
let bytes = serialize_price.unwrap;
// Deserialize back
let decoded = deserialize_price.unwrap;
assert_eq!;
See the conversions module for trait-based serialization patterns:
use InstrumentId;
use ;
let instrument_id = from;
// Using traits
let mut message = new_default;
let builder = message.;
instrument_id.to_capnp;
// Serialize to bytes
let mut bytes = Vecnew;
write_message.unwrap;
// Deserialize
let reader = read_message.unwrap;
let root = reader..unwrap;
let decoded = from_capnp.unwrap;
Contributing schemas
When adding or modifying schemas:
- Edit schema files in the appropriate subdirectory under
schemas/capnp/. - Use lowerCamelCase for field names to match Cap'n Proto conventions.
- Generate a unique schema ID using:
capnp id. - Implement
ToCapnpandFromCapnptraits insrc/capnp/conversions.rs. - Add integration tests in
tests/to verify roundtrip serialization.
The build script (build.rs) automatically discovers and compiles all .capnp files during build.
Serialization format comparison
This crate supports three serialization formats for market data types. Choose the format based on your use case:
| Format | Serialize | Deserialize | Size | Use case |
|---|---|---|---|---|
| Cap'n Proto | ~267ns | ~530ns | 264 bytes | High-frequency data streams, IPC, caching. |
| JSON | ~332ns | ~779ns | 174 bytes | Human-readable output, debugging, APIs. |
| MsgPack | ~375ns | ~634ns | 134 bytes | Compact storage, network transmission. |
| Arrow | TBD | TBD | Columnar | Batch processing, Parquet, IPC, analytics. |
Performance numbers shown for QuoteTick serialization (measured on AMD Ryzen 9 7950X). Cap'n Proto provides the
fastest serialization and deserialization, while MsgPack offers the smallest size. Arrow is optimized for batch
processing rather than individual messages.
Note: Cap'n Proto performance can be further optimized through zero-copy techniques and direct buffer manipulation for specialized use cases.
Usage examples
JSON serialization
use Serializable;
use QuoteTick;
let quote = QuoteTick ;
// Serialize to JSON
let json_bytes = quote.to_json_bytes?;
// Deserialize from JSON
let decoded = from_json_bytes?;
MsgPack serialization
use ;
use QuoteTick;
let quote = QuoteTick ;
// Serialize to MsgPack
let msgpack_bytes = quote.to_msgpack_bytes?;
// Deserialize from MsgPack
let decoded = from_msgpack_bytes?;
Cap'n Proto serialization
use QuoteTick;
use ;
let quote = QuoteTick ;
// Serialize to Cap'n Proto
let mut message = new_default;
let builder = message.;
quote.to_capnp;
let mut bytes = Vecnew;
write_message?;
// Deserialize from Cap'n Proto
let reader = read_message?;
let root = reader.?;
let decoded = from_capnp?;
Benchmarking
Run benchmarks to compare serialization performance across formats:
# Compare all formats for QuoteTick
# Compare all formats for TradeTick
# Compare all formats for Bar
# Run all Cap'n Proto benchmarks (including OrderBookDeltas with varying sizes)
# Run all comparison benchmarks
Benchmark results include serialization and deserialization times for each format.
Documentation
See the docs for more detailed usage.
License
The source code for NautilusTrader is available on GitHub under the GNU Lesser General Public License v3.0. Contributions to the project are welcome and require the completion of a standard Contributor License Agreement (CLA).
NautilusTraderâ„¢ is developed and maintained by Nautech Systems, a technology company specializing in the development of high-performance trading systems. For more information, visit https://nautilustrader.io.
© 2015-2026 Nautech Systems Pty Ltd. All rights reserved.