pybroker.eval module
Contains implementation of evaluation metrics.
- class BootConfIntervals(low_2p5: float, high_2p5: float, low_5: float, high_5: float, low_10: float, high_10: float)[source]
Bases:
NamedTuple
Holds confidence intervals of bootstrap tests.
- class BootstrapResult(conf_intervals: DataFrame, drawdown_conf: DataFrame, profit_factor: BootConfIntervals, sharpe: BootConfIntervals, drawdown: DrawdownMetrics)[source]
Bases:
NamedTuple
Contains results of bootstrap tests.
- conf_intervals
pandas.DataFrame
containing confidence intervals forlog_profit_factor()
andsharpe_ratio()
.
- drawdown_conf
pandas.DataFrame
containing upper bounds of confidence intervals for maximum drawdown.
- profit_factor
Contains profit factor confidence intervals.
- sharpe
Contains Sharpe Ratio confidence intervals.
- drawdown
Contains drawdown confidence intervals.
- class ConfInterval(name: str, conf: str, lower: float, upper: float)[source]
Bases:
NamedTuple
Confidence interval upper and low bounds.
- class DrawdownConfs(q_001: float, q_01: float, q_05: float, q_10: float)[source]
Bases:
NamedTuple
Contains upper bounds of confidence intervals for maximum drawdown.
- class DrawdownMetrics(confs: DrawdownConfs, pct_confs: DrawdownConfs)[source]
Bases:
NamedTuple
Contains drawdown metrics.
- confs
Upper bounds of confidence intervals for maximum drawdown, measured in cash.
- pct_confs
Upper bounds of confidence intervals for maximum drawdown, measured in percentage.
- class EvalMetrics(trade_count: int = 0, initial_market_value: float = 0, end_market_value: float = 0, total_pnl: float = 0, unrealized_pnl: float = 0, total_return_pct: float = 0, annual_return_pct: float | None = None, total_profit: float = 0, total_loss: float = 0, total_fees: float = 0, max_drawdown: float = 0, max_drawdown_pct: float = 0, win_rate: float = 0, loss_rate: float = 0, winning_trades: int = 0, losing_trades: int = 0, avg_pnl: float = 0, avg_return_pct: float = 0, avg_trade_bars: float = 0, avg_profit: float = 0, avg_profit_pct: float = 0, avg_winning_trade_bars: float = 0, avg_loss: float = 0, avg_loss_pct: float = 0, avg_losing_trade_bars: float = 0, largest_win: float = 0, largest_win_pct: float = 0, largest_win_bars: int = 0, largest_loss: float = 0, largest_loss_pct: float = 0, largest_loss_bars: int = 0, max_wins: int = 0, max_losses: int = 0, sharpe: float = 0, sortino: float = 0, calmar: float | None = None, profit_factor: float = 0, ulcer_index: float = 0, upi: float = 0, equity_r2: float = 0, std_error: float = 0, annual_std_error: float | None = None, annual_volatility_pct: float | None = None)[source]
Bases:
object
Contains metrics for evaluating a
pybroker.strategy.Strategy
.- initial_market_value
Initial market value of the
pybroker.portfolio.Portfolio
.- Type:
- end_market_value
Ending market value of the
pybroker.portfolio.Portfolio
.- Type:
- total_fees
Total brokerage fees. See
pybroker.config.StrategyConfig.fee_mode
for more info.- Type:
- sharpe
Sharpe Ratio, computed per bar.
- Type:
- sortino
Sortino Ratio, computed per bar.
- Type:
- ulcer_index
Ulcer Index, computed per bar.
- Type:
- upi
Ulcer Performance Index, computed per bar.
- Type:
- class EvalResult(metrics: EvalMetrics, bootstrap: BootstrapResult | None)[source]
Bases:
NamedTuple
Contains evaluation result.
- metrics
Evaluation metrics.
- bootstrap
Randomized bootstrap metrics.
- Type:
- class EvaluateMixin[source]
Bases:
object
Mixin for computing evaluation metrics.
- evaluate(portfolio_df: DataFrame, trades_df: DataFrame, calc_bootstrap: bool, bootstrap_sample_size: int, bootstrap_samples: int, bars_per_year: int | None) EvalResult [source]
Computes evaluation metrics.
- Parameters:
portfolio_df –
pandas.DataFrame
of portfolio market values per bar.trades_df –
pandas.DataFrame
of trades.calc_bootstrap –
True
to calculate randomized bootstrap metrics.bootstrap_sample_size – Size of each random bootstrap sample.
bootstrap_samples – Number of random bootstrap samples to use.
bars_per_year – Number of observations per years that will be used to annualize evaluation metrics. For example, a value of
252
would be used to annualize the Sharpe Ratio for daily returns.
- Returns:
EvalResult
containing evaluation metrics.
- annual_total_return_percent(initial_value: float, pnl: float, bars_per_year: int, total_bars: int) float [source]
Computes annualized total return as percentage.
- Parameters:
initial_value – Initial value.
pnl – Total profit and loss (PnL).
bars_per_year – Number of bars per annum.
total_bars – Total number of bars of the return.
- avg_profit_loss(pnls: ndarray[Any, dtype[float64]]) tuple[float, float] [source]
Computes the average profit and average loss per trade.
- Parameters:
pnls – Array of profits and losses (PnLs) per trade.
- Returns:
tuple[float, float]
of average profit and average loss.
- bca_boot_conf(x: ndarray[Any, dtype[float64]], n: int, n_boot: int, fn: Callable[[ndarray[Any, dtype[float64]]], float]) BootConfIntervals [source]
Computes confidence intervals for a user-defined parameter using the bias corrected and accelerated (BCa) bootstrap method.
- Parameters:
x –
numpy.ndarray
containing the data for the randomized bootstrap sampling.n – Number of elements in each random bootstrap sample.
n_boot – Number of random bootstrap samples to use.
fn –
Callable
for computing the parameter used for the confidence intervals.
- Returns:
BootConfIntervals
containing the computed confidence intervals.
- calmar_ratio(changes: ndarray[Any, dtype[float64]], bars_per_year: int) float [source]
Computes the Calmar Ratio.
- Parameters:
changes – Array of differences between each bar and the previous bar.
bars_per_year – Number of bars per annum.
- conf_profit_factor(x: ndarray[Any, dtype[float64]], n: int, n_boot: int) BootConfIntervals [source]
Computes confidence intervals for
profit_factor()
.
- conf_sharpe_ratio(x: ndarray[Any, dtype[float64]], n: int, n_boot: int, obs: int | None = None) BootConfIntervals [source]
Computes confidence intervals for
sharpe_ratio()
.
- iqr(values: ndarray[Any, dtype[float64]]) float [source]
Computes the interquartile range (IQR) of
values
.
- largest_win_loss(pnls: ndarray[Any, dtype[float64]]) tuple[float, float] [source]
Computes the largest profit and largest loss of all trades.
- Parameters:
pnls – Array of profits and losses (PnLs) per trade.
- Returns:
tuple[float, float]
of largest profit and largest loss.
- log_profit_factor(changes: ndarray[Any, dtype[float64]]) floating [source]
Computes the log transformed profit factor, which is the ratio of gross profit to gross loss.
- Parameters:
changes – Array of differences between each bar and the previous bar.
- max_drawdown_percent(returns: ndarray[Any, dtype[float64]]) float [source]
Computes maximum drawdown, measured in percentage loss.
- Parameters:
returns – Array of returns centered at 0.
- max_wins_losses(pnls: ndarray[Any, dtype[float64]]) tuple[int, int] [source]
Computes the max consecutive wins and max consecutive losses.
- Parameters:
pnls – Array of profits and losses (PnLs) per trade.
- Returns:
tuple[int, int]
of max consecutive wins and max consecutive losses.
- relative_entropy(values: ndarray[Any, dtype[float64]]) float [source]
Computes the relative entropy.
- sharpe_ratio(changes: ndarray[Any, dtype[float64]], obs: int | None = None, downside_only: bool = False) floating [source]
Computes the Sharpe Ratio.
- Parameters:
changes – Array of differences between each bar and the previous bar.
obs – Number of observations used to annualize the Sharpe Ratio. For example, a value of
252
would be used to annualize daily returns.
- sortino_ratio(changes: ndarray[Any, dtype[float64]], obs: int | None = None) float [source]
Computes the Sortino Ratio.
- Parameters:
changes – Array of differences between each bar and the previous bar.
obs – Number of observations used to annualize the Sortino Ratio. For example, a value of
252
would be used to annualize daily returns.
- total_profit_loss(pnls: ndarray[Any, dtype[float64]]) tuple[float, float] [source]
Computes total profit and loss.
- Parameters:
pnls – Array of profits and losses (PnLs) per trade.
- Returns:
tuple[float, float]
of total profit and total loss.
- total_return_percent(initial_value: float, pnl: float) float [source]
Computes total return as percentage.
- Parameters:
initial_value – Initial value.
pnl – Total profit and loss (PnL).