%% KRG Post Trade Analysis Example
% This example performs post trade analysis and calculations using Kissell
% Research Group transaction cost analytics.  The calculations are
% implementation shortfall, benchmark cost, and broke value add metric.
%
% Users need to provide their trade list: Symbol, Side, Shares, and Avg
% Execution Price for analysis.
%
% For Implementation Shortfall calculations, the user must enter data for
% Decision Price, Arrival Price, End Price, and Fixed Dollar Cost.
% 
% For Benchmark Cost calculations, the user must enter data for the 
% benchmark price fields for the analysis, for example, Previous Close, 
% Open, Close, Arrival Price, Full Day VWAP or Period VWAP.
%
% For Value-Add calculations, the user must enter data for the Arrival 
% Price, and corresponding point-in-time trade data to compute the expected 
% trading costs during the actual trading horizon.
%
% It is important to note that the user needs to ensure that the price data
% for the trade list and market prices at the time of the trade is actual 
% point-time data.
%
% The data may appear as
%
% PostTradeData = 
% 
%     Symbol     Side     SideIndicator       Date        DecisionTime    ArrivalTime     EndTime      AvgExecPrice    OrderShares    TradedShares    Volatility      ADV          POV        CCYRate    MICategory    PrevClose    Open     Close    ArrivalPrice    PeriodVWAP      Broker      Algorithm    Manager       Trader      SectorCategory    OrderSizeCategory    VolatilityCategory    POVRateCategory    MktCapCategory    StockMomentumCategory    MktMovementCategory    StepOut    ISDecisionPrice    ISArrivalPrice    ISEndPrice    ISFixedDollars    ISDollars        ISFixed            ISDelayCost        ISExecutionCost     ISOpportunityCost          ISCost           Alpha_Realized      Alpha_TotalPeriod    Alpha_CapturePct      PrevClose_Cost          Open_Cost           Close_Cost          Arrival_Cost        PeriodVWAP_Cost              Size            Price
%     ______    ______    _____________    ___________    ____________    ___________    __________    ____________    ___________    ____________    __________    _______    ___________    _______    __________    _________    _____    _____    ____________    __________    __________    _________    ________    __________    ______________    _________________    __________________    _______________    ______________    _____________________    ___________________    _______    _______________    ______________    __________    ______________    _________    ________________    _________________    _________________    __________________    _________________    _________________    _________________    _________________    _________________    _________________    _________________    _________________    __________________    ____________________    _____
% 
%     'AAL'     'Buy'      1               '7/5/2015'     '9:30 AM'       '3:45 PM'      '4:00 PM'     48.6735         10200           9300           0.48          2984000    0.000981426    1          1             47.64        47.83    48.65    48.83           48.6731       'Broker-A'    'VWAP'       'PM-IV'     'Trader-5'    'Industrials'     'Small'              'High'                'Passive'          'LC'              'Neutral'                'Neutral'              0          47.83              48.83             48.66          166               487866      3.40257365752071     20.9073803052478    -2.98329869267383    -0.313610704578719     21.0130445655159    -2.76469383575681    -34.8146631169367    0.920588235294115     216.939546599495     176.353752874765     4.83042137718366    -32.0499692811798    0.0821809171799347     0.00311662198391421    48.83
%     'AAPL'    'Sell'    -1               '6/28/2015'    '9:30 AM'       '3:45 PM'      '3:59 PM'     41.4577         29000          27200           0.46           981000    0.168111661    1          1                40        40.22    41.69    40.58           41.3334       'Broker-E'    'Dynamic'    'PM-V'      'Trader-4'    'Cons. Disc'      'Large'              'High'                'Normal'           'SC'              'Neutral'                'Neutral'              0          40.22              40.58              41.7          982              1166380      8.41921157770195     -8.9507707608155    -20.4679778459851     -1.72842469864024    -22.7279617277389    -59.7092163627403    -275.998028585511    0.783660714285715    -364.425000000001    -307.732471407261     55.7207963540406    -216.288812222771     -30.0725321410784      0.0277268093781855    40.58
%     'AAWW'    'Buy'      1               '7/16/2015'    '9:30 AM'       '11:30 AM'     '11:30 AM'    20.6463          1500           1500           0.22          5086000    0.069224854    1          1              20.4        20.46    20.72    20.49           20.6157       'Broker-A'    'TWAP'       'PM-II'     'Trader-4'    'Cons. Disc'      'Small'              'Medium'              'Passive'          'LC'              'Neutral'                'Neutral'              0          20.46              20.49             20.84         2242                30690      730.531117627892     1.46627565982393     7.63929618768337                     0     739.636689475399     94.5339189848706     170.815031722792    0.553428571428568     120.735294117648     91.0557184750729    -35.5694980694975     76.2811127379218      14.8430565054787    0.000294927251278018    20.49
%     'ADM'     'Buy'      1               '6/28/2015'    '9:30 AM'       '3:04 PM'      '3:04 PM'     57.4057           900            900           0.22          5140000    0.025274147    1          1             54.84        56.53    57.23    57.63           57.4096       'Broker-D'    'Arrival'    'PM-II'     'Trader-5'    'Cons. Disc'      'Small'              'Medium'              'Passive'          'LC'              'Neutral'                'Neutral'              0          56.53              57.63             57.32          112                50877      22.0138766043595     19.4586944984964    -3.96780470546612                     0     37.5047663973898    -14.8707270518832    -53.7914280756554    0.723548387096767     467.851932895697     154.908897930303     30.7006814607734    -38.9207010237723    -0.679328892727778    0.000175097276264591    57.63
%     'ADT'     'Buy'      1               '7/14/2015'    '9:30 AM'       '3:45 PM'      '3:59 PM'      9.7919         78600          78600            0.3          2372000    0.129275867    1          1              9.55         9.55     9.93     9.49            9.8058       'Broker-A'    'Dynamic'    'PM-III'    'Trader-4'    'Cons. Disc'      'Medium'             'Medium'              'Normal'           'MC'              'Favorable'              'Neutral'              0           9.55               9.49              9.93         2354               750630      31.3603239945113    -6.28272251308906     31.6125654450262                     0     56.6901669264484     145.521601685985     463.645943097997    0.313863636363636     253.298429319371     253.298429319371    -139.073514602215     318.124341412012     -14.1752840155822      0.0331365935919056     9.49
%
% and the completed analysis returns
%
% PostTradeData = 
% 
%     Symbol     Side     SideIndicator       Date        DecisionTime    ArrivalTime     EndTime      AvgExecPrice    OrderShares    TradedShares    Volatility      ADV          POV        CCYRate    MICategory    PrevClose    Open     Close    ArrivalPrice    PeriodVWAP      Broker      Algorithm    Manager       Trader      SectorCategory    OrderSizeCategory    VolatilityCategory    POVRateCategory    MktCapCategory    StockMomentumCategory    MktMovementCategory    StepOut    ISDecisionPrice    ISArrivalPrice    ISEndPrice    ISFixedDollars    ISDollars        ISFixed            ISDelayCost        ISExecutionCost     ISOpportunityCost          ISCost           Alpha_Realized      Alpha_TotalPeriod    Alpha_CapturePct      PrevClose_Cost          Open_Cost           Close_Cost          Arrival_Cost        PeriodVWAP_Cost              Size            Price         miCost                 tr               ValueAdd              zScore      
%     ______    ______    _____________    ___________    ____________    ___________    __________    ____________    ___________    ____________    __________    _______    ___________    _______    __________    _________    _____    _____    ____________    __________    __________    _________    ________    __________    ______________    _________________    __________________    _______________    ______________    _____________________    ___________________    _______    _______________    ______________    __________    ______________    _________    ________________    _________________    _________________    __________________    _________________    _________________    _________________    _________________    _________________    _________________    _________________    _________________    __________________    ____________________    _____    _________________    ________________    _________________    _________________
% 
%     'AAL'     'Buy'      1               '7/5/2015'     '9:30 AM'       '3:45 PM'      '4:00 PM'     48.6735         10200           9300           0.48          2984000    0.000981426    1          1             47.64        47.83    48.65    48.83           48.6731       'Broker-A'    'VWAP'       'PM-IV'     'Trader-5'    'Industrials'     'Small'              'High'                'Passive'          'LC'              'Neutral'                'Neutral'              0          47.83              48.83             48.66          166               487866      3.40257365752071     20.9073803052478    -2.98329869267383    -0.313610704578719     21.0130445655159    -2.76469383575681    -34.8146631169367    0.920588235294115     216.939546599495     176.353752874765     4.83042137718366    -32.0499692811798    0.0821809171799347     0.00311662198391421    48.83    0.824094739154673    312.184022623923     32.8740640203345    0.105303480120559
%     'AAPL'    'Sell'    -1               '6/28/2015'    '9:30 AM'       '3:45 PM'      '3:59 PM'     41.4577         29000          27200           0.46           981000    0.168111661    1          1                40        40.22    41.69    40.58           41.3334       'Broker-E'    'Dynamic'    'PM-V'      'Trader-4'    'Cons. Disc'      'Large'              'High'                'Normal'           'SC'              'Neutral'                'Neutral'              0          40.22              40.58              41.7          982              1166380      8.41921157770195     -8.9507707608155    -20.4679778459851     -1.72842469864024    -22.7279617277389    -59.7092163627403    -275.998028585511    0.783660714285715    -364.425000000001    -307.732471407261     55.7207963540406    -216.288812222771     -30.0725321410784      0.0277268093781855    40.58     35.7737637742356    62.2172407806339     252.062575997007     4.05133003062175
%     'AAWW'    'Buy'      1               '7/16/2015'    '9:30 AM'       '11:30 AM'     '11:30 AM'    20.6463          1500           1500           0.22          5086000    0.069224854    1          1              20.4        20.46    20.72    20.49           20.6157       'Broker-A'    'TWAP'       'PM-II'     'Trader-4'    'Cons. Disc'      'Small'              'Medium'              'Passive'          'LC'              'Neutral'                'Neutral'              0          20.46              20.49             20.84         2242                30690      730.531117627892     1.46627565982393     7.63929618768337                     0     739.636689475399     94.5339189848706     170.815031722792    0.553428571428568     120.735294117648     91.0557184750729    -35.5694980694975     76.2811127379218      14.8430565054787    0.000294927251278018    20.49     2.18504006338673    5.05872260489979     -74.096072674535    -14.6471903011181
%     'ADM'     'Buy'      1               '6/28/2015'    '9:30 AM'       '3:04 PM'      '3:04 PM'     57.4057           900            900           0.22          5140000    0.025274147    1          1             54.84        56.53    57.23    57.63           57.4096       'Broker-D'    'Arrival'    'PM-II'     'Trader-5'    'Cons. Disc'      'Small'              'Medium'              'Passive'          'LC'              'Neutral'                'Neutral'              0          56.53              57.63             57.32          112                50877      22.0138766043595     19.4586944984964    -3.96780470546612                     0     37.5047663973898    -14.8707270518832    -53.7914280756554    0.723548387096767     467.851932895697     154.908897930303     30.7006814607734    -38.9207010237723    -0.679328892727778    0.000175097276264591    57.63    0.896410702578935    6.60138000449572     39.8171117263512     6.03163455205346
%     'ADT'     'Buy'      1               '7/14/2015'    '9:30 AM'       '3:45 PM'      '3:59 PM'      9.7919         78600          78600            0.3          2372000    0.129275867    1          1              9.55         9.55     9.93     9.49            9.8058       'Broker-A'    'Dynamic'    'PM-III'    'Trader-4'    'Cons. Disc'      'Medium'             'Medium'              'Normal'           'MC'              'Favorable'              'Neutral'              0           9.55               9.49              9.93         2354               750630      31.3603239945113    -6.28272251308906     31.6125654450262                     0     56.6901669264484     145.521601685985     463.645943097997    0.313863636363636     253.298429319371     253.298429319371    -139.073514602215     318.124341412012     -14.1752840155822      0.0331365935919056     9.49     24.3316408621064    51.7518780709164    -293.792700549906    -5.67694761042909

