% Alessio Sacco % 2/18/22 % Finding monthly gain % gain per day : seen % gain per month : today YEAR = 1; MONTH = 2; DAY = 3; OPEN = 4; HIGH = 5; LOW = 6; CLOSE = 7; VOLUME = 8; % read the file data = load('djia.txt'); %% % % gain of a month % gainOfMonthI = (cloesOnLastDay - openOnFirstDay)/closeOnLastDay * 100; start = 1; finish = length(data); currentMonth = data( 1, MONTH ); %has a value between 1 and 12 startRowOfCurrentMonth = 1; for i=start:finish monthOfRowI = data( i, MONTH ); if monthOfRowI ~= currentMonth openOnFirstDay = data( startRowOfCurrentMonth, OPEN ); closeOnLastDay = data( i-1, CLOSE ); gainOfCurrentMonth = 100*( closeOnLastDay - openOnFirstDay )/ closeOnLastDay; fprintf('The month of %d/%d had a gain of %0.2f\n', ... data(i-1,MONTH), data(i-1, YEAR), gainOfCurrentMonth); %AFTER we use the gain, then we update currentMonth etc. %for computing the next month currentMonth = data( i, MONTH ); startRowOfCurrentMonth = i; end end