Clear Filters
Clear Filters

How to calculate risk indicator for a portfolio as maximum drawdown, sortino ratio and ulcer index?

17 views (last 30 days)
Based on the following code, is it possible to have the correct formula to calculate the maximum drawdown, the sortino ratio and the ulcer index?
clear all;
clc;
NumPorts=2000;
% Set Up the Data
open ("MatlabBOGLE.xlsx");
t = readtable("MatlabBOGLE.xlsx");
symbol = t.Properties.VariableNames(2:end);
dailyreturn= tick2ret(t(:,2:end));
dailyreturn2 = table2array(dailyreturn);
% Create a Portfolio Object (with the risk-free rate)
RiskFreeRate=0.00/252;
p = Portfolio("AssetList",symbol,"RiskFreeRate",RiskFreeRate);
x0 =[0.1,0.1,0.3,0.5,0,0];
p = setInitPort(p,x0);
p = estimateAssetMoments(p,dailyreturn);
[initialrisk,initialreturn] = estimatePortMoments(p,p.InitPort);
display(initialrisk); %"Rischio" Iniziale del lazy Portfolio con le %di allocazioni inalterate
display(initialreturn); %Rendimento Iniziale del lazy Portfolio con le %di allocazioni inalterate
clf;
portfolioexamples_plot('Asset Risks and Returns', ...
{'scatter', initialrisk,initialreturn, {'StartPort'}}, ...
{'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList, '.r'});
% Set Up a Portfolio Optimization Problem
p=setDefaultConstraints(p);
pwgt = estimateFrontier(p,NumPorts);
[portrisk,portret] = estimatePortMoments (p,pwgt);
figure
portfolioexamples_plot('Efficient Frontier', ...
{'line', portrisk, portret}, ...
{'scatter', initialrisk, initialreturn, {'StartPort'}}, ...
{'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList, '.r'});
% Maximize the Sharpe Ratio
p = setInitPort(p, x0);
swgt = estimateMaxSharpeRatio(p);
[srsk,sret] = estimatePortMoments(p,swgt);
display(swgt);
display(srsk);
display(sret);
figure
portfolioexamples_plot('Efficient Frontier with Maximum Sharpe Ratio Portfolio', ...
{'line', portrisk, portret}, ...
{'scatter', srsk, sret, {'Sharpe'}}, ...
{'scatter', initialrisk,initialreturn, {'StartPort'}}, ...
{'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList, '.r'});

Accepted Answer

LeoAiE
LeoAiE on 23 Apr 2023
In your current code, you've implemented a portfolio optimization strategy using the Sharpe ratio. To calculate the maximum drawdown, Sortino ratio, and ulcer index, you can
  1. Calculate the portfolio returns using the optimal weights obtained from the portfolio optimization.
  2. Calculate the maximum drawdown.
  3. Calculate the Sortino ratio.
  4. Calculate the ulcer index.
% Your existing code
% ...
% Step 1: Calculate portfolio returns using the optimal weights (swgt)
portfolio_returns = dailyreturn2 * swgt;
% Step 2: Calculate maximum drawdown
cumulative_returns = cumprod(1 + portfolio_returns) - 1;
cumulative_max = cummax(cumulative_returns);
drawdowns = (cumulative_max - cumulative_returns) ./ (cumulative_max + 1);
max_drawdown = max(drawdowns);
% Step 3: Calculate Sortino ratio
target_return = 0; % Define target return (usually 0 for Sortino ratio)
excess_returns = portfolio_returns - RiskFreeRate;
downside_returns = excess_returns;
downside_returns(downside_returns > target_return) = 0;
sortino_ratio = (mean(excess_returns) - target_return) / sqrt(mean(downside_returns .^ 2));
% Step 4: Calculate Ulcer index
ulcer_index = sqrt(mean(drawdowns .^ 2));
% Display the results
display(max_drawdown);
display(sortino_ratio);
display(ulcer_index);
  4 Comments
Mattia Smanio
Mattia Smanio on 25 Apr 2023
Thank you for your kind cooperation (I will buy for sure the book that you have recomended), at least i would ask two more questions:
  1. Is there a summary to calculate the other indicators like Volatility, Upside Potential Ratio, Sterling Ratio, Omega Ratio, and MAR?
  2. In the following picture, is it possible to change x axis with the first column of my file excel named "Date" (connected with the previous part of the code)?
LeoAiE
LeoAiE on 7 May 2023
Volatility (annualized): Since you already have daily returns, you can calculate the annualized volatility using the standard deviation of daily returns.
volatility = std(portfolio_returns) * sqrt(252);
Upside Potential Ratio: It measures the expected value of returns above a target return.
upside_returns = excess_returns;
upside_returns(upside_returns < target_return) = 0;
upside_potential_ratio = mean(upside_returns) / sqrt(mean(downside_returns .^ 2));
Sterling Ratio: It measures the reward-to-pain ratio.
sterling_ratio = (mean(portfolio_returns) - RiskFreeRate) / (max_drawdown + 0.1); % Adding a constant to the denominator
Omega Ratio: It measures the ratio of the probability weighted gains to losses.
gain_returns = excess_returns;
gain_returns(gain_returns < target_return) = 0;
loss_returns = excess_returns;
loss_returns(loss_returns >= target_return) = 0;
omega_ratio = sum(gain_returns) / abs(sum(loss_returns));
MAR (Managed Account Reports) Ratio: It is similar to the Calmar ratio.
mar_ratio = (mean(portfolio_returns) - RiskFreeRate) / max_drawdown;
To plot the graph with the x-axis as dates, you can use the following code:
dates = t.Date(2:end); % Assuming you have dates in the first column, skipping the header
figure;
plot(dates, cumulative_returns, 'b-', 'LineWidth', 1.5);
hold on;
plot(dates, cumulative_max, 'r--', 'LineWidth', 1.5);
hold off;
legend('Cumulative Returns', 'Cumulative Maximum');
xlabel('Time');
ylabel('Cumulative Returns');
title('Drawdown Graph');
grid on;
datetick('x', 'yyyy-mm-dd', 'keepticks'); % Format the x-axis dates

Sign in to comment.

More Answers (0)

Categories

Find more on Portfolio Optimization and Asset Allocation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!