ConfigDBCurveCalculationConfigSource.reinitOnChanges(context, this);
}
@Override
public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) {
final Position position = target.getPosition();
final ConfigSource configSource = OpenGammaExecutionContext.getConfigSource(executionContext);
final Clock snapshotClock = executionContext.getValuationClock();
final LocalDate now = ZonedDateTime.now(snapshotClock).toLocalDate();
final Currency currency = FinancialSecurityUtils.getCurrency(position.getSecurity());
final String currencyString = currency.getCode();
final ValueRequirement desiredValue = desiredValues.iterator().next();
final ValueProperties constraints = desiredValue.getConstraints();
final String surfaceName = getPropertyName(constraints.getValues(ValuePropertyNames.SURFACE));
final String curveCalculationConfigName = desiredValue.getConstraint(ValuePropertyNames.CURVE_CALCULATION_CONFIG);
final Set<String> yieldCurveNames = constraints.getValues(ValuePropertyNames.CURVE);
final Period samplingPeriod = getSamplingPeriod(constraints.getValues(ValuePropertyNames.SAMPLING_PERIOD));
final LocalDate startDate = now.minus(samplingPeriod);
final Schedule scheduleCalculator = getScheduleCalculator(constraints.getValues(ValuePropertyNames.SCHEDULE_CALCULATOR));
final TimeSeriesSamplingFunction samplingFunction = getSamplingFunction(constraints.getValues(ValuePropertyNames.SAMPLING_FUNCTION));
final LocalDate[] schedule = HOLIDAY_REMOVER.getStrippedSchedule(scheduleCalculator.getSchedule(startDate, now, true, false), WEEKEND_CALENDAR); //REVIEW emcleod should "fromEnd" be hard-coded?
final ConfigDBCurveCalculationConfigSource curveCalculationConfigSource = new ConfigDBCurveCalculationConfigSource(configSource);
DoubleTimeSeries<?> result = null;
final MultiCurveCalculationConfig curveCalculationConfig = curveCalculationConfigSource.getConfig(curveCalculationConfigName);
for (final String yieldCurveName : yieldCurveNames) {
final ValueRequirement ycnsRequirement = getYCNSRequirement(currencyString, curveCalculationConfigName, yieldCurveName, surfaceName, target);
final DoubleLabelledMatrix1D ycns = (DoubleLabelledMatrix1D) inputs.getValue(ycnsRequirement);
final HistoricalTimeSeriesBundle ychts = (HistoricalTimeSeriesBundle) inputs.getValue(getYCHTSRequirement(currency, yieldCurveName, samplingPeriod.toString()));
final DoubleTimeSeries<?> pnLSeries;
if (curveCalculationConfig.getCalculationMethod().equals(FXImpliedYieldCurveFunction.FX_IMPLIED)) {
pnLSeries = getPnLSeries(ycns, ychts, schedule, samplingFunction);
} else {
final ValueRequirement curveSpecRequirement = getCurveSpecRequirement(currency, yieldCurveName);
final InterpolatedYieldCurveSpecificationWithSecurities curveSpec = (InterpolatedYieldCurveSpecificationWithSecurities) inputs.getValue(curveSpecRequirement);
pnLSeries = getPnLSeries(curveSpec, ycns, ychts, schedule, samplingFunction);
}
if (result == null) {
result = pnLSeries;
} else {
result = result.add(result);
}
}
if (result == null) {
throw new OpenGammaRuntimeException("Could not get any values for security " + position.getSecurity());
}
result = result.multiply(position.getQuantity().doubleValue());
final ValueProperties resultProperties = getResultProperties(desiredValue, currencyString);
final ValueSpecification resultSpec = new ValueSpecification(ValueRequirementNames.PNL_SERIES, target.toSpecification(), resultProperties);
return Sets.newHashSet(new ComputedValue(resultSpec, result));
}