Code covered by the BSD License  

Highlights from
Previous Working Day

Previous Working Day

by

 

Retrieves the previous working day for a given date and holiday schedule.

previousWorkday( hDate,holidaySchedule )
function prevWday = previousWorkday( hDate,holidaySchedule )
%Function calculates the working day (trading day) prior to the input date
%   input is in any format
%   date must be within the holidaySchedule or function returns an error

switch nargin
    case 1
    %need to get the holiday schedule
    fid=fopen('NYSEHolidays.csv','r');
    raw=textscan(fid,'%s','delimiter',',','HeaderLines',0);
    fclose(fid);
    holidaySchedule=raw{1};
    case 2
        
end

% first check to make sure we are within the holiday schedule
hDate=datenum(hDate);
holidaySchedule=datenum(holidaySchedule);

if ~between(hDate,holidaySchedule(1),holidaySchedule(end))
    disp('Please Extend Holiday Schedule Prior to Proceeding')
    return
end

priorDay=hDate-1;
if(ismember(priorDay,holidaySchedule))
    priorDay=hDate-2;
else priorDay=hDate-1;
end

if weekday(priorDay)==1 %Sunday
    prevWday=priorDay-2; %Look to Friday
elseif weekday(priorDay)==7 %Saturday
    prevWday=priorDay-1; %Look to Friday
else prevWday=priorDay;
end


if(ismember(prevWday,holidaySchedule))
    prevWday=prevWday-1;
end
                    
            
        
end

Contact us