backtrader.analyzers.sharpe_ratio_stats module

Sharpe Ratio Statistics Module - Advanced Sharpe ratio calculations.

This module provides functions for calculating Sharpe ratio statistics including estimated, probabilistic, and defecto Sharpe ratios, along with their confidence intervals and significance tests.

Functions:

estimated_sharpe_ratio: Calculate basic Sharpe ratio. ann_estimated_sharpe_ratio: Calculate annualized Sharpe ratio. estimated_sharpe_ratio_stdev: Standard deviation of Sharpe estimation. probabilistic_sharpe_ratio: PSR calculation. min_track_record_length: Minimum track record for significance. sharpe_ratio_defacto: Defacto Sharpe ratio calculation.

backtrader.analyzers.sharpe_ratio_stats.estimated_sharpe_ratio(returns)[source]

Calculate the estimated sharpe ratio (risk_free=0).

Parameters:

returns (np.array, pd.Series, pd.DataFrame)

Return type:

float, pd.Series

backtrader.analyzers.sharpe_ratio_stats.ann_estimated_sharpe_ratio(returns=None, periods=261, *, sr=None)[source]

Calculate the annualized estimated sharpe ratio (risk_free=0).

Parameters:
  • returns (np.array, pd.Series, pd.DataFrame)

  • periods (int) – How many items in returns complete a Year. If returns are daily: 261, weekly: 52, monthly: 12, …

  • sr (float, np.array, pd.Series, pd.DataFrame) – Sharpe ratio to be annualized, its frequency must be coherent with periods

Return type:

float, pd.Series

backtrader.analyzers.sharpe_ratio_stats.estimated_sharpe_ratio_stdev(returns=None, *, n=None, skew=None, kurtosis=None, sr=None)[source]

Calculate the standard deviation of the sharpe ratio estimation.

Parameters:
  • returns (np.array, pd.Series, pd.DataFrame) – If no returns are passed it is mandatory to pass the other four parameters.

  • n (int) – Number of returns samples used for calculating skew, kurtosis and sr.

  • skew (float, np.array, pd.Series, pd.DataFrame) – The third moment expressed in the same frequency as the other parameters. `Skew`=0 for normal returns.

  • kurtosis (float, np.array, pd.Series, pd.DataFrame) – The fourth moment expressed in the same frequency as the other parameters. `Kurtosis`=3 for normal returns.

  • sr (float, np.array, pd.Series, pd.DataFrame) – Sharpe ratio expressed in the same frequency as the other parameters.

Return type:

float, pd.Series

Notes

This formula generalizes for both normal and non-normal returns. https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1821643

backtrader.analyzers.sharpe_ratio_stats.probabilistic_sharpe_ratio(returns=None, sr_benchmark=0.0, *, sr=None, sr_std=None)[source]

Calculate the Probabilistic Sharpe Ratio (PSR).

Parameters:
  • returns (np.array, pd.Series, pd.DataFrame) – If no returns are passed it is mandatory to pass a sr and sr_std.

  • sr_benchmark (float) – Benchmark sharpe ratio expressed in the same frequency as the other parameters. By default, set to zero (comparing against no investment skill).

  • sr (float, np.array, pd.Series, pd.DataFrame) – Sharpe ratio expressed in the same frequency as the other parameters.

  • sr_std (float, np.array, pd.Series, pd.DataFrame) – Standard deviation fo the Estimated sharpe ratio, expressed in the same frequency as the other parameters.

Return type:

float, pd.Series

Notes

PSR(SR*) = probability that SR^ > SR* SR^ = sharpe ratio estimated with returns, or sr SR* = sr_benchmark

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1821643

backtrader.analyzers.sharpe_ratio_stats.min_track_record_length(returns=None, sr_benchmark=0.0, prob=0.95, *, n=None, sr=None, sr_std=None)[source]

Calculate the MIn Track Record Length (minTRL).

Parameters:
  • returns (np.array, pd.Series, pd.DataFrame) – If no returns are passed it is mandatory to pass a sr and sr_std.

  • sr_benchmark (float) – Benchmark sharpe ratio expressed in the same frequency as the other parameters. By default, set to zero (comparing against no investment skill).

  • prob (float) – Confidence level used for calculating the minTRL. Between 0 and 1, by default=0.95

  • n (int) – Number of returns samples used for calculating sr and sr_std.

  • sr (float, np.array, pd.Series, pd.DataFrame) – Sharpe ratio expressed in the same frequency as the other parameters.

  • sr_std (float, np.array, pd.Series, pd.DataFrame) – Standard deviation fo the Estimated sharpe ratio, expressed in the same frequency as the other parameters.

Return type:

float, pd.Series

Notes

minTRL = minimum of returns/samples needed (with same SR and SR_STD) to accomplish a PSR(SR*) > prob PSR(SR*) = probability that SR^ > SR* SR^ = sharpe ratio estimated with returns, or sr SR* = sr_benchmark

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1821643

backtrader.analyzers.sharpe_ratio_stats.num_independent_trials(trials_returns=None, *, m=None, p=None)[source]

Calculate the number of independent trials.

Parameters:
  • trials_returns (pd.DataFrame) – All trials returns, not only the independent trials.

  • m (int) – Number of total trials.

  • p (float) – Average correlation between all the trials.

Return type:

int

backtrader.analyzers.sharpe_ratio_stats.expected_maximum_sr(trials_returns=None, expected_mean_sr=0.0, *, independent_trials=None, trials_sr_std=None)[source]

Compute the expected maximum Sharpe ratio (Analytically)

Parameters:
  • trials_returns (pd.DataFrame) – All trials returns, not only the independent trials.

  • expected_mean_sr (float) – Expected mean SR, usually 0. We assume that random startegies will have a mean SR of 0, expressed in the same frequency as the other parameters.

  • independent_trials (int) – Number of independent trials must be between 1 and trials_returns.shape[1]

  • trials_sr_std (float) – Standard deviation for the Estimated sharpe ratios of all trials, expressed in the same frequency as the other parameters.

Return type:

float

backtrader.analyzers.sharpe_ratio_stats.deflated_sharpe_ratio(trials_returns=None, returns_selected=None, expected_mean_sr=0.0, independent_trials=10, expected_max_sr=None)[source]

Calculate the Deflated Sharpe Ratio (PSR).

Parameters:
  • trials_returns (pd.DataFrame) – All trials returns, not only the independent trials.

  • returns_selected (pd.Series)

  • expected_mean_sr (float) – Expected mean SR, usually 0. We assume that random startegies will have a mean SR of 0, expressed in the same frequency as the other parameters.

  • expected_max_sr (float) – The expected maximum sharpe ratio expected after running all the trials, expressed in the same frequency as the other parameters.

  • independent_trials (int)

Return type:

float

Notes

DFS = PSR(SR⁰) = probability that SR^ > SR⁰ SR^ = sharpe ratio estimated with returns, or sr SR⁰ = max_expected_sr

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2460551