tried out some stuff…here’s my R code…shows NA for prices. It’s not able to take data from bloomberg…a one liner fix will help…thanks
Copyright © 2012 - present by OpenGamma Inc. and the OpenGamma group of companies
Please see distribution for license.
Demonstrates constructing a portfolio containing securities with different properties
(in this case FX Options with a range of strikes) to show how a single OpenGamma
engine cycle can then be used to calculate everything in parallel.
expiry <- Sys.time () + as.difftime (26, format = “%X”, units = “weeks”) # About six-months to expiry
option.strikes <- seq (1700, 2000, length.out = 50) # This can take a while - if running on a workstation, try a smaller number
notional <- 1000000
Create a portfolio containing all of the theortical securities
positions <- c ()
settlementDate <- expiry + as.difftime (2, format = “%X”, units = “days”) #2 days after expiry
for(option.strike in option.strikes)
{
security1 <- EquityIndexOptionSecurity(name=paste(“SPX Index”,option.strike),“CALL”,option.strike,“USD”,“SPX~Index~0”,exerciseType=EuropeanExerciseType(),expiry <- Sys.time () + as.difftime (26, format = “%X”, units = “weeks”),2.001,“NYSE”)
security1.id <- StoreSecurity (security1)
positions <- c (positions, PortfolioPosition (security = security1.id, quantity = 1))
}
node <- PortfolioNode (name = “EquityOptions”, positions = positions)
portfolio <- Portfolio (name = “EquityOptions Portfolio”, rootNode = node)
portfolio.id <- StorePortfolio (portfolio)
Fetch the position IDs as stored in the database - results will be keyed by these
positions <- sapply (fields.FudgeMsg (FetchPortfolio (portfolio.id)$root$positions), function (x) { x$Value$uniqueId })
Create a view on this portfolio. Note the constraints used to configure how
calculations are performed; refer to the online documentation for the full range
of parameters that can be used to control each underlying analytic model.
requirements <- list (
price = new.ValueRequirement (ValueRequirementNames.Present.Value, paste (
“CalculationMethod=LocalVolatilityPDE”,
“SmileInterpolator=Spline”,
“PDEDirection=Forward”,
sep = ", ")),
forwardDelta = new.ValueRequirement(
ValueRequirementNames.Forward.Delta, paste (
“CalculationMethod=LocalVolatilityPDE”,
“SmileInterpolator=SABR”,
“PDEDirection=Forward”,
sep = ", ")))
view <- ViewDefinition (“EquityOptionsExample”, portfolio.id, requirements)
view.id <- StoreViewDefinition (view)
calc.config <- “Default”
Create a view client attached to the snapshot and execute a cycle against current market data.
This could equally use a snapshot as shown in other examples.
view.client <- ViewClient (view.id, useSharedProcess = FALSE)
TriggerViewCycle (view.client)
view.result <- GetViewResult (viewClient = view.client, waitForResult = -1)
For each value requirement, extract the result column values and produce that in the portfolio
ordering (i.e. to correspond to option.strikes)
view.result.data <- results.ViewComputationResultModel (view.result)[[calc.config]]
results <- data.frame (
strikes = option.strikes,
lapply (requirements, function (requirement) {
columns <- columns.ViewComputationResultModel (view.result.data, requirement)
sapply (positions, function (position) {
firstValue.ViewComputationResultModel (view.result.data[position,], columns)
}, USE.NAMES = FALSE)}))
Print the results data frame
print (results)