Oct 27

Posted by abiao at 21:54 | Code » Matlab | Comments(0) | Reads(21506)

Anyway, it is your turn to compare them.  I write M files of several selected quantitative trading strategies from the book I have played, be aware although I wrote carefully, i dont gurantee the correct of them, and i didn't optimize the code, either, sorry.

Moving Average: long when x day moving average crosses above y day moving average, short when x day moving average crosses below y day moving average
pos=zeros(size(price,1),1);
lag = [zeros(y-1,1); lag];

Volatility Breakouts:
m = size(price,1);
pos=zeros(m,1);
for i = 2:m
%put here the way to calculate variance C
UpperTrigger = price(i-1)+multiplier*sqrt(C);
LowerTrigger = price(i-1)-multiplier*sqrt(C);
if price(i)>=UpperTrigger pos(i) = 1;
elseif price(i)<=LowerTrigger pos(i) = -1;
end
end

stochastic indicator:
stosc = stochosc(highp, lowp, closep, kperiod, dperiod); %embedded Matlab function
m = size(highp,1);
pos=zeros(m,1);
inx1 = find(stosc(:,1)>=30);
inx2 = find(stosc(:,1)>=stosc(:,2));
pos(intersect(inx1,inx2)) = 1;
inx1 = find(stosc(:,1)<=80);
inx2 = find(stosc(:,1)<=stosc(:,2));
pos(intersect(inx1,inx2)) = -1;

Divergence Index:
%divergence index strategy, m is long momentum period, n is for short
longmom = tsmom(price,m);
shortmom = tsmom(price,n);
mm = size(price,1);
pos=zeros(mm,1);
DI = longmom.*shortmom./var(diff(price));
inx1 = find(DI<-8);
inx2 = find(longmom<0);
inx3 = find(longmom>0);
pos(intersect(inx1,inx2))=-1;
pos(intersect(inx1,inx3))=1;

Moving Average Confluence Method:
% p - *price* data
% N - number of points to generate signal
pos=zeros(size(p,1),1);
macs = zeros(size(p,1),20);
for i=1:20
j=i*4;