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

files = dir(strcat(tradeFolder,'*.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);

    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');
    legend('tradeprice','bid','ask')
end





%part c)
n = 1000;
d=3;
X = randn(n,d);
randnoise = randn(n,1);
Y = ((X(:,1)+X(:,2)+randnoise)>0)*2-1;
%training set is first 30
n=30;
Yt=Y(1:n,1); %pick the X and Y for the particular SVM
Xt=X(1:n,:);
C= norm(mean(abs(Xt))); %better estimate for C, or you can just use 10
cvx_begin %classical svm
    variables w(d) e(n) b
    dual variable alpha
    minimize( 0.5*w'*w + C*sum(e)) %norm(w) takes an extra sqrt
    subject to
        Yt.*(Xt*w+b)-1 +e >0   :alpha;
        e>0; %slack
cvx_end

