public class VolatilitySurfaceDefinitionFunction extends AbstractFunction {
@Override
public CompiledFunctionDefinition compile(final FunctionCompilationContext outerContext, final Instant atInstant) {
final ConfigSource configSource = OpenGammaCompilationContext.getConfigSource(outerContext);
final ConfigDBVolatilitySurfaceDefinitionSource source = new ConfigDBVolatilitySurfaceDefinitionSource(configSource);
final ZonedDateTime atZDT = ZonedDateTime.ofInstant(atInstant, ZoneOffset.UTC);
return new AbstractInvokingCompiledFunction(atZDT.with(LocalTime.MIDNIGHT), atZDT.plusDays(1).with(LocalTime.MIDNIGHT).minusNanos(1000000)) {
@Override
public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target,
final Set<ValueRequirement> desiredValues) {
final ValueRequirement desiredValue = desiredValues.iterator().next();
final String surfaceName = desiredValue.getConstraint(ValuePropertyNames.SURFACE);
final String instrumentType = desiredValue.getConstraint(InstrumentTypeProperties.PROPERTY_SURFACE_INSTRUMENT_TYPE);
VolatilitySurfaceDefinition<?, ?> definition = null;
if (instrumentType.equals(InstrumentTypeProperties.FOREX)) {
final UnorderedCurrencyPair pair = UnorderedCurrencyPair.of(target.getUniqueId());
String name = pair.getFirstCurrency().getCode() + pair.getSecondCurrency().getCode();
String fullDefinitionName = surfaceName + "_" + name;
definition = source.getDefinition(fullDefinitionName, instrumentType);
if (definition == null) {
name = pair.getSecondCurrency().getCode() + pair.getFirstCurrency().getCode();
fullDefinitionName = surfaceName + "_" + name;
definition = source.getDefinition(fullDefinitionName, instrumentType);
if (definition == null) {
throw new OpenGammaRuntimeException("Could not get volatility surface definition named " + fullDefinitionName);
}
}
} else if (instrumentType.equals(InstrumentTypeProperties.EQUITY_OPTION) || instrumentType.equals(InstrumentTypeProperties.EQUITY_FUTURE_OPTION)) {
//FIXME: Modify to take ExternalId to avoid incorrect cast to UniqueId
final String fullDefinitionName = surfaceName + "_" + EquitySecurityUtils.getTrimmedTarget(UniqueId.parse(target.getValue().toString()));
definition = source.getDefinition(fullDefinitionName, instrumentType);
if (definition == null) {
throw new OpenGammaRuntimeException("Could not get volatility surface definition named " + fullDefinitionName + " for instrument type " + instrumentType);
}
} else {
final String fullDefinitionName = surfaceName + "_" + target.getUniqueId().getValue();
definition = source.getDefinition(fullDefinitionName, instrumentType);
if (definition == null) {
throw new OpenGammaRuntimeException("Could not get volatility surface definition named " + fullDefinitionName + " for instrument type " + instrumentType);
}
}
@SuppressWarnings("synthetic-access")