private ValueRequirement getIndexTimeSeriesRequirement(
     final InterestRateInstrumentType type,
     final SwapLeg leg,
     final ZonedDateTime swapEffectiveDate) {
   if (leg instanceof FloatingInterestRateLeg) {
     final FloatingInterestRateLeg floatingLeg = (FloatingInterestRateLeg) leg;
     final ExternalIdBundle id = getIndexIdForSwap(floatingLeg);
     final LocalDate startDate = swapEffectiveDate.toLocalDate().minusDays(360);
     // Implementation note: To catch first fixing. SwapSecurity does not have this date.
     final HistoricalTimeSeriesResolutionResult ts =
         getTimeSeriesResolver()
             .resolve(id, null, null, null, MarketDataRequirementNames.MARKET_VALUE, null);
     if (ts == null) {
       return null;
     }
     return HistoricalTimeSeriesFunctionUtils.createHTSRequirement(
         ts,
         MarketDataRequirementNames.MARKET_VALUE,
         DateConstraint.of(startDate),
         true,
         DateConstraint.VALUATION_TIME,
         true);
   }
   return null;
 }
 @Override
 public Set<ValueRequirement> getTimeSeriesRequirements(
     final FRASecurity security, final String[] curveNames) {
   final ExternalId indexId = security.getUnderlyingId();
   final ConventionBundle indexConvention =
       getConventionSource().getConventionBundle(indexId);
   if (indexConvention == null) {
     throw new OpenGammaRuntimeException(
         "No conventions found for floating reference rate " + indexId);
   }
   final ExternalIdBundle indexIdBundle = indexConvention.getIdentifiers();
   final HistoricalTimeSeriesResolutionResult timeSeries =
       getTimeSeriesResolver()
           .resolve(
               indexIdBundle,
               null,
               null,
               null,
               MarketDataRequirementNames.MARKET_VALUE,
               null);
   if (timeSeries == null) {
     return null;
   }
   return Collections.singleton(
       HistoricalTimeSeriesFunctionUtils.createHTSRequirement(
           timeSeries,
           MarketDataRequirementNames.MARKET_VALUE,
           DateConstraint.VALUATION_TIME.minus(Period.ofDays(7)).previousWeekDay(),
           true,
           DateConstraint.VALUATION_TIME,
           false));
 }
 private ValueRequirement getIndexTimeSeriesRequirement(
     final ExternalIdBundle id, final LocalDate startDate) {
   final HistoricalTimeSeriesResolutionResult timeSeries =
       getTimeSeriesResolver()
           .resolve(id, null, null, null, MarketDataRequirementNames.MARKET_VALUE, null);
   if (timeSeries == null) {
     return null;
   }
   return HistoricalTimeSeriesFunctionUtils.createHTSRequirement(
       timeSeries,
       MarketDataRequirementNames.MARKET_VALUE,
       DateConstraint.of(startDate),
       true,
       DateConstraint.VALUATION_TIME,
       true);
 }
 protected ValueRequirement getSensitivityRequirement(final ExternalId externalId) {
   final HistoricalTimeSeriesResolutionResult resolutionResult =
       _htsResolver.resolve(ExternalIdBundle.of(externalId), null, null, null, "EXPOSURE", null);
   final ValueRequirement htsRequirement =
       HistoricalTimeSeriesFunctionUtils.createHTSRequirement(
           resolutionResult,
           "EXPOSURE",
           DateConstraint.VALUATION_TIME,
           true,
           DateConstraint.VALUATION_TIME,
           true);
   return htsRequirement;
   // return new ValueRequirement();
   // return new ValueRequirement(/*ExternalDataRequirementNames.SENSITIVITY*/"EXPOSURE",
   // ComputationTargetType.PRIMITIVE, UniqueId.of(externalId.getScheme().getName(),
   // externalId.getValue()));
 }
 @Override
 public Set<ComputedValue> execute(
     final FunctionExecutionContext executionContext,
     final FunctionInputs inputs,
     final ComputationTarget target,
     final Set<ValueRequirement> desiredValues) {
   final FinancialSecurity security = (FinancialSecurity) target.getSecurity();
   final Currency currency = FinancialSecurityUtils.getCurrency(security);
   final Clock snapshotClock = executionContext.getValuationClock();
   final ZonedDateTime now = snapshotClock.zonedDateTime();
   final HistoricalTimeSeriesBundle timeSeries =
       HistoricalTimeSeriesFunctionUtils.getHistoricalTimeSeriesInputs(executionContext, inputs);
   final ValueRequirement desiredValue = desiredValues.iterator().next();
   final String curveCalculationConfigName =
       desiredValue.getConstraint(ValuePropertyNames.CURVE_CALCULATION_CONFIG);
   final ConfigSource configSource = OpenGammaExecutionContext.getConfigSource(executionContext);
   final ConfigDBCurveCalculationConfigSource curveCalculationConfigSource =
       new ConfigDBCurveCalculationConfigSource(configSource);
   final MultiCurveCalculationConfig curveCalculationConfig =
       curveCalculationConfigSource.getConfig(curveCalculationConfigName);
   if (curveCalculationConfig == null) {
     throw new OpenGammaRuntimeException(
         "Could not find curve calculation configuration named " + curveCalculationConfigName);
   }
   final String[] curveNames = curveCalculationConfig.getYieldCurveNames();
   final String[] yieldCurveNames =
       curveNames.length == 1 ? new String[] {curveNames[0], curveNames[0]} : curveNames;
   final String[] curveNamesForSecurity =
       FixedIncomeInstrumentCurveExposureHelper.getCurveNamesForSecurity(
           security, yieldCurveNames[0], yieldCurveNames[1]);
   final YieldCurveBundle bundle =
       YieldCurveFunctionUtils.getAllYieldCurves(
           inputs, curveCalculationConfig, curveCalculationConfigSource);
   final InstrumentDefinition<?> definition = security.accept(_visitor);
   if (definition == null) {
     throw new OpenGammaRuntimeException("Definition for security " + security + " was null");
   }
   final InstrumentDerivative derivative =
       getDerivative(
           security, now, timeSeries, curveNamesForSecurity, definition, _definitionConverter);
   return getComputedValues(
       derivative, bundle, security, target, curveCalculationConfigName, currency.getCode());
 }
 @Override
 public Set<ValueRequirement> getTimeSeriesRequirements(
     final IRFutureOptionSecurity security, final String[] curveNames) {
   final HistoricalTimeSeriesResolutionResult timeSeries =
       getTimeSeriesResolver()
           .resolve(
               security.getExternalIdBundle(),
               null,
               null,
               null,
               MarketDataRequirementNames.MARKET_VALUE,
               null);
   if (timeSeries == null) {
     return null;
   }
   return Collections.singleton(
       HistoricalTimeSeriesFunctionUtils.createHTSRequirement(
           timeSeries,
           MarketDataRequirementNames.MARKET_VALUE,
           DateConstraint.VALUATION_TIME.minus(Period.ofMonths(1)).previousWeekDay(),
           true,
           DateConstraint.VALUATION_TIME,
           false));
 }
 @Override
 public Set<ComputedValue> execute(
     final FunctionExecutionContext executionContext,
     final FunctionInputs inputs,
     final ComputationTarget target,
     final Set<ValueRequirement> desiredValues) {
   final Clock snapshotClock = executionContext.getValuationClock();
   final ZonedDateTime now = ZonedDateTime.now(snapshotClock);
   final SecuritySource securitySource =
       OpenGammaExecutionContext.getSecuritySource(executionContext);
   final SwaptionSecurity security = (SwaptionSecurity) target.getSecurity();
   final ValueRequirement desiredValue = desiredValues.iterator().next();
   final Currency currency = FinancialSecurityUtils.getCurrency(security);
   final String surfaceName = desiredValue.getConstraint(ValuePropertyNames.SURFACE);
   final String curveCalculationConfigName =
       desiredValue.getConstraint(ValuePropertyNames.CURVE_CALCULATION_CONFIG);
   final ConfigSource configSource = OpenGammaExecutionContext.getConfigSource(executionContext);
   final ConfigDBCurveCalculationConfigSource curveCalculationConfigSource =
       new ConfigDBCurveCalculationConfigSource(configSource);
   final MultiCurveCalculationConfig curveCalculationConfig =
       curveCalculationConfigSource.getConfig(curveCalculationConfigName);
   if (curveCalculationConfig == null) {
     throw new OpenGammaRuntimeException(
         "Could not find curve calculation configuration named " + curveCalculationConfigName);
   }
   String[] curveNames = curveCalculationConfig.getYieldCurveNames(); // TODO
   if (curveNames.length == 1) {
     curveNames = new String[] {curveNames[0], curveNames[0]};
   }
   final String[] fullCurveNames = new String[curveNames.length];
   for (int i = 0; i < curveNames.length; i++) {
     fullCurveNames[i] = curveNames[i] + "_" + currency.getCode();
   }
   final YieldCurveBundle curves =
       YieldCurveFunctionUtils.getYieldCurves(inputs, curveCalculationConfig);
   final Object volatilitySurfaceObject =
       inputs.getValue(getVolatilityRequirement(surfaceName, currency));
   if (volatilitySurfaceObject == null) {
     throw new OpenGammaRuntimeException("Could not get volatility surface");
   }
   final VolatilitySurface volatilitySurface = (VolatilitySurface) volatilitySurfaceObject;
   if (!(volatilitySurface.getSurface() instanceof InterpolatedDoublesSurface)) {
     throw new OpenGammaRuntimeException(
         "Expecting an InterpolatedDoublesSurface; got "
             + volatilitySurface.getSurface().getClass());
   }
   final InstrumentDefinition<?> definition = security.accept(_visitor);
   final HistoricalTimeSeriesBundle timeSeries =
       HistoricalTimeSeriesFunctionUtils.getHistoricalTimeSeriesInputs(executionContext, inputs);
   final InstrumentDerivative swaption =
       _definitionConverter.convert(security, definition, now, fullCurveNames, timeSeries);
   final ValueProperties properties =
       getResultProperties(currency.getCode(), curveCalculationConfigName, surfaceName);
   final ValueSpecification spec =
       new ValueSpecification(_valueRequirementName, target.toSpecification(), properties);
   final BlackFlatSwaptionParameters parameters =
       new BlackFlatSwaptionParameters(
           volatilitySurface.getSurface(),
           SwaptionUtils.getSwapGenerator(security, definition, securitySource));
   final YieldCurveWithBlackSwaptionBundle data =
       new YieldCurveWithBlackSwaptionBundle(parameters, curves);
   return getResult(swaption, data, spec);
 }
 @Override
 public Set<ValueRequirement> getRequirements(
     final FunctionCompilationContext context,
     final ComputationTarget target,
     final ValueRequirement desiredValue) {
   final Object positionOrNode = getTarget(target);
   final Set<ValueRequirement> result = new HashSet<ValueRequirement>();
   final ValueProperties constraints = desiredValue.getConstraints();
   final Set<String> samplingPeriodNames =
       constraints.getValues(ValuePropertyNames.SAMPLING_PERIOD);
   if (samplingPeriodNames == null || samplingPeriodNames.size() != 1) {
     return null;
   }
   final Set<String> scheduleCalculatorNames =
       constraints.getValues(ValuePropertyNames.SCHEDULE_CALCULATOR);
   if (scheduleCalculatorNames == null || scheduleCalculatorNames.size() != 1) {
     return null;
   }
   final Set<String> samplingFunctionNames =
       constraints.getValues(ValuePropertyNames.SAMPLING_FUNCTION);
   if (samplingFunctionNames == null || samplingFunctionNames.size() != 1) {
     return null;
   }
   final Set<String> returnCalculatorNames =
       constraints.getValues(ValuePropertyNames.RETURN_CALCULATOR);
   if (returnCalculatorNames == null || returnCalculatorNames.size() != 1) {
     return null;
   }
   final Set<String> stdDevCalculatorNames =
       constraints.getValues(ValuePropertyNames.STD_DEV_CALCULATOR);
   if (stdDevCalculatorNames == null || stdDevCalculatorNames.size() != 1) {
     return null;
   }
   final Set<String> covarianceCalculatorNames =
       constraints.getValues(ValuePropertyNames.COVARIANCE_CALCULATOR);
   if (covarianceCalculatorNames == null || covarianceCalculatorNames.size() != 1) {
     return null;
   }
   final Set<String> varianceCalculatorNames =
       constraints.getValues(ValuePropertyNames.VARIANCE_CALCULATOR);
   if (varianceCalculatorNames == null || varianceCalculatorNames.size() != 1) {
     return null;
   }
   final String samplingPeriodName = samplingPeriodNames.iterator().next();
   final String scheduleCalculatorName = scheduleCalculatorNames.iterator().next();
   final String samplingFunctionName = samplingFunctionNames.iterator().next();
   final String returnCalculatorName = returnCalculatorNames.iterator().next();
   final ValueProperties pnlSeriesProperties =
       ValueProperties.builder()
           .withAny(ValuePropertyNames.CURRENCY)
           .with(ValuePropertyNames.SAMPLING_PERIOD, samplingPeriodName)
           .with(ValuePropertyNames.SCHEDULE_CALCULATOR, scheduleCalculatorName)
           .with(ValuePropertyNames.SAMPLING_FUNCTION, samplingFunctionName)
           .with(ValuePropertyNames.RETURN_CALCULATOR, returnCalculatorName)
           .get();
   final ValueProperties betaProperties =
       ValueProperties.builder()
           .with(ValuePropertyNames.SAMPLING_PERIOD, samplingPeriodName)
           .with(ValuePropertyNames.SCHEDULE_CALCULATOR, scheduleCalculatorName)
           .with(ValuePropertyNames.SAMPLING_FUNCTION, samplingFunctionName)
           .with(ValuePropertyNames.RETURN_CALCULATOR, returnCalculatorName)
           .with(
               ValuePropertyNames.COVARIANCE_CALCULATOR,
               covarianceCalculatorNames.iterator().next())
           .with(ValuePropertyNames.VARIANCE_CALCULATOR, varianceCalculatorNames.iterator().next())
           .get();
   result.add(
       new ValueRequirement(
           ValueRequirementNames.PNL_SERIES, positionOrNode, pnlSeriesProperties));
   result.add(new ValueRequirement(ValueRequirementNames.FAIR_VALUE, positionOrNode));
   result.add(
       new ValueRequirement(ValueRequirementNames.CAPM_BETA, positionOrNode, betaProperties));
   final HistoricalTimeSeriesResolver resolver =
       OpenGammaCompilationContext.getHistoricalTimeSeriesResolver(context);
   final ConventionBundleSource conventionSource =
       OpenGammaCompilationContext.getConventionBundleSource(context);
   final ConventionBundle bundle =
       conventionSource.getConventionBundle(
           ExternalId.of(InMemoryConventionBundleMaster.SIMPLE_NAME_SCHEME, "USD_CAPM"));
   final HistoricalTimeSeriesResolutionResult timeSeries =
       resolver.resolve(
           bundle.getCAPMMarket(),
           null,
           null,
           null,
           MarketDataRequirementNames.MARKET_VALUE,
           _resolutionKey);
   if (timeSeries == null) {
     return null;
   }
   result.add(
       HistoricalTimeSeriesFunctionUtils.createHTSRequirement(
           timeSeries,
           MarketDataRequirementNames.MARKET_VALUE,
           DateConstraint.VALUATION_TIME.minus(samplingPeriodName),
           true,
           DateConstraint.VALUATION_TIME,
           true));
   return result;
 }