install.packages("RQuantLib") library(RQuantLib) # ?DiscountCurve # function (params, tsQuotes, times = seq(0, 10, 0.1)) # "params" # A list specifying the tradeDate (month/day/year), settleDate, forward rate time span dt, and two curve construction options: interpWhat (with possible values discount, forward, and zero) and interpHow (with possible values linear, loglinear, and spline). spline here means cubic spline interpolation of the interpWhat value. params <- list(tradeDate = as.Date("2004-09-20"), # trade date settleDate = as.Date("2004-09-22"), # settlement date dt = .25, # fwd rate time span interpWhat = "discount", # oppure "forward" o "zero" interpHow = "loglinear") # oppure "linear" o "spline" setEvaluationDate(as.Date("2004-09-20")) # "tsQuotes" # Market quotes used to construct the spot term structure of interest rates. Must be a list of name/value pairs, where the currently recognized names are: # flat rate for a flat yield curve # d1w 1-week deposit rate # d1m 1-month deposit rate # d3m 3-month deposit rate # d6m 6-month deposit rate # d9m 9-month deposit rate # d1y 1-year deposit rate # s2y 2-year swap rate # s3y 3-year swap rate # s5y 5-year swap rate # s10y 10-year swap rate # s15y 15-year swap rate # s20y 20-year swap rate # s30y 30-year swap rate # fut1--fut8 3-month futures contracts # fra3x6 3x6 FRA # fra6x9 6x9 FRA # fra6x12 6x12 FRA # Here rates are expected as fractions (so 5% means .05). If flat is specified it must be the first and only item in the list. The eight futures correspond to the first eight IMM dates. The maturity dates of the instruments specified need not be ordered, but they must be distinct. # Forward rates and zero rates are computed assuming continuous compounding, so the forward rate f over the period from t1 to t2 is determined by the relation # # d1/d2 = exp(f(t2 - t1)), # # where d1 and d2 are discount factors corresponding to the two times. In the case of the zero rate t1 is the current time (the spot date). # # Curve construction can be a delicate problem and the algorithms may fail for some input data sets and/or some combinations of the values for interpWhat and interpHow. Fortunately, the C++ exception mechanism seems to work well with the R interface, and QuantLib exceptions are propagated back to the R user, usually with a message that indicates what went wrong. (The first part of the message contains technical information about the precise location of the problem in the QuantLib code. Scroll to the end to find information that is meaningful to the R user.) ## We get numerical issue for the spline interpolation if we add ## any on of these three extra futures -- the original example ## creates different curves based on different deposit, fra, futures ## and swap data ## Removing s2y helps, as kindly pointed out by Luigi Ballabio tsQuotes <- list(d1w = 0.0382, # deposit rates (eg EURIBOR) d1m = 0.0372, d3m = 0.0363, d6m = 0.0353, d9m = 0.0348, d1y = 0.0345, fut1 = 96.2875, # 3-months interest rates fut2 = 96.7875, # futures contracts fut3 = 96.9875, fut4 = 96.6875, fut5 = 96.4875, fut6 = 96.3875, fut7 = 96.2875, fut8 =96.0875, # s2y = 0.037125, # swap rates s3y = 0.0398, s5y = 0.0443, s10y = 0.05165, s15y = 0.055175) # "times" # A vector of times at which to return the discount factors, forward rates, and zero rates. Times must be specified such that the largest time plus dt does not exceed the longest maturity of the instruments used for calibration (no extrapolation). times <- seq(0, 10, .1) # Loglinear interpolation of discount factors curves <- DiscountCurve(params, tsQuotes, times) plot(curves) # Linear interpolation of discount factors params$interpHow = "linear" curves <- DiscountCurve(params, tsQuotes, times) plot(curves) # Spline interpolation of discount factors params$interpHow = "spline" curves <- DiscountCurve(params, tsQuotes, times) plot(curves) ################################################### # Reference for parameters when constructing a bond ?Enum # DayCounter # 0 Actual360 # 1 Actual360FixEd # 2 ActualActual # 3 ActualBusiness252 # 4 OneDayCounter # 5 SimpleDayCounter # 6 Thirty360 # 7 Actual365NoLeap # 8 ActualActual.ISMA # 9 ActualActual.Bond # 10 ActualActual.ISDA # 11 ActualActual.Historical # 12 ActualActual.AFB # anything else ActualActual.Euro # businessDayConvention # 0 Following # 1 ModifiedFollowing # 2 Preceding # 3 ModifiedPreceding # anything else UNadjusted # compounding # 0 Simple # 1 Compounded # 2 Continuous # 3 SimpleThenCompounded # period or frequency # -1 NoFrequency # 0 Once # 1 Annual # 2 Semiannual # 3 EveryFourthMonth # 4 Quarterly # 6 BiMonthtly # 12 Monthly # 13 EveryFourthWeek # 26 BiWeekly # 52 Weekly # 365 Daily # anything else OtherFrequency # date generation # specify date generation rule # 0 Backward # 1 Forward # 2 Zero # 3 ThirdWednesday # 4 Twentieth # anything else TwentiethIMM # durationType # specify duration type # 0 Simple # 1 Macaulay # 2 Modified