Joshua,

 

Thanks.

 

This is the first time I have ever worked with zoo or xts (or dates).

 

Here is the problem.

 

I am given data in an Excel spreadsheet. 

 

I am given quote dates, say

 

1/9/2010

1/10/2010

1/11/2010

1/12/2010

1/13/2010

1/14/2010

1/15/2010

1/16/2010

1/17/2010

 

I am given the vector of expiration dates of forward contracts, say

 

2/1/2010          3/1/2010          4/1/2010          5/1/2010

 

 

And the prices on those dates

 

4.38     4.38     4.54     4.50

4.84     4.46     4.22     4.60

4.92     4.12     4.43     4.73

4.95     4.47     4.72     4.14

4.25     4.85     4.99     4.78

4.39     4.93     4.87     4.89

4.35     4.97     4.90     4.63

4.32     4.86     4.74     4.62

4.99     4.37     4.04     4.27

 

I then have to price a CMF.  For example for the quote date of 1/17/2010 I add one month and get 2/17/2010. Then I compare that to the vector of expiration dates. I choose the closest one (3/1/2010). I use the price that is associated with that date. 

 

For 1/17/2010, I also have to add 2 months and also add three months and so forth.

 

In Excel it was possible to use the EDATE function. I need to come up with an R solution.

 

The idea is to use the following two tables to produce the answer. I have only included a small segment of the data.

 

         
      Forward Prices
        
      quote date
     2/1/2010
     3/1/2010
     4/1/2010
     5/1/2010
     
      1/9/2010
     4.38
     4.38
     4.54
     4.50
     
      1/10/2010
     4.84
     4.46
     4.22
     4.60
     
      1/11/2010
     4.92
     4.12
     4.43
     4.73
     
      1/12/2010
     4.95
     4.47
     4.72
     4.14
     
      1/13/2010
     4.25
     4.85
     4.99
     4.78
     
      1/14/2010
     4.39
     4.93
     4.87
     4.89
     
      1/15/2010
     4.35
     4.97
     4.90
     4.63
     
      1/16/2010
     4.32
     4.86
     4.74
     4.62
     
      1/17/2010
     4.99
     4.37
     4.04
     4.27
     
         
         
      offset date
     1
     2
     3
     4
     
      1/9/2010
     2/9/2010
     3/9/2010
     4/9/2010
     5/9/2010
     
      1/10/2010
     2/10/2010
     3/10/2010
     4/10/2010
     5/10/2010
     
      1/11/2010
     2/11/2010
     3/11/2010
     4/11/2010
     5/11/2010
     
      1/12/2010
     2/12/2010
     3/12/2010
     4/12/2010
     5/12/2010
     
      1/13/2010
     2/13/2010
     3/13/2010
     4/13/2010
     5/13/2010
     
      1/14/2010
     2/14/2010
     3/14/2010
     4/14/2010
     5/14/2010
     
      1/15/2010
     2/15/2010
     3/15/2010
     4/15/2010
     5/15/2010
     
      1/16/2010
     2/16/2010
     3/16/2010
     4/16/2010
     5/16/2010
     
      1/17/2010
     2/17/2010
     3/17/2010
     4/17/2010
     5/17/2010
     

 

 

A user will determine the window of quote dates. The user will also determine how many CMFs are needed. Here is the function call.

 

computeCMF = function(zForwardPrices, startQuoteDateRequested , 

         endQuoteDateRequested, numberOfCMF)

 

 

Thanks!

 

Ira

 

PS I had noticed the following problem, but it was not my main concern. And I am actually doing this for someone else. Is there a preferred convention of dealing with this issue? Note that contract expirations are not necessarily the first of the month.

 

Are you sure this is fine?  Are you comfortable with the assumptions
being made?  For example:
mDate <- timeBasedSeq("201101/20110203")
tail(mDate + months(1))
# [1] "2011-03-01" "2011-03-02" "2011-03-03" "2011-03-01"
# [5] "2011-03-02" "2011-03-03"

  ----- Original Message ----- 
  From: Joshua Ulrich 
  To: Ira Sharenow 
  Cc: r-sig-finance@r-project.org 
  Sent: Friday, June 24, 2011 7:03 PM
  Subject: Re: [R-SIG-Finance] Offset a vector by 1 to k months


  On Tue, Jun 21, 2011 at 12:03 AM, Ira Sharenow <irasharenow100@yahoo.com> wrote:
  > I am new to financial time series, zoo, and xts.
  >
  > I have a length n vector (vDates) of dates. I have an n by k matrix (mDates). In row i and column j of the matrix, I would like the value to be the row i value of the vector + j months.
  >
  Adding a certain number of months isn't necessarily that easy.  You
  need to choose a calendar convention.  For example, see
  RQuantLib::advance().

  > mDates[i,j] = vDates[i] + j months.
  >
  > I would like to do this without loops.
  >
  > I solved the problem with just numbers.
  >
  > v1 = 11:15
  > m1 = matrix(rep(0,20), nrow = 5)
  > m1 = v1 + col(m1)
  >
  > I do not know how to add months in zoo or xts, so I tried lubridate.
  >
  > library(lubridate)
  > vDate = as.Date(c("2011-06-01", "2011-06-02", "2011-06-03"), "%Y-%m-%d")
  > mDate2 = vDate + months(1) this works fine
  >
  Are you sure this is fine?  Are you comfortable with the assumptions
  being made?  For example:
  mDate <- timeBasedSeq("201101/20110203")
  tail(mDate + months(1))
  # [1] "2011-03-01" "2011-03-02" "2011-03-03" "2011-03-01"
  # [5] "2011-03-02" "2011-03-03"

  > But I do not know how to solve the problem for a matrix.
  >
  > library(xts)
  > charvec = paste("2011-0", 1:2, "-01", sep = "")
  > theData = matrix(rep(0,6),nrow = 2)
  > z1 = xts(theData, as.Date(charvec))
  >
  > I want the rows of z1 data to be
  > 2011-02-01 2011-03-01 2011-04-01
  > 2011-03-01 2011-04-01 2011-05-01
  >
  You can add a month to each index value via:
  ISOdate(year(index(z1)),month(index(z1))+1,day(index(z1)))
  # [1] "2011-02-01 12:00:00 GMT" "2011-03-01 12:00:00 GMT"

  But you can't have Date class objects inside xts/zoo objects.  xts/zoo
  objects are just matrix objects with an index attribute and matrix
  objects can only contain atomic types (or list or expression).

  Perhaps if you provide a bit more detail about your actual problem,
  someone can show you a more "R-like" solution.

  > Thanks.
  > Ira
  >

  Hope that helps,
  --
  Joshua Ulrich  |  FOSS Trading: www.fosstrading.com
	[[alternative HTML version deleted]]

