Skip to content

Portfolio Statistics

Loren1166 edited this page Sep 23, 2024 · 2 revisions

Portfolio Statistics 投资组合统计

There are a variety of built-in portfolio statistics which are used to analyse a trading portfolios performance for both backtests and live trading.

有各种内置的投资组合统计数据,用于分析回测和实时交易的交易投资组合的表现。

The statistics are generally categorized as follows.

统计数据通常分类如下。

  • PnLs based statistics (per currency) 基于盈亏的统计数据(每种货币)
  • Returns based statistics 基于回报的统计数据
  • Positions based statistics 基于头寸的统计数据
  • Orders based statistics 基于订单的统计数据

It's also possible to call a traders PortfolioAnalyzer and calculate statistics at any arbitrary time, including during a backtest, or live trading session.

也可以调用交易者的 PortfolioAnalyzer 并在任意时间计算统计数据,包括在回测或实时交易期间。

Custom Statistics 自定义统计数据

Custom portfolio statistics can be defined by inheriting from the PortfolioStatistic base class, and implementing any of the calculate_ methods.

可以通过继承 PortfolioStatistic 基类并实现任何 calculate_ 方法来定义自定义投资组合统计数据。

For example, the following is the implementation for the built-in WinRate statistic:

例如,以下是内置 WinRate 统计数据的实现:

from nautilus_trader.analysis.statistic import PortfolioStatistic


class WinRate(PortfolioStatistic):
    """
    Calculates the win rate from a realized PnLs series.
    # 从已实现盈亏系列计算胜率。
    """

    def calculate_from_realized_pnls(self, realized_pnls: pd.Series) -> Any | None:
        # Preconditions
        # 前提条件
        if realized_pnls is None or realized_pnls.empty:
            return 0.0

        # Calculate statistic
        # 计算统计数据
        winners = [x for x in realized_pnls if x > 0.0]
        losers = [x for x in realized_pnls if x <= 0.0]

        return len(winners) / float(max(1, (len(winners) + len(losers))))

These statistics can then be registered with a traders PortfolioAnalyzer.

然后可以将这些统计数据注册到交易者的 PortfolioAnalyzer 中。

stat = WinRate()

engine.portfolio.analyzer.register_statistic(stat)

tip 提示

Ensure your statistic is robust to degenerate inputs such as None, empty series, or insufficient data.

确保您的统计数据对退化输入(例如 None、空系列或数据不足)具有鲁棒性。

The expectation is that you would then return None, NaN or a reasonable default.

预期是您将返回 NoneNaN 或合理的默认值。

Backtest Analysis 回测分析

Following a backtest run a performance analysis will be carried out by passing realized PnLs, returns, positions and orders data to each registered statistic in turn, calculating their values (with a default configuration). Any output is then displayed in the tear sheet under the Portfolio Performance heading, grouped as.

在回测运行后,将通过将已实现的盈亏、回报、头寸和订单数据依次传递给每个注册的统计数据来执行性能分析,并计算它们的值(使用默认配置)。然后,任何输出都将显示在投资组合表现标题下的摘要表中,分组如下。

  • Realized PnL statistics (per currency) 已实现盈亏统计数据(每种货币)
  • Returns statistics (for the entire portfolio) 回报统计数据(针对整个投资组合)
  • General statistics derived from position and order data (for the entire portfolio) 从头寸和订单数据得出的一般统计数据(针对整个投资组合)
文档 (Documentation)
入门 (Getting Started)
概念 (Concepts)
教程 (Tutorials)
集成 (Integrations)
Python API
Rust API[未翻译]
开发者指南 (Developer Guide)
Clone this wiki locally