Conduct Back Test on Portfolio
This example shows how to conduct a back test on a set of stocks using transaction cost analysis from the Kissell Research Group.
Analyze the implementation of an investment strategy on a specific day or date range.
Estimate historical market-impact costs and the corresponding dollar values for the specified historical dates.
Analyze the trading costs of different orders on various dates.
To access the example code, enter edit KRGBackTestingExample.m
at the command line.
Retrieve Market-Impact Parameters and Load Historical Data
Retrieve the market-impact data from the Kissell Research Group FTP site.
Connect to the FTP site using the ftp
function with a user
name and password. Navigate to the MI_Parameters
folder and
retrieve the market-impact data in the
MI_Encrypted_Parameters.csv
file.
miData
contains the encrypted market-impact date, code,
and parameters.
f = ftp('ftp.kissellresearch.com','username','pwd'); mget(f,'MI_Encrypted_Parameters.csv'); close(f) miData = readtable('MI_Encrypted_Parameters.csv','delimiter', ... ',','ReadRowNames',false,'ReadVariableNames',true);
Create a Kissell Research Group transaction cost analysis object
k
. Specify initial settings for the date, market-impact
code, and number of trading days.
k = krg(miData,datetime('today'),1,250);
Load the example data TradeDataBackTest
from the file
KRGExampleData.mat
, which is included with the
Datafeed Toolbox™.
load KRGExampleData TradeDataBackTest
For a description of the example data, see Kissell Research Group Data Sets.
Prepare Data for Back Testing
Determine the number of stocks numRecords
in the
portfolio.
numRecords = length(TradeDataBackTest.Symbol);
Preallocate the output data table o
.
o = table(TradeDataBackTest.Symbol,TradeDataBackTest.Side, ... TradeDataBackTest.Date,NaN(numRecords,1),NaN(numRecords,1), ... 'VariableNames',{'Symbol','Side','Date','MI','MIDollar'});
Ensure that the number of shares is a positive value using the
abs
function.
TradeDataBackTest.Shares = abs(TradeDataBackTest.Shares);
Convert trade time trade strategy to the percentage of volume trade strategy.
TradeDataBackTest.TradeTime = TradeDataBackTest.TradeTime ... .* TradeDataBackTest.ADV; TradeDataBackTest.POV = krg.tradetime2pov(TradeDataBackTest.TradeTime, ... TradeDataBackTest.Shares);
Conduct Back Test by Estimating Historical Market-Impact Costs
Estimate the historical market-impact costs for each stock in the portfolio on
different dates using marketImpact
. Convert market-impact
cost from decimal into local dollars. Retrieve the resulting data in the output
data table o
.
for ii = 1:numRecords k.MiDate = TradeDataBackTest.Date(ii); k.MiCode = TradeDataBackTest.MICode(ii); o.MI(ii) = marketImpact(k,TradeDataBackTest(ii,:)); MIDollars = (TradeDataBackTest.Shares(ii) * TradeDataBackTest.Price(ii)) ... * o.MI(ii)/10000 * TradeDataBackTest.FXRate(ii); o.MIDollar(ii) = MIDollars; end
Display the first three rows of output data.
o(1:3,:)
ans = Symbol Side Date MI MIDollar ______ ____ __________ ____ ________ 'A' 1.00 '5/1/2015' 1.04 103.91 'B' 1.00 '5/1/2015' 3.09 3864.44 'C' 1.00 '5/1/2015' 8.54 5335.03
The output data contains these variables:
Stock symbol
Side
Historical trade date
Historical market-impact cost in basis points
Historical market-impact value in local dollars
References
[1] Kissell, Robert. “Creating Dynamic Pre-Trade Models: Beyond the Black Box.” Journal of Trading. Vol. 6, Number 4, Fall 2011, pp. 8–15.
[2] Kissell, Robert. “TCA in the Investment Process: An Overview.” Journal of Index Investing. Vol. 2, Number 1, Summer 2011, pp. 60–64.
[3] Kissell, Robert. The Science of Algorithmic Trading and Portfolio Management. Cambridge, MA: Elsevier/Academic Press, 2013.
[4] Chung, Grace and Robert Kissell. “An Application of Transaction Costs in the Portfolio Optimization Process.” Journal of Trading. Vol. 11, Number 2, Spring 2016, pp. 11–20.