clear all; close all;

%part a)
%plot all the trades
tradeFolder='Trades/DATA/';
quoteFolder='Quotes/DATA/';
daten=05;

files = dir(strcat(tradeFolder,'679.asc'));
for i = 1:size(files,1);
    f = files(i);
    name = strcat(tradeFolder, f.name );
    fid = fopen(name);
    C = textscan(fid, '%s %s %f %d  %*[^\n]', 'delimiter', ',', ...
             'EmptyValue', -Inf);
    fclose(fid);
    time=char(C{2});
    date=char(C{1});
    dateidx=(str2num(date(:,4:5))==daten); %#ok<*ST2NM>

    hour=str2num(time(dateidx,1:2));
    min=str2num(time(dateidx,4:5));
    sec=str2num(time(dateidx,7:end));
    tsecs=hour*60*60+min*60+sec;

    price=C{3}(dateidx);

    name = strcat(quoteFolder, f.name );
    fid = fopen(name);
    C = textscan(fid, '%s %s %s %f %f %d %d  %*[^\n]',...
       'delimiter', ',','EmptyValue', -Inf);
    fclose(fid);

    date=char(C{1});
    dateidx=(str2num(date(:,4:5))==daten);

    time=char(C{2});
    exch=char(C{3});
    idx=(exch=='N') & dateidx;

    hour=str2num(time(idx,1:2));
    min=str2num(time(idx,4:5));
    sec=str2num(time(idx,7:end));
    qsecs=hour*60*60+min*60+sec;
    bid=C{4}(idx);
    ask=C{5}(idx);

    figure;
    starttime=10*60*60; %10am
    pidx=(tsecs>starttime) & (tsecs<(starttime+5*60*60)); %plus 5h
    qidx=(qsecs>starttime) & (qsecs<(starttime+5*60*60)); %plus 5h

    plot(tsecs(pidx),price(pidx),'b',qsecs(qidx),bid(qidx),'r',...
	qsecs(qidx),ask(qidx),'g');
    hold on;
    legend('tradeprice','bid','ask')
end

load_file_removebad;