[R-SIG-Finance] Help with date arithmetic

Joe W. Byers Joe-Byers at utulsa.edu
Mon Oct 23 01:35:40 CEST 2006


Not off base.  I do have a calendar that is only business days created 
to set up a simulation.

I am working on a simulation of Natural gas prices that has spot, 
balance of the month, and forward prices for each business day.  The 
problem I am addressing is that NG spot and balmo trade on a business 
day, but the commodity transfers title on the flow days, or one day 
after the trade date for the spot.  The balmo contract is interesting 
because it starts flowing on the next day after spot gas flows. 
Business days are easy, spot flows one business day after the 
transaction, and balmo flows 2 business days after the transaction.  But 
on the last business day of a week, spot gas is traded for the next 
three days, two non business and one business day (Sat-Mon).  So, balmo 
does not start flowing till the following Tues or four days.  If there 
is a holiday anywhere in here, it has to be handled to get balmo 
correct.  Because spot and balmo have different flow start dates each 
trading day, I have to calculate them instead of using the specified 
contract month as with the NYMEX futures contracts.  The flow date for 
spot and balmo is basically the same as a futures contract month here. 
Another thing to remember with NYMEX NG 1 contract for next Jan is 
10000MMBTU and it flow or in delivered in 1/31 of a contract increments 
starting on Jan 1st.

Equities, fixed income, and others are easy.  Transaction days, 
expiration days, settlement days all line up nice and neatly for 
analysis.  Commodities not so, and carry charges can be significant 
sometimes.  Commodities violate our wonderful Black-scholes world we 
assume exists.

I maybe can use the calendar that I have created and select the first 
business day greater than the trade date I have for the spot and also 
the second for the balmo.  Still a loop, but I may be able to prebuild a 
matrix for these contracts easier than the function I have.

So you are not off base.  You gave me another idea to try.

Thank you very much

Leeds, Mark (IED) wrote:
 > Joe : I'm just jumping in so excuse me if I'm totally off base. It seems
 > like youa re writing code to jump form one date to the next business
 > day.
 > I'm sure youa re clever and close tog etting it working but whren I do (
 > or did ) in the past to dela with this problem
 > Is just have a series out there that represents only business days. For
 > example, if you pull ibm's data from
 > Yahoo or wherever, it is only
 > Going to come back with business dzy dates.
 > If you need to do the same thing for japan, pull "toyota" from somewhere
 > ( maybe yahoo also ? ) and it will only come back with
 > business day dates for japan.  Anyway, this was always my easy solution
 > but you may have some reason why you can't do it hat way.
 > Your code is way beyond my level of knowledge of R so I can't help you
 > with it.
 > -----Original Message-----
 > From: r-sig-finance-bounces at stat.math.ethz.ch
 > [mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Joe W.
 > Byers
 > Sent: Saturday, October 21, 2006 3:31 PM
 > To: r-sig-finance at stat.math.ethz.ch
 > Subject: Re: [R-SIG-Finance] Help with date arithmetic
 > 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
 > 	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
 > _______________________________________________
 > R-SIG-Finance at stat.math.ethz.ch mailing list
 > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
 > --------------------------------------------------------
 > This is not an offer (or solicitation of an offer) to buy/sell the 
securities/instruments mentioned or an official confirmation.  Morgan 
Stanley may deal as principal in or own or act as market maker for 
securities/instruments mentioned or may advise the issuers.  This is not 
research and is not from MS Research but it may refer to a research 
analyst/research report.  Unless indicated, these views are the author's 
and may differ from those of Morgan Stanley research or others in the 
Firm.  We do not represent this is accurate or complete and we may not 
update this.  Past performance is not indicative of future returns.  For 
additional information, research reports and important disclosures, 
contact me or see https://secure.ms.com/servlet/cls.  You should not use 
e-mail to request, authorize or effect the purchase or sale of any 
security or instrument, to send transfer instructions, or to effect any 
other transactions.  We cannot guarantee that any such requests received 
via e-mail will be processed in a timely manner.  This communication is 
solely for the addressee(s) and may contain confidential information. 
We do not waive confidentiality by mistransmission.  Contact me if you 
do not wish to receive these communications.  In the UK, this 
communication is directed in the UK to those persons who are market 
counterparties or intermediate customers (as defined in the UK Financial 
Services Authority's rules).

More information about the R-SIG-Finance mailing list