www.gusucode.com > trading工具箱matlab源码程序 > trading/trading/@krg/portfolioCostCurves.m

    function pcc = portfolioCostCurves(k,portfolioData,tradeQuantity,tqRange,tradeStrategy,tsRange)
%PORTFOLIOCOSTCURVES Market impact cost of order execution.
%   PCC = PORTFOLIOCOSTCURVES(K,PORTFOLIODATA,TRADEQUANTITY,TQRANGE,TRADESTRATEGY,TSRANGE)
%   calculates the market impact cost curves given PORTFOLIODATA, the trade
%   quantity TQRANGE, and trade strategy TSRANGE.  TRADEQUANTITY can be
%   specified as DollarValue or PercentValue.    TRADESTRATEGY can be specified
%   as POV or TradeTime.   The values of TQRANGE and TSRANGE should match
%   these inputs.  TQRANGE is entered as a range of portfolio values or as 
%   a range of percentages of portfolio values. TSRANGE is entered as range
%   of POV values or as a trade time range.  K is the KRG object containing
%   the market impact parameters.   Note the size of the market impact
%   parameters in K, the KRG object, must match the number of symbols in
%   the input data.   If the input portfolio consists of 10 symbols, there
%   must be 10 corresponding values for each market impact parameter.
%
%   For example,
%
%   PortfolioData = 
% 
%     Symbol    Price_Local    Price_Currency        ADV         Volatility      Shares  
%     ______    ___________    ______________    ____________    __________    __________
% 
%     'AA'        8.50           8.50             16292000.00    0.22           753123.00
%     'AAPL'    428.85         428.85             19569000.00    0.18           146000.00
%     'ABBV'     46.03          46.03              6199000.00    0.31          -497700.00
%     'ADP'      71.74          71.74              1728000.00    0.15           145300.00
%     'AES'      13.08          13.08              6281000.00    0.25           149600.00
%     'AET'      59.83          59.83              3288000.00    0.23          -293600.00
%     'AIZ'      49.57          49.57               756000.00    0.19            41100.00
%
%   pcc = portfolioCostCurves(k,PortfolioData,'dollarValue',(100000000:10000000:200000000),'POV',[(0.35:0.05:0.50)]);
%
%   pcc = 
% 
%     Size      Shares       TradeValue     AbsTradeValue    POV     TradeTime    Cost_bp    Cost_DollarsPerShare    Cost_Dollars
%     ____    __________    ____________    _____________    ____    _________    _______    ____________________    ____________
% 
%     0.10    4717401.05    100000000.00    288448157.84     0.35    0.18         123.67     0.26                    1236651.07  
%     0.10    4717401.05    100000000.00    288448157.84     0.40    0.15         135.17     0.29                    1351669.20  
%     0.10    4717401.05    100000000.00    288448157.84     0.45    0.12         146.21     0.31                    1462072.33  
%     0.10    4717401.05    100000000.00    288448157.84     0.50    0.10         156.85     0.33                    1568531.40  
%     0.11    5189141.16    110000000.00    317292973.62     0.35    0.20         127.98     0.27                    1407829.62  
%     0.11    5189141.16    110000000.00    317292973.62     0.40    0.16         139.89     0.30                    1538768.69  
%     0.11    5189141.16    110000000.00    317292973.62     0.45    0.13         151.31     0.32                    1664453.93  
%     0.11    5189141.16    110000000.00    317292973.62     0.50    0.11         162.33     0.34                    1785649.18  
%     0.12    5660881.26    120000000.00    346137789.40     0.35    0.22         132.06     0.28                    1584713.06  
%     0.12    5660881.26    120000000.00    346137789.40     0.40    0.18         144.34     0.31                    1732103.67  
%     0.12    5660881.26    120000000.00    346137789.40     0.45    0.14         156.13     0.33                    1873580.34  
%        ...
%
%    References:   The Science of Algorithmic Trading
%                  Optimal Trading Strategy
%                  Multi-Asset Risk Modeling
%
%   See also costCurves, iStar, marketImpact, timingRisk.

%   Copyright 2016 The MathWorks, Inc.

% Number of symbols in data
numSymbols = length(portfolioData.Symbol);