%   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 PostTradeData

% Calculate implementation shortfall costs in basis points
PostTradeData.ISDollars = PostTradeData.OrderShares .* PostTradeData.ISDecisionPrice;
PostTradeData.ISFixed = PostTradeData.ISFixedDollars ./ PostTradeData.ISDollars * 10000;
PostTradeData.ISDelayCost = PostTradeData.OrderShares .* (PostTradeData.ISArrivalPrice-PostTradeData.ISDecisionPrice) .* PostTradeData.SideIndicator ./ PostTradeData.ISDollars * 1000;
PostTradeData.ISExecutionCost = PostTradeData.TradedShares .* (PostTradeData.AvgExecPrice-PostTradeData.ISArrivalPrice) .* PostTradeData.SideIndicator ./ PostTradeData.ISDollars * 1000;
PostTradeData.ISOpportunityCost = (PostTradeData.OrderShares-PostTradeData.TradedShares) .* (PostTradeData.ISEndPrice-PostTradeData.ISArrivalPrice) .* PostTradeData.SideIndicator ./ PostTradeData.ISDollars * 1000;

% Calculate total implementation shortfall cost
PostTradeData.ISCost = PostTradeData.ISFixed+PostTradeData.ISDelayCost + PostTradeData.ISExecutionCost + PostTradeData.ISOpportunityCost;

