from TSM-simulation by Anders
A strategy simulation of time-series price momentum.

TSM-simulation.m
clear, clc

load franceL.txt % load the data matrix from the file


g = franceL(:,2)/100;
w = log(1 + g); % convert to log returns
numElementsToSum = 10; %Dictates the number of months used in the model
mSum = conv(w, ones(numElementsToSum, 1), 'valid'); %Gives the sum of last n months
movsum = mSum(1:end-1); %Needed to delete the last sum since it is out of sample
returns = g(numElementsToSum+1:end); %deletes the first n months which is the initial evaluation period
StdMkt=std(g)
ExretMkt=mean(g)

riskfree = 0.02; % annual risk-free rate of return
Rf = ((1+riskfree)^(1/12)-1)*ones(length(returns),1);% create the vector of monthly Rf
Rf2 = ((1+riskfree)^(1/12)-1)*ones(length(g),1);


% compute the returns to the market timing strategy
StratSimulation=returns;
idx=find(movsum<0);
StratSimulation(idx)=Rf(idx);
StdStratSim=std(StratSimulation)
ExretStratsim=mean(StratSimulation)

% compute excess returns
ExRetPassive = g - Rf2;
ExRetActive = StratSimulation - Rf;
ExRetPassivetwo = ExRetPassive(numElementsToSum+1:end)

% OK with monthly Sharpe ratios
SrPassive = mean(ExRetPassive)/std(ExRetPassive)
SrActive = mean(ExRetActive)/std(ExRetActive)

p = polyfit (returns,StratSimulation,1)
yfit = polyval(p,returns)
yresid = StratSimulation - yfit
SSresid = sum(yresid.^2)
SStotal = (length(StratSimulation)-1)*var(StratSimulation)
rsq = 1 - (SSresid/SStotal)
res=[ExretStratsim;StdStratSim;SrActive;ExretMkt;StdMkt;SrPassive]
ols=[ExRetActive;ExRetPassive]

Contact us