algorithm implementation
显示 更早的评论
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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!