% Calculate the alpha capture or profit
PostTradeData.Alpha_Realized = (PostTradeData.ISEndPrice-PostTradeData.AvgExecPrice) .* PostTradeData.TradedShares .* PostTradeData.SideIndicator ./ (PostTradeData.TradedShares .* PostTradeData.ISArrivalPrice) * 10000;
PostTradeData.Alpha_TotalPeriod = (PostTradeData.ISEndPrice-PostTradeData.ISArrivalPrice) .* PostTradeData.TradedShares .* PostTradeData.SideIndicator ./ (PostTradeData.TradedShares .* PostTradeData.ISArrivalPrice) * 10000;
lenAlpha_Realized = length(PostTradeData.Alpha_Realized);
PostTradeData.Alpha_CapturePct = zeros(lenAlpha_Realized,1);
for ii = 1:lenAlpha_Realized
    if PostTradeData.Alpha_TotalPeriod(ii) > 0
        PostTradeData.Alpha_CapturePct(ii) = PostTradeData.Alpha_Realized(ii) ./ PostTradeData.Alpha_TotalPeriod(ii);
    else
        PostTradeData.Alpha_CapturePct(ii) = -(PostTradeData.Alpha_Realized(ii) - PostTradeData.Alpha_TotalPeriod(ii)) ./ PostTradeData.Alpha_TotalPeriod(ii);
    end
