%% KRG Portfolio Liquidity Example
% The users needs to evaluate execution strategies such as cost to liquidate
% a portfolio or components and the corresponding time frame.  The goal is to 
% determine the cost of liquidating individual stocks in the portfolio and 
% the sensitivity of prices to order flow.   
%
% The analysis returns liquidity factor, trade times, costs as well as 
% charts that display market impact, timing risk, order size, volatility, 
% and liquidity factor data.

%   Copyright 2016-2020 The MathWorks, Inc.

% Load the market impact data provided by the Kissell Research Group
f = ftp('ftp.kissellresearch.com','username','password');
cd(f,'MI_Parameters');
mget(f,'MI_Encrypted_Parameters.csv');

miData = readtable('MI_Encrypted_Parameters.csv','Format','auto','delimiter',',','ReadRowNames',false,'ReadVariableNames',true);
k = krg(miData,datetime('today'),1,250);

% Get example data
load KRGExampleData.mat TradeData

% Calculate market impact cost
TradeData.mi = marketImpact(k,TradeData);

% Calculate timing risk
TradeData.tr = timingRisk(k,TradeData);

% Calculate liquidity factor
TradeData.lf = liquidityFactor(k,TradeData);

% Plot data
axOrder = subplot(2,3,1);
nSymbols = 1:length(TradeData.Size);
scatter(nSymbols,TradeData.Size*100,10,'filled')
grid on
box on
title(' Order Size (%ADV)')
set(axOrder.YAxis,'TickLabelFormat','%.1f%%')

axVolatility = subplot(2,3,2);
scatter(nSymbols,TradeData.Volatility*100,10,'filled')
grid on
box on
title('Volatility')
set(axVolatility.YAxis,'TickLabelFormat','%g%%')

axMI = subplot(2,3,4);
scatter(nSymbols,TradeData.mi,10,'filled')
grid on
box on
title('Market Impact (bp)')
set(axMI.YAxis,'TickLabelFormat','%.1f')

axTR = subplot(2,3,5);
scatter(nSymbols,TradeData.tr,10,'filled')
grid on
box on
title('Timing Risk (bp)')
set(axTR.YAxis,'TickLabelFormat','%.1f')

axLF = subplot(2,3,6);
scatter(nSymbols,TradeData.lf*100,10,'filled')
grid on
box on
title('Liquidity Factor')
set(axLF.YAxis,'TickLabelFormat','%.2f%%')