dow = load('djia.txt');

YEAR = 1;
MONTH = 2;
DATE = 3;
OPEN = 4;
HIGH = 5;
LOW = 6;
CLOSE = 7;
VOLUME = 8;


bigDay = 1;                         % row#
bigGain = dow(1,CLOSE) - dow(1,OPEN);

for day = 2:length(dow)
    todayGain = dow(day,CLOSE) - dow(day,OPEN);
    if todayGain > bigGain
        bigDay = day;
        bigGain = todayGain;
    end   
end

% I have the correct answer
fprintf('The biggest one day gain was %.2f points on %d/%02d/%02d.\n', ...
    bigGain, dow(bigDay,1), dow(bigDay,2), dow(bigDay,3));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

badDay = 1;                         % row#
badLoss = dow(1,7) - dow(1,4);

for day = 2:length(dow)
    todayLoss = dow(day,7) - dow(day,4);
    if todayLoss < badLoss
        badDay = day;
        badLoss = todayLoss;
    end   
end

% I have the correct answer
fprintf('The worst one day loss was %.2f points on %d/%02d/%02d.\n', ...
    badLoss, dow(badDay,1), dow(badDay,2), dow(badDay,3));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

bigDay = 1;                         % row#
bigPct = dow(1,7)/dow(1,4);

for day = 2:length(dow)
    todayPct = dow(day,7)/dow(day,4);
    if todayPct > bigPct
        bigDay = day;
        bigPct = todayPct;
    end   
end

% I have the correct answer
fprintf('The biggest one day percentage gain was %.2f%% on %d/%02d/%02d.\n', ...
    100*(bigPct-1), dow(bigDay,1), dow(bigDay,2), dow(bigDay,3));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

gain = 0;       % actual total point gain
numGain = 0;    % number of days with a gain
numEven = 0;
loss = 0;       % cumulative point loss
numLoss = 0;

totalDiff = 0;

for day = 1:length(dow)
    diff = dow(day,CLOSE) - dow(day,OPEN);
    totalDiff = totalDiff + diff;
    if diff > 0                 % a daily gain
        gain = gain + diff;
        numGain = 1 + numGain;
    elseif diff == 0            % even
        numEven = numEven +1;
    else                        % must be a loss
        numLoss = numLoss + 1;
        loss = loss + diff;
    end
end

fprintf('Total number of gains: %d\n', numGain);
fprintf('   average point gain was %.2f\n', gain/numGain);
fprintf('Total number of washes: %d\n', numEven);
fprintf('Total number of losses: %d\n', numLoss);
fprintf('   average point loss was %.2f\n', loss/numLoss);
fprintf('Total diff is %.2f\n', totalDiff);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
