-
Notifications
You must be signed in to change notification settings - Fork 0
Analysis
The analysis subpackage groups components relating to trading performance statistics and analysis.
分析子包将与交易性能统计和分析相关的组件分组。
class PortfolioAnalyzer
Provides a portfolio performance analyzer for tracking and generating performance metrics and statistics.
提供用于跟踪和生成性能指标和统计数据的投资组合性能分析器。
def register_statistic(self, statistic: PortfolioStatistic) -> None:
"""
Register the given statistic with the analyzer.
Parameters
----------
statistic : PortfolioStatistic
The statistic to register.
# 要注册的统计数据。
"""
...
def deregister_statistic(self, statistic: PortfolioStatistic) -> None:
"""
Deregister a statistic from the analyzer.
# 从分析器中取消注册统计数据。
"""
...
def deregister_statistics(self) -> None:
"""
Deregister all statistics from the analyzer.
# 从分析器中取消注册所有统计数据。
"""
...
def reset(self) -> None:
"""
Reset the analyzer.
All stateful fields are reset to their initial value.
# 所有有状态字段都重置为其初始值。
"""
...
@property
def currencies(self) -> list[Currency]:
"""
Return the analyzed currencies.
Returns
-------
list[Currency]
"""
...
def statistic(self, name: str) -> PortfolioStatistic | None:
"""
Return the statistic with the given name (if found).
Returns
-------
PortfolioStatistic or None
"""
...
def returns(self) -> Series:
"""
Return raw the returns data.
Returns
-------
pd.Series
"""
...
def calculate_statistics(self, account: Account, positions: list[Position]) -> None:
"""
Calculate performance metrics from the given data.
Parameters
----------
account : Account
The account for the calculations.
# 用于计算的账户。
positions : list[Position]
The positions for the calculations.
# 用于计算的头寸。
"""
...
def add_positions(self, positions: list[Position]) -> None:
"""
Add positions data to the analyzer.
Parameters
----------
positions : list[Position]
The positions for analysis.
# 用于分析的头寸。
"""
...
def add_trade(self, position_id: PositionId, realized_pnl: Money) -> None:
"""
Add trade data to the analyzer.
Parameters
----------
position_id : PositionId
The position ID for the trade.
# 交易的头寸 ID。
realized_pnl : Money
The realized PnL for the trade.
# 交易的已实现盈亏。
"""
...
def add_return(self, timestamp: datetime, value: float) -> None:
"""
Add return data to the analyzer.
Parameters
----------
timestamp : datetime
The timestamp for the returns entry.
# 回报条目的时间戳。
value : double
The return value to add.
# 要添加的回报值。
"""
...
def realized_pnls(self, currency: Currency | None = None) -> Series | None:
"""
Return the realized PnL for the portfolio.
For multi-currency portfolios, specify the currency for the result.
Parameters
----------
currency : Currency, optional
The currency for the result.
# 结果的货币。
Returns
-------
pd.Series or None
Raises
------
ValueError
If currency is None when analyzing multi-currency portfolios.
"""
...
def total_pnl(self, currency: Currency | None = None, unrealized_pnl: Money | None = None) -> float:
"""
Return the total PnL for the portfolio.
For multi-currency portfolios, specify the currency for the result.
Parameters
----------
currency : Currency, optional
The currency for the result.
# 结果的货币。
unrealized_pnl : Money, optional
The unrealized PnL for the given currency.
# 给定货币的未实现盈亏。
Returns
-------
float
Raises
------
ValueError
If currency is None when analyzing multi-currency portfolios.
ValueError
If currency is not contained in the tracked account balances.
ValueError
If unrealized_pnl is not None and currency is not equal to the given currency.
"""
...
def total_pnl_percentage(self, currency: Currency | None = None, unrealized_pnl: Money | None = None) -> float:
"""
Return the percentage change of the total PnL for the portfolio.
For multi-currency accounts, specify the currency for the result.
Parameters
----------
currency : Currency, optional
The currency for the result.
# 结果的货币。
unrealized_pnl : Money, optional
The unrealized PnL for the given currency.
# 给定货币的未实现盈亏。
Returns
-------
float
Raises
------
ValueError
If currency is None when analyzing multi-currency portfolios.
ValueError
If currency is not contained in the tracked account balances.
ValueError
If unrealized_pnl is not None and currency is not equal to the given currency.
"""
...
def get_performance_stats_pnls(self, currency: Currency | None = None, unrealized_pnl: Money | None = None) -> dict[str, float]:
"""
Return the ‘PnL’ (profit and loss) performance statistics, optionally includes the unrealized PnL.
Money objects are converted to floats.
Parameters
----------
currency : Currency
The currency for the performance.
# 性能的货币。
unrealized_pnl : Money, optional
The unrealized PnL for the performance.
# 性能的未实现盈亏。
Returns
-------
dict[str, Any]
"""
...
def get_performance_stats_returns(self) -> dict[str, Any]:
"""
Return the return performance statistics values.
Returns
-------
dict[str, Any]
"""
...
def get_performance_stats_general(self) -> dict[str, Any]:
"""
Return the general performance statistics.
Returns
-------
dict[str, Any]
"""
...
def get_stats_pnls_formatted(self, currency: Currency | None = None, unrealized_pnl: Money | None = None) -> list[str]:
"""
Return the performance statistics from the last backtest run formatted for
printing in the backtest run footer.
Parameters
----------
currency : Currency
The currency for the performance.
# 性能的货币。
unrealized_pnl : Money, optional
The unrealized PnL for the performance.
# 性能的未实现盈亏。
Returns
-------
list[str]
"""
...
def get_stats_returns_formatted(self) -> list[str]:
"""
Return the performance statistics for returns from the last backtest run
formatted for printing in the backtest run footer.
Returns
-------
list[str]
"""
...
def get_stats_general_formatted(self) -> list[str]:
"""
Return the performance statistics for returns from the last backtest run
formatted for printing in the backtest run footer.
Returns
-------
list[str]
"""
...
class ReportProvider
Provides various portfolio analysis reports.
提供各种投资组合分析报告。
@staticmethod
def generate_orders_report(orders: list[Order]) -> DataFrame:
"""
Generate an orders report.
Parameters
----------
orders : list[Order]
The orders for the report.
# 报告的订单。
Returns
-------
pd.DataFrame
"""
...
@staticmethod
def generate_order_fills_report(orders: list[Order]) -> DataFrame:
"""
Generate an order fills report.
This report provides a row per order.
Parameters
----------
orders : list[Order]
The orders for the report.
# 报告的订单。
Returns
-------
pd.DataFrame
"""
...
@staticmethod
def generate_fills_report(orders: list[Order]) -> DataFrame:
"""
Generate a fills report.
This report provides a row per individual fill event.
Parameters
----------
orders : list[Order]
The orders for the report.
# 报告的订单。
Returns
-------
pd.DataFrame
"""
...
@staticmethod
def generate_positions_report(positions: list[Position]) -> DataFrame:
"""
Generate a positions report.
Parameters
----------
positions : list[Position]
The positions for the report.
# 报告的头寸。
Returns
-------
pd.DataFrame
"""
...
@staticmethod
def generate_account_report(account: Account) -> DataFrame:
"""
Generate an account report for the given optional time range.
Parameters
----------
account : Account
The account for the report.
# 报告的账户。
Returns
-------
pd.DataFrame
"""
...
class PortfolioStatistic
The base class for all portfolio performance statistics.
所有投资组合性能统计的基类。
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
class Expectancy(PortfolioStatistic)
Calculates the expectancy from a realized PnLs series.
从已实现的盈亏系列计算预期。
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
class LongRatio(PortfolioStatistic)
Calculates the ratio of long (to short) positions.
计算多头(与空头)头寸的比率。
Parameters:
precision
(int
, default2
): The decimal precision for the output.precision
(int
,默认值2
):输出的小数精度。
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
class AvgLoser(PortfolioStatistic)
Calculates the average loser from a series of PnLs.
从一系列盈亏中计算平均亏损。
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
class MaxLoser(PortfolioStatistic)
Calculates the maximum loser from a series of PnLs.
从一系列盈亏中计算最大亏损。
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
class MinLoser(PortfolioStatistic)
Calculates the minimum loser from a series of PnLs.
从一系列盈亏中计算最小亏损。
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
class ProfitFactor(PortfolioStatistic)
Calculates the annualized profit factor or ratio (wins/loss).
计算年度利润因子或比率(盈利/亏损)。
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
class ReturnsAverage(PortfolioStatistic)
Calculates the average return.
计算平均回报。
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
class ReturnsAverageLoss(PortfolioStatistic)
Calculates the average losing return.
计算平均亏损回报。
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
class ReturnsAverageWin(PortfolioStatistic)
Calculates the average winning return.
计算平均盈利回报。
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
class ReturnsVolatility(PortfolioStatistic)
Calculates the volatility of returns.
计算回报的波动率。
The returns will be downsampled into daily bins.
回报将被下采样到每日仓位。
Parameters:
period
(int
, default252
): The trading period in days.period
(int
,默认值252
):交易周期(以天为单位)。
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
class RiskReturnRatio(PortfolioStatistic)
Calculates the return on risk ratio.
计算风险回报率。
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
class SharpeRatio(PortfolioStatistic)
Calculates the Sharpe Ratio from returns.
从回报中计算夏普比率。
The returns will be downsampled into daily bins.
回报将被下采样到每日仓位。
Parameters:
period
(int
, default252
): The trading period in days.period
(int
,默认值252
):交易周期(以天为单位)。
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
class SortinoRatio(PortfolioStatistic)
Calculates the annualized Sortino Ratio from returns.
从回报中计算年度索提诺比率。
The returns will be downsampled into daily bins.
回报将被下采样到每日仓位。
Parameters:
period
(int
, default252
): The trading period in days.period
(int
,默认值252
):交易周期(以天为单位)。
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
class WinRate(PortfolioStatistic)
Calculates the win rate from a realized PnLs series.
从已实现的盈亏系列计算胜率。
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
class AvgWinner(PortfolioStatistic)
Calculates the average winner from a series of PnLs.
从一系列盈亏中计算平均盈利。
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
class MaxWinner(PortfolioStatistic)
Calculates the maximum winner from a series of PnLs.
从一系列盈亏中计算最大盈利。
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...
class MinWinner(PortfolioStatistic)
Calculates the minimum winner from a series of PnLs.
从一系列盈亏中计算最小盈利。
def calculate_from_realized_pnls(self, realized_pnls: Series) -> Any | None:
"""
Calculate the statistic value from the given raw realized PnLs.
Parameters
----------
realized_pnls : pd.Series
The raw PnLs for the calculation.
# 用于计算的原始盈亏。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_orders(self, orders: list[Order]) -> Any | None:
"""
Calculate the statistic value from the given orders.
Parameters
----------
orders : list[Order]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_positions(self, positions: list[Position]) -> Any | None:
"""
Calculate the statistic value from the given positions.
Parameters
----------
positions : list[Position]
The positions to use for the calculation.
# 用于计算的头寸。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
def calculate_from_returns(self, returns: Series) -> Any | None:
"""
Calculate the statistic value from the given raw returns.
Parameters
----------
returns : pd.Series
The returns to use for the calculation.
# 用于计算的回报。
Returns
-------
Any or None
A JSON serializable primitive.
# JSON 可序列化原语。
"""
...
@classmethod
def fully_qualified_name() -> str:
"""
Return the fully qualified name for the PortfolioStatistic class.
Returns
-------
str
"""
...
@property
def name(self) -> str:
"""
Return the name for the statistic.
Returns
-------
str
"""
...