end

% Calculate benchmark costs in basis points
PostTradeData.PrevClose_Cost = (PostTradeData.AvgExecPrice-PostTradeData.PrevClose) .* PostTradeData.SideIndicator ./ PostTradeData.PrevClose * 10000;
PostTradeData.Open_Cost = (PostTradeData.AvgExecPrice-PostTradeData.Open) .* PostTradeData.SideIndicator./PostTradeData.Open * 10000;
PostTradeData.Close_Cost = (PostTradeData.AvgExecPrice-PostTradeData.Close) .* PostTradeData.SideIndicator ./ PostTradeData.Close * 10000;
PostTradeData.Arrival_Cost = (PostTradeData.AvgExecPrice-PostTradeData.ArrivalPrice) .* PostTradeData.SideIndicator ./ PostTradeData.ArrivalPrice * 10000;
PostTradeData.PeriodVWAP_Cost = (PostTradeData.AvgExecPrice-PostTradeData.PeriodVWAP) .* PostTradeData.SideIndicator ./ PostTradeData.PeriodVWAP * 10000;

% Calculate broker value add.  This calculation is dependent on Arrival
% Price (Price) and Arrival Cost.

% Compute the market impact  and timing risk cost
PostTradeData.Size = PostTradeData.TradedShares./PostTradeData.ADV;
PostTradeData.Price = PostTradeData.ArrivalPrice;
PostTradeData.miCost = marketImpact(k,PostTradeData);
PostTradeData.tr = timingRisk(k,PostTradeData);

% Value Add Calculation
PostTradeData.ValueAdd = (PostTradeData.Arrival_Cost - PostTradeData.miCost) * -1;

% Z-Score
PostTradeData.zScore = PostTradeData.ValueAdd./PostTradeData.tr;