Analysis
The analysis subpackage groups components relating to trading performance statistics and analysis.
class PortfolioAnalyzer
Bases: object
Provides a portfolio performance analyzer for tracking and generating performance metrics and statistics.
register_statistic(statistic: PortfolioStatistic) → None
Register the given statistic with the analyzer.
- Parameters: statistic (PortfolioStatistic) – The statistic to register.
deregister_statistic(statistic: PortfolioStatistic) → None
Deregister a statistic from the analyzer.
deregister_statistics() → None
Deregister all statistics from the analyzer.
reset() → None
Reset the analyzer.
All stateful fields are reset to their initial value.
property currencies : list[Currency]
Return the analyzed currencies.
- Return type: list[Currency]
statistic(name: str) → PortfolioStatistic | None
Return the statistic with the given name (if found).
- Return type:
PortfolioStatistic or
None
returns() → Series
Return raw the returns data.
- Return type: pd.Series
calculate_statistics(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.
add_positions(positions: list[Position]) → None
Add positions data to the analyzer.
- Parameters: positions (list [Position ]) – The positions for analysis.
add_trade(position_id: PositionId, realized_pnl: Money) → None
Add trade data to the analyzer.
- Parameters:
- position_id (PositionId) – The position ID for the trade.
- realized_pnl (Money) – The realized PnL for the trade.
add_return(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.
realized_pnls(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.
- Return type:
pd.Series or
None
- Raises:
ValueError – If currency is
None
when analyzing multi-currency portfolios.
total_pnl(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:
- Return type: 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.
- ValueError – If currency is
total_pnl_percentage(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:
- Return type: 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.
- ValueError – If currency is
get_performance_stats_pnls(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:
- Return type: dict[str, Any]
get_performance_stats_returns() → dict[str, Any]
Return the return performance statistics values.
- Return type: dict[str, Any]
get_performance_stats_general() → dict[str, Any]
Return the general performance statistics.
- Return type: dict[str, Any]
get_stats_pnls_formatted(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:
- Return type: list[str]
get_stats_returns_formatted() → list[str]
Return the performance statistics for returns from the last backtest run formatted for printing in the backtest run footer.
- Return type: list[str]
get_stats_general_formatted() → list[str]
Return the performance statistics for returns from the last backtest run formatted for printing in the backtest run footer.
- Return type: list[str]
class ReportProvider
Bases: object
Provides various portfolio analysis reports.
static generate_orders_report(orders: list[Order]) → DataFrame
Generate an orders report.
- Parameters: orders (list [Order ]) – The orders for the report.
- Return type: pd.DataFrame
static 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.
- Return type: pd.DataFrame
static 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.
- Return type: pd.DataFrame
static generate_positions_report(positions: list[Position]) → DataFrame
Generate a positions report.
- Parameters: positions (list [Position ]) – The positions for the report.
- Return type: pd.DataFrame
static generate_account_report(account: Account) → DataFrame
Generate an account report for the given optional time range.
- Parameters: account (Account) – The account for the report.
- Return type: pd.DataFrame
class PortfolioStatistic
Bases: object
The base class for all portfolio performance statistics.
classmethod fully_qualified_name() → str
Return the fully qualified name for the PortfolioStatistic class.
- Return type: str
property name : str
Return the name for the statistic.
- Return type: str
calculate_from_returns(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_realized_pnls(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_orders(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_positions(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: A JSON serializable primitive.
- Return type:
Any or
None
class Expectancy
Bases: PortfolioStatistic
Calculates the expectancy from a realized PnLs series.
calculate_from_realized_pnls(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_orders(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_positions(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_returns(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: A JSON serializable primitive.
- Return type:
Any or
None
classmethod fully_qualified_name() → str
Return the fully qualified name for the PortfolioStatistic class.
- Return type: str
property name : str
Return the name for the statistic.
- Return type: str
class LongRatio
Bases: PortfolioStatistic
Calculates the ratio of long (to short) positions.
- Parameters: precision (int , default 2) – The decimal precision for the output.
calculate_from_positions(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_orders(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_realized_pnls(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_returns(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: A JSON serializable primitive.
- Return type:
Any or
None
classmethod fully_qualified_name() → str
Return the fully qualified name for the PortfolioStatistic class.
- Return type: str
property name : str
Return the name for the statistic.
- Return type: str
class AvgLoser
Bases: PortfolioStatistic
Calculates the average loser from a series of PnLs.
calculate_from_realized_pnls(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_orders(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_positions(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_returns(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: A JSON serializable primitive.
- Return type:
Any or
None
classmethod fully_qualified_name() → str
Return the fully qualified name for the PortfolioStatistic class.
- Return type: str
property name : str
Return the name for the statistic.
- Return type: str
class MaxLoser
Bases: PortfolioStatistic
Calculates the maximum loser from a series of PnLs.
calculate_from_realized_pnls(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_orders(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_positions(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_returns(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: A JSON serializable primitive.
- Return type:
Any or
None
classmethod fully_qualified_name() → str
Return the fully qualified name for the PortfolioStatistic class.
- Return type: str
property name : str
Return the name for the statistic.
- Return type: str
class MinLoser
Bases: PortfolioStatistic
Calculates the minimum loser from a series of PnLs.
calculate_from_realized_pnls(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_orders(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_positions(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_returns(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: A JSON serializable primitive.
- Return type:
Any or
None
classmethod fully_qualified_name() → str
Return the fully qualified name for the PortfolioStatistic class.
- Return type: str
property name : str
Return the name for the statistic.
- Return type: str
class ProfitFactor
Bases: PortfolioStatistic
Calculates the annualized profit factor or ratio (wins/loss).
calculate_from_returns(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_orders(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: A JSON serializable primitive.
- Return type:
Any or
None
calculate_from_positions(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: A JSON serializable primitive.
- Return type:
Any or
None