public class VolatilitySurfaceSpecificationFunction extends AbstractFunction {
@Override
public CompiledFunctionDefinition compile(final FunctionCompilationContext outerContext, final Instant atInstant) {
final ConfigSource configSource = OpenGammaCompilationContext.getConfigSource(outerContext);
final ConfigDBVolatilitySurfaceSpecificationSource source = new ConfigDBVolatilitySurfaceSpecificationSource(configSource);
final ZonedDateTime atZDT = ZonedDateTime.ofInstant(atInstant, ZoneOffset.UTC);
return new AbstractInvokingCompiledFunction(atZDT.with(LocalTime.MIDNIGHT).toInstant(), atZDT.plusDays(1).with(LocalTime.MIDNIGHT).minusNanos(1000000).toInstant()) {
@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);
VolatilitySurfaceSpecification specification = null;
if (instrumentType.equals(InstrumentTypeProperties.FOREX)) {
final UnorderedCurrencyPair pair = UnorderedCurrencyPair.of(target.getUniqueId());
String name = pair.getFirstCurrency().getCode() + pair.getSecondCurrency().getCode();
String fullSpecificationName = surfaceName + "_" + name;
specification = source.getSpecification(fullSpecificationName, instrumentType);
if (specification == null) {
name = pair.getSecondCurrency().getCode() + pair.getFirstCurrency().getCode();
fullSpecificationName = surfaceName + "_" + name;
specification = source.getSpecification(fullSpecificationName, instrumentType);
if (specification == null) {
throw new OpenGammaRuntimeException("Could not get volatility surface specification named " + fullSpecificationName);
}
}
} else if (instrumentType.equals(InstrumentTypeProperties.EQUITY_OPTION) || instrumentType.equals(InstrumentTypeProperties.EQUITY_FUTURE_OPTION)) {
final String fullSpecificationName = surfaceName + "_" + EquitySecurityUtils.getTrimmedTarget(UniqueId.parse(target.getValue().toString()));
specification = source.getSpecification(fullSpecificationName, instrumentType);
if (specification == null) {
throw new OpenGammaRuntimeException("Could not get volatility surface specification named " + fullSpecificationName + " for instrument type " + instrumentType);
}
} else {
final String fullSpecificationName = surfaceName + "_" + target.getUniqueId().getValue();
specification = source.getSpecification(fullSpecificationName, instrumentType);
if (specification == null) {
throw new OpenGammaRuntimeException("Could not get volatility surface specification named " + fullSpecificationName + " for instrument type " + instrumentType);
}
}
@SuppressWarnings("synthetic-access")