[R-sig-finance] nearby

Omar Lakkis uofiowa at gmail.com
Wed Feb 16 16:06:14 CET 2005

In Splus there is a function called "nearby" (description below). Is
there an equivalent utility in R?

Futures Nearby Creation Function 

Create a nearby series from a multivariate time series representing
multiple futures  contracts.

nearby(x, rule, contracts)  

<b>x </b>
multivariate time series containing futures prices for the contracts. 
rule giving the roll date (last date to use each contract) from one
futures contract  to the next, relative to the beginning of the
contract month. This can be given as a  time span or relative time
object, or a character string which can be coerced into a  relative
time object.
the contract months for the columns of the time series, given as
character strings  of the form "F95", or "F1995", where the first
letter is the month code and the  remainder is the year, and where
two−digit years are converted to four−digit years  using
options("time.century"). These should be in ascending order.

a single−column time series containing the specified nearby series. 

The series is generated with the following steps. First, the contract
months are  converted to dates of the first of the month, e.g. Z98
becomes December 1, 1998. Then  the roll rule is added to each date,
to calculate the last day each column of the data set  should be used.
The time series positions and the end dates are then passed to the cut
 function to determine which column is needed for each output date,
and these data  values are subscripted from x and returned. NA will be
the result for positions that have  no valid column data.

timeRelative, timeSeries 

# make a 5−column time series  
x <− timeSeries(data=matrix(1:750, ncol=5),      
         pos=timeDate("1/1/1995", format="%a %02m/%02d/%Y")+1:150)  
# make a nearby series, assuming this are Feb−June 1995 contracts (ignori 
# the fact that there should be NAs in the matrix), rolling on the 3rd  
# Friday of the month before the contract month  
nearby(x, timeRelative("−1mth −1day +3fri"), 
         paste(c("G", "H", "J", "K", "M"), 1995, sep=""))

More information about the R-sig-finance mailing list