%% KRG Sensitivity Analysis Example
%   This example calculates the trading cost estimates based on actual and 
%   user specified market conditions. Users have the ability to specify 
%   adjustment factors to the market conditions and market impact parameters 
%   to calculate what these costs would be under different scenarios. 
%   Additionally, the difference in cost between the two scenarios (current 
%   and adjusted) is provided.  The adjusted values are the factors by
%   which the initial data is modified.
%
%   For example,
%
%    TradeData = 
% 
%     Symbol    Side    Shares     Size    Price       ADV       Volatility    POV     Alpha_bp
%     ______    ____    _______    ____    ______    ________    __________    ____    ________
% 
%     'IBM'     1         41742    0.01    162.72     4174150    0.196456       0.1    50      
%     'MSFT'    1       1380889    0.05     48.19    27617780    0.195524       0.2    20      
%     'AAPL'    1       4588737     0.1     115.5    45887370    0.219272      0.05    10      
%     'HSNI'    1         49455    0.15     72.58      329700     0.23793      0.15    25      
%     'GE'      1       5626336     0.2    26.371    28131680    0.150651       0.3    30
%     ...

%   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 data
load KRGExampleData.mat

% Initial trading cost calculations
ttc = iStar(k,TradeData);
mi = marketImpact(k,TradeData);
tr = timingRisk(k,TradeData);
pa = priceAppreciation(k,TradeData);
initTCA = [ttc mi tr pa];

% Use new parameters to recalculate input data to apply the data adjustments:
% In this example, POV and TradeTime are adjusted based on user strategy.
% These values are the factors by which the original values are changed.
adjADV = 0.5;
adjVolatility = 2.0;
adjPrice = 1.0;
adjVolume = 1.0;
adjAlpha = 1.0;
adjPOV = 1.0;
adjTradeTime = 1.0;

TradeDataAdj = TradeData;
TradeDataAdj.Size = TradeData.Size .* (1./adjADV);
TradeDataAdj.ADV = TradeData.ADV .* adjADV;
TradeDataAdj.Volatility = TradeData.Volatility .* adjVolatility;
TradeDataAdj.Price = TradeData.Price .* adjPrice;
TradeDataAdj.Alpha_bp = TradeData.Alpha_bp .* adjAlpha;

% The TradeTime and POV adjustment calculation are based on user strategy
[~,povFlag,timeFlag] = krg.krgDataFlags(TradeData);
if povFlag
  TradeDataAdj.POV = TradeData.POV.*adjPOV;
  TradeDataAdj.TradeTime = TradeDataAdj.Size .* ((1-TradeDataAdj.POV) ./ TradeDataAdj.POV) .* (1./adjVolume);
elseif timeFlag
  TradeDataAdj.TradeTime = tradedata.TradeTime .* adjTradeTime;
  TradeDataAdj.POV = TradeDataAdj.Size ./ (TradeDataAdj.Size + TradeDataAdj.TradeTime .* adjVolume);
end

% Calculate estimated trading costs based on new conditions
ttc = iStar(k,TradeDataAdj);
mi = marketImpact(k,TradeDataAdj);
tr = timingRisk(k,TradeDataAdj);
pa = priceAppreciation(k,TradeDataAdj);
newTCA = [ttc mi tr pa];

% Calculate the difference in the trading costs and return as table or
% structure based on format of input data
rawWI = newTCA - initTCA;
wi = table(rawWI(:,1),rawWI(:,2),rawWI(:,3),rawWI(:,4),'VariableNames',{'TTC','MI','TR','PA'});