[R-SIG-Finance] Help with date arithmetic

Joe W. Byers Joe-Byers at utulsa.edu
Sat Oct 21 21:30:43 CEST 2006


All here is what I came up with to increment over non-business days and 
NYSE holidays.  I would appreciate any help in refining this code to be 
more generic for use with vectors, more date formats, and other 
holidays.  It is extremely slow to run this function over a large time 
sequence.  I think I could change the code from using fCalendar 
timesequence() to using the date.mdy()$year for part of the code, but I 
am still learning these things in R.

#function call
Bizday.increment('09/25/04',1)

# Created by: Joe W. Byers
# Date: 10/20/06
#Method to increment a give date to the next business day given 
holidayNYSE() holidays
Bizday.increment<-function(data,inc=1,format='%m/%d/%Y'){
	#need to add date coersion if data numeric
	# need to add ability to perform over a vector and return all increments
	# need to add ability to perform with different holidays beside NYSE
	Loop=T; # set loop test
	ans<-format.Date(as.date(data,order='mdy')+inc,format)
	while(Loop==T){
		t1<-isBizday(timeSequence(from=ans,to=ans,format=format),
			holidays=holidayNYSE(as.numeric(
			factor(atoms(timeSequence(from=ans,to=ans,format=format)
			)$Y)@levels)))
		if (t1==T) {Loop=F}
		else {ans<-format.Date(as.date(ans,order='mdy')+inc,format)}
	}
	ans
}
Thank you
Joe

Joe W. Byers wrote:
> I have the following day
> dts<-('02/08/2002');
> I want to return a vector of character dates by adding 1 to the current 
> day and adding the number of non business days for a weekend and/or 
> holiday if they occur to get the following results
> 
> should.be.results<-('02/09/2002','02/12/2002');
> 
> I can handle the first calculation but I can not find how to increment 
> the date for non business days.
> 
> results<-c(format.Date(as.date(dts,order='mdy')+1,'%m/%d/%Y'),
> 	I need help  here);
> 
> I have review the fCalendar docs and others.  I have also created a time 
> sequence for this project where I removed the non-business days using 
> the isBizday method.  Any help is greatly appreciated.
> Thank you
> Joe
>



More information about the R-SIG-Finance mailing list