Compute Performance Table#
The compute_performance_table
function calculates a performance table for a given pandas DataFrame of prices. It takes into account the frequency of the data and allows customization of the output. The performance table provides key performance metrics, such as CAGR, Standard Deviation, Sharpe Ratio, Maximum Drawdown, and MAR.
Function#
def compute_performance_table(dataframe, years='si', freq='daily', numeric=False, ms_table=False, title=True):
"""
Function to calculate a performance table given a dataframe of prices.
Takes into account the frequency of the data.
"""
def print_title(title_str):
print("\n" + title_str + "\n" + "-" * len(title_str))
if years == 'si':
years = len(pd.date_range(dataframe.index[0], dataframe.index[-1], freq='D')) / 365.25
metrics = pd.DataFrame([compute_cagr(dataframe, years),
compute_std_dev(dataframe, freq),
compute_sharpe(dataframe, years, freq),
compute_max_DD(dataframe),
compute_mar(dataframe)])
metrics.index = ['CAGR', 'StdDev', 'Sharpe', 'Max DD', 'MAR']
metrics = round(metrics.transpose(), 2)
# Format as percentage
if not numeric:
metrics['CAGR'] = (metrics['CAGR'] / 100).apply('{:.2%}'.format)
metrics['StdDev'] = (metrics['StdDev'] / 100).apply('{:.2%}'.format)
metrics['Max DD'] = (metrics['Max DD'] / 100).apply('{:.2%}'.format)
if title:
start = str(dataframe.index[0])[0:10]
end = str(dataframe.index[-1])[0:10]
print_title('Performance from ' + start + ' to ' + end + ' (≈ ' + str(round(years, 1)) + ' years)')
return metrics
else:
dataframe = filter_by_date(dataframe, years)
metrics = pd.DataFrame([compute_cagr(dataframe, years=years),
compute_std_dev(dataframe),
compute_sharpe(dataframe),
compute_max_DD(dataframe),
compute_mar(dataframe)])
metrics.index = ['CAGR', 'StdDev', 'Sharpe', 'Max DD', 'MAR']
metrics = round(metrics.transpose(), 2)
# Format as percentage
if not numeric:
metrics['CAGR'] = (metrics['CAGR'] / 100).apply('{:.2%}'.format)
metrics['StdDev'] = (metrics['StdDev'] / 100).apply('{:.2%}'.format)
metrics['Max DD'] = (metrics['Max DD'] / 100).apply('{:.2%}'.format)
if title:
start = str(dataframe.index[0])[0:10]
end = str(dataframe.index[-1])[0:10]
if years == 1:
print_title('Performance from ' + start + ' to ' + end + ' (' + str(years) + ' year)')
else:
print_title('Performance from ' + start + ' to ' + end + ' (' + str(years) + ' years)')
return metrics
Parameters#
dataframe (pandas.DataFrame): DataFrame containing price data. The index should be of datetime type.
years (str or int, optional): The number of years to include in the calculation. Default is ‘si’, which calculates the performance for the entire DataFrame.
freq (str, optional): Frequency of the data. Default is ‘daily’.
numeric (bool, optional): If False, formats the output as a percentage. Default is False.
ms_table (bool, optional): If True, formats the output as a Morningstar-style table. Default is False. title (bool, optional): If True, prints a title with the performance period. Default is True.
Returns#
pandas.DataFrame: A DataFrame containing the performance metrics for the input price data.
Example#
import ds4finance as dsf
import yfinance as yf
import pandas as pd
def download_data(ticker, start, end):
data = yf.download(ticker, start=start, end=end, progress=False)
adj_close = data['Adj Close']
return adj_close
ticker = 'SPY'
start_date = '2015-12-31'
end_date = '2022-12-31'
# Load your price data into a pandas DataFrame
spy = pd.DataFrame(download_data(ticker, start_date, end_date))
spy.columns = ['spy']
# Calculate the performance table for the last 5 years
performance_5_years = dsf.compute_performance_table(spy, years=5, freq='daily')
performance_5_years
Performance from 2018-01-02 to 2022-12-30 (5 years)
---------------------------------------------------
CAGR | StdDev | Sharpe | Max DD | MAR | |
---|---|---|---|---|---|
spy | 9.16% | 21.54% | 0.43 | -33.72% | 0.27 |
# Calculate the performance table for the entire dataset
total_performance = dsf.compute_performance_table(spy)
total_performance
Performance from 2015-12-31 to 2022-12-30 (≈ 7.0 years)
-------------------------------------------------------
CAGR | StdDev | Sharpe | Max DD | MAR | |
---|---|---|---|---|---|
spy | 11.39% | 19.04% | 0.6 | -33.72% | 0.34 |
# Calculate the performance table for the entire dataset
total_performance = dsf.compute_performance_table(spy, numeric=True)
total_performance
Performance from 2015-12-31 to 2022-12-30 (≈ 7.0 years)
-------------------------------------------------------
CAGR | StdDev | Sharpe | Max DD | MAR | |
---|---|---|---|---|---|
spy | 11.39 | 19.04 | 0.6 | -33.72 | 0.34 |
# Calculate the performance table for the entire dataset
total_performance = dsf.compute_performance_table(spy, title=True)
total_performance
Performance from 2015-12-31 to 2022-12-30 (≈ 7.0 years)
-------------------------------------------------------
CAGR | StdDev | Sharpe | Max DD | MAR | |
---|---|---|---|---|---|
spy | 11.39% | 19.04% | 0.6 | -33.72% | 0.34 |