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;elseif trade(a-1)==1 trade(a)=1;else trade(a)=0;endend a=a+1; end guard=a; endtrade=trade';counter=1;while strcmp(A(1,6),A(counter,6))==1 counter=counter+1;endif strcmp(A(1,6),'0')==1miden=A(1,6);ena=A(counter,6);elsemiden=A(counter,6);ena=A(1,6);endfor a=1:length(trade)if trade(a)==0A(a,7)=miden;else A(a,7)=ena;endend% However I note some discrepancies. Could someone advise on why?
Panos
Best Answer