MATLAB code to extract TP values and indicator data from an FTS


% moving prices to a matrix %
prc = fts2mat (TS);

% getting macd %
macdF = macd(TS);
macd_m = fts2mat (macdF);

% getting rsi %
rsiF = rsindex(TS);
rsi_m = fts2mat (rsiF);

% getting the MA's %
mov1F = tsmovavg(TS,'s',10);
mov2F = tsmovavg(TS,'s',20);
mov1_m = fts2mat (mov1F.CLOSE);
mov2_m = fts2mat (mov2F.CLOSE);

% MA's difference for crossing points %
mvdiff = mov1_m - mov2_m;

% getting the 1's and 0's %
[ro,co] = size(mvdiff);

pol(1)=0;

for i=2:ro
pol(i)=hardlim(mvdiff(i)/mvdiff(i-1)*-1);
end

pol = pol';

% getting the optimal TP levels %
for j=1:ro
if pol(j)==0
TP(j)=0;
elseif pol(j)==1 && mov1_m(j)
k=j+1;
TP(j)=prc(j,3)-prc(k,4);
k=j+2;
while(pol(k-1)~=1 && k<=ro)
if ((prc(j,3)-prc(k,4))>TP(j))
TP(j)= prc(j,3)-prc(k,4);
end
k=k+1;
end
elseif pol(j)==1 && mov1_m(j)>mov2_m(j) % upcrossing %

k=j+1;
TP(j)=prc(k,5)-prc(j,3);
k=j+2;

while(pol(k-1)~=1 && k<=ro)
if((prc(k,5)-prc(j,3)>TP(j)))
TP(j)=prc(k,5)-prc(j,3);
end
k=k+1;
end
end
end

TP = TP';

% creating an unclean ready matrix %
unclean = [macd_m rsi_m TP];

% cleaning out zero rows %
j=1;
for i=1:ro
if (unclean(i,4)~=0)
clean(j,1)=unclean(i,1);
clean(j,2)=unclean(i,2);
clean(j,3)=unclean(i,3);
clean(j,4)=unclean(i,4);
j=j+1;
end
end

% getting inputs and outputs ready %
tinp = clean(:,1:3);
targ = clean(:,4);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s