[R-sig-finance] nearby

davidr at rhotrading.com davidr at rhotrading.com
Wed Feb 16 19:29:12 CET 2005

Even if this were available, I wouldn't use it.
This method will give you jumps when the contracts roll
(for almost all products.)
I would roll my own, blending from the old to the new over some period to 
smooth out the jumps. This results in more realistic volatilities and 
At least, that's what I've been doing for 20 years.

David Reiner

-----Original Message-----
From: Omar Lakkis [mailto:uofiowa at gmail.com] 
Sent: Wednesday, February 16, 2005 9:06 AM
To: r-sig-finance at stat.math.ethz.ch
Subject: [R-sig-finance] nearby

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=""))

R-sig-finance at stat.math.ethz.ch mailing list

More information about the R-sig-finance mailing list