nautilus_analysis/lib.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Portfolio analysis and performance metrics for [NautilusTrader](https://nautilustrader.io).
17//!
18//! The `nautilus-analysis` crate provides portfolio analysis tools and performance
19//! statistics for evaluating trading strategies and portfolios. This includes return-based metrics,
20//! PnL-based statistics, and risk measurements commonly used in quantitative finance:
21//!
22//! - Portfolio analyzer for tracking account states and positions.
23//! - Extensive collection of performance statistics and risk metrics.
24//! - Flexible statistic calculation framework supporting different data sources.
25//! - Support for multi-currency portfolios and unrealized PnL calculations.
26//!
27//! # NautilusTrader
28//!
29//! [NautilusTrader](https://nautilustrader.io) is an open-source, production-grade, Rust-native
30//! engine for multi-asset, multi-venue trading systems.
31//!
32//! The system spans research, deterministic simulation, and live execution within a single
33//! event-driven architecture, providing research-to-live semantic parity.
34//!
35//! # Feature Flags
36//!
37//! This crate provides feature flags to control source code inclusion during compilation,
38//! depending on the intended use case, i.e. whether to provide Python bindings
39//! for the [nautilus_trader](https://pypi.org/project/nautilus_trader) Python package,
40//! or as part of a Rust only build.
41//!
42//! - `extension-module`: Builds as a Python extension module.
43//! - `python`: Enables Python bindings from [PyO3](https://pyo3.rs).
44
45#![warn(rustc::all)]
46#![warn(clippy::pedantic)]
47#![allow(
48 clippy::manual_let_else,
49 clippy::option_if_let_else,
50 clippy::or_fun_call,
51 clippy::redundant_else,
52 clippy::similar_names,
53 clippy::single_match_else,
54 reason = "workspace policy; generated by scripts/update-clippy-allowances.py"
55)]
56#![deny(unsafe_code)]
57#![deny(unsafe_op_in_unsafe_fn)]
58#![deny(nonstandard_style)]
59#![deny(missing_debug_implementations)]
60#![deny(clippy::missing_errors_doc)]
61#![deny(clippy::missing_panics_doc)]
62#![deny(rustdoc::broken_intra_doc_links)]
63#![allow(
64 clippy::cast_possible_truncation,
65 clippy::cast_possible_wrap,
66 clippy::cast_precision_loss,
67 clippy::cast_sign_loss,
68 reason = "analysis math casts between usize/i32/i64/f64 with values bounded by sample counts"
69)]
70#![allow(
71 clippy::assert_is_empty,
72 reason = "`assert!(x.is_empty())` is clearer than comparing against an empty value"
73)]
74#![cfg_attr(
75 test,
76 allow(
77 clippy::float_cmp,
78 clippy::unreadable_literal,
79 reason = "analysis tests assert exact float outputs and reference statistic constants"
80 )
81)]
82
83pub mod analyzer;
84pub mod snapshot;
85pub mod statistic;
86pub mod statistics;
87
88pub use snapshot::PortfolioStatistics;
89
90#[cfg(feature = "python")]
91pub mod python;
92
93use std::collections::BTreeMap;
94
95use nautilus_core::UnixNanos;
96
97/// Type alias for time-indexed returns data used in portfolio analysis.
98///
99/// Maps timestamps to return values for time-series analysis of portfolio performance.
100pub type Returns = BTreeMap<UnixNanos, f64>;