from Weak form market efficiency tests by Anis Ben Hassen
market efficiency tests

[Ntrading,nb,ns,yb,ys,yb_s,B,S,BS]=rob_VMA(x,s,l,band,tax,time_lag)
function [Ntrading,nb,ns,yb,ys,yb_s,B,S,BS]=rob_VMA(x,s,l,band,tax,time_lag)
% -----------------------------------------------------------------
% Elaborated by : BEN HASSEN Anis 
% "Institut Suprieur de Gestion de Tunis" (ISG Tunis)
% University of Tunis
% 41, rue de la Libert - Cit Bouchoucha - C.P. : 2000 Le Bardo
% Tunisia
% University e-mail: http://www.isg.rnu.tn/
% Personal e-mail: benhassenanis@yahoo.com
% _________________________________________________________________
% January 14, 2004.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Output:
%   1: number of trading days
%   2: number of buy signals
%   3: number of sell signals
%   4: average return on buy signals
%   5: average return on sell signals
%   6: average buy-sell excess return (spread) relative to a buy-and-hold
%      strategy and net of transaction costs
%   7: t-statistic of the difference between buys & buy-and-hold returns
%   8: t-statistic of the difference between sells & buy-and-hold returns
%   9: t-statistic of the difference between buy-sell & buy-and-hold
%      returns
%******************************************************************
% Input:
% x: a column vector of time series (prices Pt)
% s: short moving average (example 5 days)
% l: long moving average (example 100 days)
% band: band used in the strategy (example 0.01)
% tax: transaction cost on each transaction (in percentage)
% time_lag: introduced if a delay between decision and action is present.
% For example, 1 means that there in no time lag and 2 means that we skeep
% one trading day before we make a transaction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
r=diff(log(x)); % asset returns
T=length(r);
[short,long]=movavg(x,s,l);
short=short(end+l-T-1:end);
long=long(end+l-T-1:end);
aux=x(end+l-T-1:end);
Ib=(short-long)>(band*aux); % generate buy signals if short VMA is above long VMA and a band
Is=(short-long)<(-band*aux); % generate sell signals if short VMA is beyond long VMA and a band
t=length(Ib);
for i=1:t-1,
    if Ib(i+1)+Is(i+1)==0 & Ib(i)+Is(i)~=0
        Ib(i+1)=Ib(i);
        Is(i+1)=Is(i);
    end
end
for i=1:t,
    if Ib(i)==1
        I(i)=1;
    else
        I(i)=-1;
    end
end
TC=[1;abs(diff(I))'];
Ib=Ib(1:end-time_lag);
Is=Is(1:end-time_lag);
TC=TC(1:end-time_lag);
nb=sum(Ib);
ns=sum(Is);
Ntrading=sum(TC);
%Lb=mean(averday(Ib));
%Ls=mean(averday(Is));
%stdLb=std(averday(Ib));
%stdLs=std(averday(Is));
n=length(Ib);
rt=r(end-n+1:end);
y=mean(rt);
v=var(rt);
Rbuy=(rt-TC*tax).*Ib;
Rsell=(rt+TC*tax).*Is;
yb=1/nb*sum(Rbuy);
ys=1/ns*sum(Rsell);
yb_s=yb-ys;
vb=1/nb*sum(Ib.*(Rbuy-yb).^2);
vs=1/ns*sum(Is.*(Rsell-ys).^2);
B=(yb-y)/sqrt(v/n+vb/nb);
S=(ys-y)/sqrt(v/n+vs/ns);
BS=(yb-ys)/sqrt(vb/nb+vs/ns);

Contact us at files@mathworks.com