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()));
 }
  public void getHistoricalTimeSeriesByExternalIdWithMetaData() throws Exception {
    HistoricalTimeSeriesInfoSearchRequest request =
        new HistoricalTimeSeriesInfoSearchRequest(IDENTIFIERS);
    request.setValidityDate(LocalDate.now());
    request.setDataSource(BBG_DATA_SOURCE);
    request.setDataProvider(CMPL_DATA_PROVIDER);
    request.setDataField(CLOSE_DATA_FIELD);

    HistoricalTimeSeriesInfoSearchResult searchResult = new HistoricalTimeSeriesInfoSearchResult();
    HistoricalTimeSeriesInfoDocument doc = new HistoricalTimeSeriesInfoDocument();
    doc.setUniqueId(UID);

    when(_mockResolver.resolve(
            IDENTIFIERS,
            LocalDate.now(),
            BBG_DATA_SOURCE,
            CMPL_DATA_PROVIDER,
            CLOSE_DATA_FIELD,
            null))
        .thenReturn(new HistoricalTimeSeriesResolutionResult(doc.getInfo()));

    doc.getInfo().setTimeSeriesObjectId(UID.getObjectId());
    searchResult.getDocuments().add(doc);
    when(_mockMaster.search(request)).thenReturn(searchResult);

    ManageableHistoricalTimeSeries hts = new ManageableHistoricalTimeSeries();
    hts.setUniqueId(UID);
    hts.setTimeSeries(randomTimeSeries());
    when(_mockMaster.getTimeSeries(
            UID.getObjectId(),
            VersionCorrection.LATEST,
            HistoricalTimeSeriesGetFilter.ofRange(null, null)))
        .thenReturn(hts);

    HistoricalTimeSeries test =
        _tsSource.getHistoricalTimeSeries(
            IDENTIFIERS, BBG_DATA_SOURCE, CMPL_DATA_PROVIDER, CLOSE_DATA_FIELD);
    verify(_mockMaster, times(1))
        .getTimeSeries(
            UID.getObjectId(),
            VersionCorrection.LATEST,
            HistoricalTimeSeriesGetFilter.ofRange(null, null));

    assertEquals(UID, test.getUniqueId());
  }
  public void getHistoricalTimeSeriesByExternalIdWithoutMetaData() throws Exception {
    ManageableHistoricalTimeSeries hts = new ManageableHistoricalTimeSeries();
    hts.setUniqueId(UID);
    hts.setTimeSeries(randomTimeSeries());
    when(_mockMaster.getTimeSeries(UID, HistoricalTimeSeriesGetFilter.ofRange(null, null)))
        .thenReturn(hts);
    ManageableHistoricalTimeSeriesInfo tsInfo = new ManageableHistoricalTimeSeriesInfo();
    tsInfo.setUniqueId(UID);
    when(_mockResolver.resolve(
            IDENTIFIERS, LocalDate.now(), null, null, CLOSE_DATA_FIELD, TEST_CONFIG))
        .thenReturn(new HistoricalTimeSeriesResolutionResult(tsInfo));

    HistoricalTimeSeries test =
        _tsSource.getHistoricalTimeSeries(CLOSE_DATA_FIELD, IDENTIFIERS, TEST_CONFIG);
    verify(_mockMaster, times(1))
        .getTimeSeries(UID, HistoricalTimeSeriesGetFilter.ofRange(null, null));

    assertEquals(UID, test.getUniqueId());
    assertEquals(hts.getTimeSeries().times(), test.getTimeSeries().times());
    assertEquals(hts.getTimeSeries().values(), test.getTimeSeries().values());
  }
  public void getHistoricalWithInclusiveExclusiveDates() throws Exception {
    LocalDate end = DateUtils.previousWeekDay();
    LocalDate start = end.minusDays(7);

    HistoricalTimeSeriesInfoSearchRequest request =
        new HistoricalTimeSeriesInfoSearchRequest(IDENTIFIERS);
    request.setValidityDate(LocalDate.now());
    request.setDataSource(BBG_DATA_SOURCE);
    request.setDataProvider(CMPL_DATA_PROVIDER);
    request.setDataField(CLOSE_DATA_FIELD);
    LocalDateDoubleTimeSeries timeSeries = randomTimeSeries();

    HistoricalTimeSeriesInfoSearchResult searchResult = new HistoricalTimeSeriesInfoSearchResult();
    HistoricalTimeSeriesInfoDocument doc = new HistoricalTimeSeriesInfoDocument();
    doc.setUniqueId(UID);
    doc.getInfo().setTimeSeriesObjectId(UID.getObjectId());
    searchResult.getDocuments().add(doc);

    when(_mockResolver.resolve(
            IDENTIFIERS,
            LocalDate.now(),
            BBG_DATA_SOURCE,
            CMPL_DATA_PROVIDER,
            CLOSE_DATA_FIELD,
            null))
        .thenReturn(new HistoricalTimeSeriesResolutionResult(doc.getInfo()));

    for (boolean includeStart : new boolean[] {true, false}) {
      for (boolean includeEnd : new boolean[] {true, false}) {
        // Also test max points limit for various values
        for (Integer maxPoints : new Integer[] {null, -10, -1, 1, 0, -2, 2, 10}) {
          LocalDate startInput = start;
          LocalDate endInput = end;
          if (!includeStart) {
            startInput = start.plusDays(1);
          }
          if (!includeEnd) {
            endInput = end.minusDays(1);
          }

          ManageableHistoricalTimeSeries hts = new ManageableHistoricalTimeSeries();
          LocalDateDoubleTimeSeries lddts =
              (maxPoints == null)
                      || (Math.abs(maxPoints)
                          >= timeSeries.subSeries(start, includeStart, end, includeEnd).size())
                  ? timeSeries.subSeries(start, includeStart, end, includeEnd)
                  : (maxPoints >= 0)
                      ? timeSeries.subSeries(start, includeStart, end, includeEnd).head(maxPoints)
                      : timeSeries.subSeries(start, includeStart, end, includeEnd).tail(-maxPoints);
          hts.setUniqueId(UID);
          hts.setTimeSeries(lddts);
          when(_mockMaster.getTimeSeries(
                  UID.getObjectId(),
                  VersionCorrection.LATEST,
                  HistoricalTimeSeriesGetFilter.ofRange(startInput, endInput, maxPoints)))
              .thenReturn(hts);
          when(_mockMaster.search(request)).thenReturn(searchResult);

          HistoricalTimeSeries test =
              (maxPoints == null)
                  ? _tsSource.getHistoricalTimeSeries(
                      IDENTIFIERS,
                      BBG_DATA_SOURCE,
                      CMPL_DATA_PROVIDER,
                      CLOSE_DATA_FIELD,
                      start,
                      includeStart,
                      end,
                      includeEnd)
                  : _tsSource.getHistoricalTimeSeries(
                      IDENTIFIERS,
                      BBG_DATA_SOURCE,
                      CMPL_DATA_PROVIDER,
                      CLOSE_DATA_FIELD,
                      start,
                      includeStart,
                      end,
                      includeEnd,
                      maxPoints);

          assertEquals(UID, test.getUniqueId());
          assertEquals(hts.getTimeSeries(), test.getTimeSeries());
        }
      }
    }
  }
 @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;
 }