Created
November 3, 2011 16:37
-
-
Save KirkWylie/1336987 to your computer and use it in GitHub Desktop.
Yield Curve Plotting Using OpenGamma and R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies | |
# | |
# Please see distribution for license. | |
## | |
# Loads the time-series for yield curve data points to construct a 3D "curve over time" graph. | |
# Curve tickers | |
tickers <- c ("US00O/N Index", "US0001W Index", "US0002W Index", "US0001M Index", "US0002M Index", "US0003M Index", "USSW2 Curncy", "USSW3 Curncy", "USSW4 Curncy", "USSW5 Curncy", "USSW6 Curncy", "USSW7 Curncy", "USSW8 Curncy", "USSW9 Curncy", "USSW10 Curncy", "USSW15 Curncy", "USSW20 Curncy", "USSW25 Curncy", "USSW30 Curncy") | |
# Fetch timeseries | |
timeseries <- lapply (tickers, function (x) { FetchTimeSeries (dataField = "PX_LAST", identifier = paste ("BLOOMBERG_TICKER", x, sep = "~")) }) | |
# Extend start of shorter timeseries so curve starts in same place | |
timeseries.start <- min (sapply (timeseries, function (x) { start (x)[1] })) | |
timeseries <- lapply (timeseries, function (x) { | |
s <- start (x)[1] | |
if (s > timeseries.start) { | |
c (rep (NA, times = s - timeseries.start), as.double (x)) | |
} else { | |
as.double (x) | |
} | |
}) | |
timeseries.length <- max (sapply (timeseries, length)) | |
# Convert to a matrix - time is in rows, each column is a time-series (padded so they are correct length) | |
timeseries <- sapply (timeseries, function (x) { | |
l <- length (x) | |
if (l < timeseries.length) { | |
c (x, rep (NA, times = timeseries.length - l)) | |
} else { | |
tail (x, timeseries.length) | |
} | |
}) | |
# Convert to XTS timeseries | |
timeseries.dates <- as.Date (sapply (index (timeseries), function (x) { x + timeseries.start }), origin = "1970-01-01") | |
timeseries <- xts (timeseries, order.by = timeseries.dates) | |
colnames (timeseries) <- tickers | |
# The timeseries object can now be used with tools such as: | |
# http://www.quantmod.com/examples/chartSeries3d/ | |
chartSeries3d0 (timeseries) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment