function f = krgLiquidityFunction(x,TradeData,PortfolioTargetValue,k)
%KRGLIQUIDITYFUNCTION Example objective function used by KRGLIQUIDITYOPTIMIZATIONEXAMPLE.
%   F = KRGLIQUIDITYEXAMPLE(X,TRADEDATA,PORTFOLIOTARGETVALUE,K) 

%   Copyright 2016-2020 The MathWorks, Inc.

% Calculate Shares from weights, portfolio value, and stock price
TargetShares=x*PortfolioTargetValue./TradeData.Price;
SharesToTrade=TradeData.Shares-TargetShares;

% MI Cost requires positive Shares
TradeData.Shares=abs(SharesToTrade);

% ADV factor in TradeTime is different than tradetime2pov calc.
TradeData.TradeTime = TradeData.TradeTime .* TradeData.ADV;
TradeData.POV = krg.tradetime2pov(TradeData.TradeTime,TradeData.Shares);

% marketImpact calculation, convert from bp to decimal
MI = marketImpact(k,TradeData)/10000;

f = MI' * abs(x);