% Determine length of trade quantity input and set trade quantity range
% based on trade quantity type input
numTradeQuantity = length(tqRange);
switch lower(tradeQuantity)
  
  case 'dollarvalue'
  
    % Trade quantity range is unchanged
  
  case 'percentvalue'
  
    tqRange = tqRange .* sum(portfolioData.Shares .* portfolioData.Price_Currency);

end

% Calculate stock and portfolio values in USD
stockValue_USD = portfolioData.Shares .* portfolioData.Price_Currency;
portfolioValue_USD = sum(stockValue_USD);

% Determine length of trade strategy input
numStrategy = length(tsRange);

% Preallocate temporary variables
tmpSize = NaN(numTradeQuantity*numStrategy,1);
tmpShares = tmpSize;
tmpTradeDollars = tmpSize;
tmpAbsTradeDollars = tmpSize;
tmpPOV = tmpSize;
tmpTradeTime = tmpSize;
tmpCostBp = tmpSize;
tmpCostDollarsPerShare = tmpSize;
tmpCostDollars = tmpSize;

% Iterate through combinations for trade quantity and trade strategy inputs
for i = 1:numTradeQuantity
  
  for j = 1:numStrategy
    
    % Calculate vector index counter
    pccCounter = i*numStrategy - numStrategy + j;
    
    % Iterate through trade quantity
    toTradeValue = tqRange(i);
    
    % Calculate size and absolute value traded shares
    Size = abs(toTradeValue ./ portfolioValue_USD .* stockValue_USD ./ portfolioData.Price_Currency ./ portfolioData.ADV);
    absTradeShares = abs(toTradeValue ./ portfolioValue_USD .* stockValue_USD ./ portfolioData.Price_Currency);
    
    % Iterate through trade strategy, calculate POV and trade time based on
    % trade strategy type input
    switch lower(tradeStrategy)
      
      case 'pov'
        
        POV = tsRange(j) * ones(numSymbols,1);
        TradeTime = krg.pov2tradetime(POV,Size);
         
      case 'tradetime'
        
        TradeTime = tsRange(j) * ones(numSymbols,1); 
        POV = krg.tradetime2pov(TradeTime,Size);
        
    end

    % Calculate stock trade value and weight
    stockTradeValue = toTradeValue ./ portfolioValue_USD .* stockValue_USD;
    absStockTradeValue = abs(stockTradeValue);
    absTradeWeight = absStockTradeValue ./ sum(absStockTradeValue);
    
    % Calculate market impact
    calcStruct.POV = POV;
    calcStruct.Volatility = portfolioData.Volatility;
    calcStruct.Size = Size;
    calcStruct.Price = portfolioData.Price_Local;
    mi = marketImpact(k,calcStruct);
    miUSD = mi/10000 .* absStockTradeValue;
    
    % Create temporary arrays of portfolio cost curve results
    tmpSize(pccCounter)=absTradeWeight'*Size;
    tmpShares(pccCounter)=sum(absTradeShares);
    tmpTradeDollars(pccCounter)=sum(stockTradeValue);
    tmpAbsTradeDollars(pccCounter)=sum(absStockTradeValue);
    tmpPOV(pccCounter)=absTradeWeight'*POV;
    tmpTradeTime(pccCounter)=absTradeWeight'*TradeTime;
    tmpCostDollars(pccCounter)=sum(miUSD);
    tmpCostDollarsPerShare(pccCounter)=sum(miUSD)/sum(absTradeShares);
    tmpCostBp(pccCounter)=sum(miUSD)/toTradeValue*10000;
        
  end
end
 
% Return table or structure based on input data type
if istable(portfolioData)
  pcc = table(tmpSize,tmpShares,tmpTradeDollars,tmpAbsTradeDollars,tmpPOV,tmpTradeTime,tmpCostBp,tmpCostDollarsPerShare,tmpCostDollars,'VariableNames',{'Size','Shares','TradeValue','AbsTradeValue','POV','TradeTime','Cost_bp','Cost_DollarsPerShare','Cost_Dollars'});
else
  pcc.Size = tmpSize;
  pcc.Shares = tmpShares;
  pcc.TradeValue = tmpTradeDollars;
  pcc.AbsTradeValue = tmpAbsTradeDollars;
  pcc.POV = tmpPOV;
  pcc.TradeTime = tmpTradeTime;
  pcc.Cost_bp = tmpCostBp;
  pcc.Cost_DollarsPerShare = tmpCostDollarsPerShare;
  pcc.Cost_Dollars = tmpCostDollars;
end