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)[源代码]
基类:
NamedTuple
Holds confidence intervals of bootstrap tests.
- class BootstrapResult(conf_intervals: DataFrame, drawdown_conf: DataFrame, profit_factor: BootConfIntervals, sharpe: BootConfIntervals, drawdown: DrawdownMetrics)[源代码]
基类:
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)[源代码]
基类:
NamedTuple
Confidence interval upper and low bounds.
- class DrawdownConfs(q_001: float, q_01: float, q_05: float, q_10: float)[源代码]
基类:
NamedTuple
Contains upper bounds of confidence intervals for maximum drawdown.
- class DrawdownMetrics(confs: DrawdownConfs, pct_confs: DrawdownConfs)[源代码]
基类:
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, max_drawdown_date: datetime | None = None, 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)[源代码]
基类:
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:
- max_drawdown_date
Date of maximum drawdown.
- Type:
datetime.datetime | None
- 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:
- annual_std_error
Annualized standard error, computed per bar on market values of portfolio.
- Type:
float | None
- class EvalResult(metrics: EvalMetrics, bootstrap: BootstrapResult | None)[源代码]
基类:
NamedTuple
Contains evaluation result.
- metrics
Evaluation metrics.
- bootstrap
Randomized bootstrap metrics.
- Type:
- class EvaluateMixin[源代码]
基类:
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 [源代码]
Computes evaluation metrics.
- 参数:
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.
- 返回:
EvalResult
containing evaluation metrics.
- annual_total_return_percent(initial_value: float, pnl: float, bars_per_year: int, total_bars: int) float [源代码]
Computes annualized total return as percentage.
- 参数:
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[tuple[int, ...], dtype[float64]]) tuple[float, float] [源代码]
Computes the average profit and average loss per trade.
- 参数:
pnls -- Array of profits and losses (PnLs) per trade.
- 返回:
tuple[float, float]
of average profit and average loss.
- bca_boot_conf(x: ndarray[tuple[int, ...], dtype[float64]], n: int, n_boot: int, fn: Callable[[ndarray[tuple[int, ...], dtype[float64]]], float]) BootConfIntervals [源代码]
Computes confidence intervals for a user-defined parameter using the bias corrected and accelerated (BCa) bootstrap method.
- 参数:
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.
- 返回:
BootConfIntervals
containing the computed confidence intervals.
- calmar_ratio(changes: ndarray[tuple[int, ...], dtype[float64]], bars_per_year: int) float [源代码]
Computes the Calmar Ratio.
- 参数:
changes -- Array of differences between each bar and the previous bar.
bars_per_year -- Number of bars per annum.
- conf_profit_factor(x: ndarray[tuple[int, ...], dtype[float64]], n: int, n_boot: int) BootConfIntervals [源代码]
Computes confidence intervals for
profit_factor()
.
- conf_sharpe_ratio(x: ndarray[tuple[int, ...], dtype[float64]], n: int, n_boot: int, obs: int | None = None) BootConfIntervals [源代码]
Computes confidence intervals for
sharpe_ratio()
.
- iqr(values: ndarray[tuple[int, ...], dtype[float64]]) float [源代码]
Computes the interquartile range (IQR) of
values
.
- largest_win_loss(pnls: ndarray[tuple[int, ...], dtype[float64]]) tuple[float, float] [源代码]
Computes the largest profit and largest loss of all trades.
- 参数:
pnls -- Array of profits and losses (PnLs) per trade.
- 返回:
tuple[float, float]
of largest profit and largest loss.
- log_profit_factor(changes: ndarray[tuple[int, ...], dtype[float64]]) floating [源代码]
Computes the log transformed profit factor, which is the ratio of gross profit to gross loss.
- 参数:
changes -- Array of differences between each bar and the previous bar.
- max_drawdown_percent(returns: ndarray[tuple[int, ...], dtype[float64]]) tuple[float, int | None] [源代码]
Computes maximum drawdown, measured in percentage loss.
- 参数:
returns -- Array of returns centered at 0.
- 返回:
Maximum drawdown, measured in percentage loss.
Index of the maximum drawdown.
- max_wins_losses(pnls: ndarray[tuple[int, ...], dtype[float64]]) tuple[int, int] [源代码]
Computes the max consecutive wins and max consecutive losses.
- 参数:
pnls -- Array of profits and losses (PnLs) per trade.
- 返回:
tuple[int, int]
of max consecutive wins and max consecutive losses.
- r_squared(values: ndarray[tuple[int, ...], dtype[float64]]) float [源代码]
Computes R-squared of
values
.
- relative_entropy(values: ndarray[tuple[int, ...], dtype[float64]]) float [源代码]
Computes the relative entropy.
- sharpe_ratio(changes: ndarray[tuple[int, ...], dtype[float64]], obs: int | None = None, downside_only: bool = False) floating [源代码]
Computes the Sharpe Ratio.
- 参数:
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[tuple[int, ...], dtype[float64]], obs: int | None = None) float [源代码]
Computes the Sortino Ratio.
- 参数:
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[tuple[int, ...], dtype[float64]]) tuple[float, float] [源代码]
Computes total profit and loss.
- 参数:
pnls -- Array of profits and losses (PnLs) per trade.
- 返回:
tuple[float, float]
of total profit and total loss.
- total_return_percent(initial_value: float, pnl: float) float [源代码]
Computes total return as percentage.
- 参数:
initial_value -- Initial value.
pnl -- Total profit and loss (PnL).