New column
Show older comments
Hello,
I work with the below part of a txt file (since the original is huge one):
Stock Date Time Price Volume Stock Category >ETE 04/01/2010 10145959 18.31 500 Big Cap >ETE 04/01/2010 10150000 18.01 70 Big Cap >ETE 04/01/2010 10170000 18.54 430 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 10250000 18.31 40 Big Cap >ETE 04/01/2010 10295959 18.74 215 Big Cap >ETE 04/01/2010 10300000 18.74 500 Big Cap >ETE 04/01/2010 10320000 18.34 500 Big Cap
% I need to create a new variable (column six, let's say 'TRADE'. It's first value will be arbitrarilly asigned to 'BUY' (is there any code to do that?). Then I need its value to be 'BUY' if the row's value of Price(column 4) is higher than the previous row's value of Price. If price is lower, then it would be 'SELL'. In case of equal prices then if value in new column 7 (Trade) of previous row is 'BUY', then it will be 'BUY', otherwise 'SELL' (that's the why I arbitrarilly define the first value),
so the sample will looking like:
Stock Date Time Price Volume Stock Category Trade >ETE 04/01/2010 10145959 18.31 500 Big Cap BUY >ETE 04/01/2010 10150000 18.01 70 Big Cap SELL >ETE 04/01/2010 10170000 18.54 430 Big Cap BUY >ABC 04/01/2010 10190000 18.34 200 Big Cap SELL >YYY 04/01/2010 10200000 18.34 100 Big Cap SELL >ETE 04/01/2010 10250000 18.31 40 Big Cap SELL >ETE 04/01/2010 10295959 18.74 215 Big Cap BUY >ETE 04/01/2010 10300000 18.74 500 Big Cap BUY >ETE 04/01/2010 10320000 18.34 500 Big Cap SELL
Any help?
Thanks in advance,
Panos
Accepted Answer
More Answers (1)
Matt Tearle
on 2 Apr 2011
% Read stock data from file
fid = fopen('stocks.txt');
data = textscan(fid,'%s%s%f%f%f%[^\n]','delimiter',' ','headerlines',1);
% Read as text (for later writing)
frewind(fid);
txt = textscan(fid,'%s','delimiter','\n');
fclose(fid);
% Get prices from imported data
Price = data{4};
% Determine which stocks to buy
buy = [true;diff(Price)>=0];
idx = find(~diff(Price));
buy(idx+1) = buy(idx);
% Make string of trade decision
buysell = cellstr(repmat(' Sell',size(Price)));
buysell(buy) = {' Buy'};
% Open file for writing
fid = fopen('stocks2.txt','wt');
% Make output string by appending trade decision
outstr = strcat(txt{1},[' Trade';buysell]);
% Write out
fprintf(fid,'%s\n',outstr{:});
fclose(fid);
4 Comments
Pap
on 2 Apr 2011
Matt Tearle
on 3 Apr 2011
It could be something like a file/directory permissions problem. Have a look at the value of fid -- I'm guessing it's -1, which indicates a problem in opening the file. In that case, do
[fid, message] = fopen(...)
and see what "message" is.
Pap
on 5 Apr 2011
Matt Tearle
on 6 Apr 2011
I've added an answer to your new question about this, but as an aside here: data should be a 1-by-6 cell array. Each cell should contain an n-by-1 array of appropriate type (cell or double).
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!