algorithm implementation

1 view (last 30 days)
Pap
Pap on 9 May 2011
Hello,
%I work the below sample txt file:
ETE 04/01/2010 10145959 18.31 500 Big Cap
ETE 04/01/2010 10150000 18.01 70 Big Cap
ABC 04/01/2010 10190000 18.34 200 Big Cap
YYY 04/01/2010 10200000 18.34 100 Big Cap
ETE 04/01/2010 10170000 18.54 430 Big Cap
%I want to create an seventh column (trade) to identify trade as buy (1) or sell(0). the column should take the price 1(0)when the current row's price is higher (lower) than the previous row's price and if its equal it should be equal to the previous row's value of trade so the file look like:
ETE 04/01/2010 10145959 18.31 500 Big Cap 1
ETE 04/01/2010 10150000 18.01 70 Big Cap 0
ABC 04/01/2010 10190000 18.34 200 Big Cap 1
YYY 04/01/2010 10200000 18.34 100 Big Cap 1
ETE 04/01/2010 10170000 18.54 430 Big Cap 1
% In order to do so I first sort rows alphabetically regarding value of row 1 and then I apply the below code:
[m,n]=size(stock);
guard=1;
A=[stock date time price volume market];
A=sortrows(A,1);
trade=[];
a=1;
guard=1;
while a<=m
trade(guard)=1;
stock_comp=A(guard,1);
price_comp=A(guard,4);
a=a+1;
while a<=length(A) && strcmp(A(a,1),stock_comp)==1
if str2num(char(A(a,4)))>str2num(char(price_comp))
trade(a)=1;
price_comp=A(a,4);
elseif str2num(char(A(a,4)))<str2num(char(price_comp))
trade(a)=0;
else
if trade(a-1)==1
trade(a)=1;
else
trade(a)=0;
end
end
a=a+1;
end
guard=a;
end
trade=trade';
counter=1;
while strcmp(A(1,6),A(counter,6))==1
counter=counter+1;
end
if strcmp(A(1,6),'0')==1
miden=A(1,6);
ena=A(counter,6);
else
miden=A(counter,6);
ena=A(1,6);
end
for a=1:length(trade)
if trade(a)==0
A(a,7)=miden;
else
A(a,7)=ena;
end
end
% However I note some discrepancies. Could someone advise on why?
Panos

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 9 May 2011
I don't think you should use the "sort" function.
I just made a simple example. The resulting matrix "d" or "d2" is what you want. One solution uses a for-loop. It's easy to understand. The other uses histc() to avoid the for-loop (Thanks to Walter Roberson). You can then concatenate it to your original matrix.
a=[18.31,18.01,18.34,18.34,18.34,18.14,18.14,18.14]';
b=diff(a);
c=nan(size(b));
c(b>0)=1;
c(b<0)=0;
InitialBuy=1;
d=[InitialBuy;c];
Row_D=size(d,1);
d2=d;
% use for-loop to process
for i=2:Row_D
if isnan(d(i)),d(i)=d(i-1);end
end
% or use histc() to process
e=find(isnan(d2));
f=find(~isnan(d2));
g=[f;Row_D+1];
[vals, Didx] = histc(e,g);
Index=g(Didx);
d2(e)=d2(Index);
% verify the two solutions are same
all(d==d2)
  2 Comments
Walter Roberson
Walter Roberson on 9 May 2011
Something like,
[vals, Didx] = histc(find(isnan(d)),find(~isnan(d)));
d(isnan(d)) = d(Didx);
Fangjun Jiang
Fangjun Jiang on 9 May 2011
Thanks, Walter! Indeed I can use histc() to avoid the for-loop.

Sign in to comment.

More Answers (0)

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!