private static final Logger s_logger = LoggerFactory.getLogger(VolatilitySurfaceHistoricalTimeSeriesFunction.class);
@Override
public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target,
final Set<ValueRequirement> desiredValues) throws AsynchronousExecution {
final HistoricalTimeSeriesSource timeSeriesSource = OpenGammaExecutionContext.getHistoricalTimeSeriesSource(executionContext);
final ConfigSource configSource = OpenGammaExecutionContext.getConfigSource(executionContext);
final ConfigDBVolatilitySurfaceDefinitionSource definitionSource = new ConfigDBVolatilitySurfaceDefinitionSource(configSource);
final ConfigDBVolatilitySurfaceSpecificationSource specificationSource = new ConfigDBVolatilitySurfaceSpecificationSource(configSource);
final ValueRequirement desiredValue = desiredValues.iterator().next();
final String dataField = desiredValue.getConstraint(HistoricalTimeSeriesFunctionUtils.DATA_FIELD_PROPERTY);
final String resolutionKey;
final Set<String> resolutionKeyConstraint = desiredValue.getConstraints().getValues(HistoricalTimeSeriesFunctionUtils.RESOLUTION_KEY_PROPERTY);
if (resolutionKeyConstraint == null || resolutionKeyConstraint.size() != 1) {
resolutionKey = "Null";
} else {
resolutionKey = Iterables.getOnlyElement(resolutionKeyConstraint);
}
final LocalDate startDate = DateConstraint.evaluate(executionContext, desiredValue.getConstraint(HistoricalTimeSeriesFunctionUtils.START_DATE_PROPERTY));
final boolean includeStart = HistoricalTimeSeriesFunctionUtils.parseBoolean(desiredValue.getConstraint(HistoricalTimeSeriesFunctionUtils.INCLUDE_START_PROPERTY));
final Set<String> endDateConstraint = desiredValue.getConstraints().getValues(HistoricalTimeSeriesFunctionUtils.END_DATE_PROPERTY);
final String endDateString;
if (endDateConstraint == null || endDateConstraint.size() != 1) {
endDateString = "Now";
} else {
endDateString = Iterables.getOnlyElement(endDateConstraint);
}
LocalDate endDate = DateConstraint.evaluate(executionContext, endDateString);
final boolean includeEnd = HistoricalTimeSeriesFunctionUtils.parseBoolean(desiredValue.getConstraint(HistoricalTimeSeriesFunctionUtils.INCLUDE_END_PROPERTY));
final String surfaceName = desiredValue.getConstraint(ValuePropertyNames.SURFACE);
final VolatilitySurfaceDefinition<Object, Object> definition = getSurfaceDefinition(target, surfaceName, definitionSource);
final VolatilitySurfaceSpecification specification = getSurfaceSpecification(target, surfaceName, specificationSource);
final SurfaceInstrumentProvider<Object, Object> provider = (SurfaceInstrumentProvider<Object, Object>) specification.getSurfaceInstrumentProvider();
final HistoricalTimeSeriesBundle bundle = new HistoricalTimeSeriesBundle();
for (final Object x : definition.getXs()) {
for (final Object y : definition.getYs()) {
ExternalId id = provider.getInstrument(x, y, endDate);
if (id.getScheme().equals(ExternalSchemes.BLOOMBERG_TICKER_WEAK)) {
id = ExternalSchemes.bloombergTickerSecurityId(id.getValue());
}
final ExternalIdBundle identifier = ExternalIdBundle.of(id);
final HistoricalTimeSeries timeSeries = timeSeriesSource.getHistoricalTimeSeries(dataField, identifier, resolutionKey, startDate, includeStart, endDate, includeEnd);
if (timeSeries != null) {
bundle.add(dataField, identifier, timeSeries);
} else {
s_logger.warn("Could not get time series for {}", identifier);
}