This module contains functions and technical indicators that are used to evaluate the performance of trading strategies and assets. These indicators help measure returns, volatility, and other key performance metrics.
The Sharpe Ratio is a measure of risk-adjusted return. It compares the excess return of an asset (or portfolio) over the risk-free rate to its standard deviation. A higher Sharpe Ratio indicates better risk-adjusted performance.
df['sharpe_ratio'] = bta.sharpe_ratio(df, risk_free_rate=0.01, window=252)
df
(pandas.DataFrame): Input DataFrame containing asset returns in a column.risk_free_rate
(float): The risk-free rate (e.g., treasury bond rate). Default is0.01
.window
(int): Look-back period for calculating the Sharpe Ratio. Default is252
(typically used for daily data in a year).
- DataFrame: A DataFrame with a single
'sharpe_ratio'
column.
The Sortino Ratio is a variation of the Sharpe Ratio that focuses only on downside volatility. It measures the risk-adjusted return by penalizing negative returns more than positive returns.
df['sortino_ratio'] = bta.sortino_ratio(df, risk_free_rate=0.01, window=252)
df
(pandas.DataFrame): Input DataFrame containing asset returns in a column.risk_free_rate
(float): The risk-free rate (e.g., treasury bond rate). Default is0.01
.window
(int): Look-back period for calculating the Sortino Ratio. Default is252
.
- DataFrame: A DataFrame with a single
'sortino_ratio'
column.
The Maximum Drawdown (MDD) measures the maximum observed loss from a peak to a trough of an asset or portfolio. It is used to assess the risk of large losses.
df['mdd'] = bta.maximum_drawdown(df, column='close')
df
(pandas.DataFrame): Input DataFrame containing price or return data.column
(str): The column to calculate the maximum drawdown on. Default is'close'
.
- DataFrame: A DataFrame with a single
'mdd'
column representing the maximum drawdown.
The Calmar Ratio is a performance indicator that measures the return of an asset or portfolio relative to its maximum drawdown. It is a risk-adjusted return metric that emphasizes downside risk.
df['calmar_ratio'] = bta.calmar_ratio(df, window=252)
df
(pandas.DataFrame): Input DataFrame containing return data.window
(int): Look-back period for calculating the Calmar Ratio. Default is252
.
- DataFrame: A DataFrame with a single
'calmar_ratio'
column.
The Information Ratio (IR) compares the excess return of an asset or portfolio to a benchmark (e.g., an index) relative to the tracking error (standard deviation of excess returns). It is used to assess performance compared to a benchmark.
df['information_ratio'] = bta.information_ratio(df, benchmark_column='benchmark', window=252)
df
(pandas.DataFrame): Input DataFrame containing returns of the asset and the benchmark.benchmark_column
(str): The column containing benchmark returns.window
(int): Look-back period for calculating the Information Ratio. Default is252
.
- DataFrame: A DataFrame with a single
'information_ratio'
column.
Alpha measures the excess return of an asset or portfolio relative to the return predicted by the Capital Asset Pricing Model (CAPM). Positive alpha indicates outperformance, while negative alpha indicates underperformance.
df['alpha'] = bta.alpha(df, benchmark_column='benchmark', risk_free_rate=0.01, window=252)
df
(pandas.DataFrame): Input DataFrame containing asset and benchmark returns.benchmark_column
(str): The column containing benchmark returns.risk_free_rate
(float): The risk-free rate. Default is0.01
.window
(int): Look-back period for calculating Alpha. Default is252
.
- DataFrame: A DataFrame with a single
'alpha'
column.
Beta measures the sensitivity of an asset's returns to the returns of the market (or a benchmark). A beta greater than 1 indicates that the asset is more volatile than the market, while a beta less than 1 indicates lower volatility.
df['beta'] = bta.beta(df, benchmark_column='benchmark', window=252)
df
(pandas.DataFrame): Input DataFrame containing asset and benchmark returns.benchmark_column
(str): The column containing benchmark returns.window
(int): Look-back period for calculating Beta. Default is252
.
- DataFrame: A DataFrame with a single
'beta'
column.
The Treynor Ratio measures the return of an asset or portfolio relative to its market risk, as measured by beta. It is similar to the Sharpe Ratio but focuses on systematic risk instead of total risk.
df['treynor_ratio'] = bta.treynor_ratio(df, benchmark_column='benchmark', risk_free_rate=0.01, window=252)
df
(pandas.DataFrame): Input DataFrame containing asset and benchmark returns.benchmark_column
(str): The column containing benchmark returns.risk_free_rate
(float): The risk-free rate. Default is0.01
.window
(int): Look-back period for calculating the Treynor Ratio. Default is252
.
- DataFrame: A DataFrame with a single
'treynor_ratio'
column.
Jensen's Alpha measures the excess return of an asset or portfolio over its expected return based on the Capital Asset Pricing Model (CAPM). Positive Jensen's Alpha indicates outperformance, while negative alpha indicates underperformance.
df['jensens_alpha'] = bta.jensens_alpha(df, benchmark_column='benchmark', risk_free_rate=0.01, window=252)
df
(pandas.DataFrame): Input DataFrame containing asset and benchmark returns.benchmark_column
(str): The column containing benchmark returns.risk_free_rate
(float): The risk-free rate. Default is0.01
.window
(int): Look-back period for calculating Jensen's Alpha. Default is252
.
- DataFrame: A DataFrame with a single
'jensens_alpha'
column.