@Override
 public List<ExternalId> visitSwaptionSecurity(final SwaptionSecurity security) {
   final List<ExternalId> result = new ArrayList<>();
   final SwapSecurity underlyingSwap =
       (SwapSecurity)
           _securitySource.getSingle(
               ExternalIdBundle.of(security.getUnderlyingId())); // TODO version
   final SwapLeg payLeg = underlyingSwap.getPayLeg();
   final SwapLeg receiveLeg = underlyingSwap.getReceiveLeg();
   final String securityType = security.getSecurityType();
   if (payLeg.getRegionId().equals(receiveLeg.getRegionId())) {
     return Arrays.asList(
         ExternalId.of(SECURITY_IDENTIFIER, securityType + "_" + payLeg.getRegionId().getValue()));
   }
   result.add(
       ExternalId.of(SECURITY_IDENTIFIER, securityType + "_" + payLeg.getRegionId().getValue()));
   result.add(
       ExternalId.of(
           SECURITY_IDENTIFIER, securityType + "_" + receiveLeg.getRegionId().getValue()));
   return result;
 }
 @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 SwaptionSecurity createSecurity() {
    final int optionLength = getRandom(OPTION_LENGTH);
    ZonedDateTime expiry = ZonedDateTime.now().plusMonths(optionLength);
    final SwapSecurity underlying = createUnderlying(expiry.plusMonths(2), expiry);
    final Currency currency = FinancialSecurityUtils.getCurrency(underlying);
    expiry = nextWorkingDay(expiry, currency);
    final boolean isPayer = getRandom().nextBoolean();
    final boolean isLong = getRandom().nextBoolean();
    final boolean isCashSettled = getRandom().nextBoolean();
    final ZonedDateTime settlementDate = nextWorkingDay(expiry.plusDays(2), currency);
    final Double notional =
        underlying
            .getPayLeg()
            .getNotional()
            .accept(
                new NotionalVisitor<Double>() {

                  @Override
                  public Double visitCommodityNotional(final CommodityNotional notional) {
                    return null;
                  }

                  @Override
                  public Double visitInterestRateNotional(final InterestRateNotional notional) {
                    return notional.getAmount();
                  }

                  @Override
                  public Double visitSecurityNotional(final SecurityNotional notional) {
                    return null;
                  }

                  @Override
                  public Double visitVarianceSwapNotional(final VarianceSwapNotional notional) {
                    return notional.getAmount();
                  }
                });
    if (notional == null) {
      return null;
    }
    Double rate = getRate(underlying.getPayLeg());
    if (rate == null) {
      rate = getRate(underlying.getReceiveLeg());
      if (rate == null) {
        return null;
      }
    }
    final SwaptionSecurity security =
        new SwaptionSecurity(
            isPayer,
            getSecurityPersister().storeSecurity(underlying).iterator().next(),
            isLong,
            new Expiry(expiry),
            isCashSettled,
            currency,
            notional,
            new EuropeanExerciseType(),
            settlementDate);
    security.setName(
        createName(
            currency,
            optionLength,
            (int) MONTHS.between(underlying.getEffectiveDate(), underlying.getMaturityDate()),
            notional,
            rate));
    return security;
  }
예제 #4
0
  protected void addSecuritySpecificMetaData(ManageableSecurity security, FlexiBean out) {
    if (security.getSecurityType().equals(SwapSecurity.SECURITY_TYPE)) {
      SwapSecurity swapSecurity = (SwapSecurity) security;
      out.put("payLegType", swapSecurity.getPayLeg().accept(new SwapLegClassifierVisitor()));
      out.put(
          "receiveLegType", swapSecurity.getReceiveLeg().accept(new SwapLegClassifierVisitor()));
    }
    if (security.getSecurityType().equals(FutureSecurity.SECURITY_TYPE)) {
      FutureSecurity futureSecurity = (FutureSecurity) security;
      out.put("futureSecurityType", futureSecurity.accept(new FutureSecurityTypeVisitor()));
      out.put("basket", getBondFutureBasket(security));
      Security underlyingSecurity = getUnderlyingFutureSecurity(futureSecurity);
      if (underlyingSecurity != null) {
        out.put("underlyingSecurity", underlyingSecurity);
      }
    }
    if (security.getSecurityType().equals(EquityOptionSecurity.SECURITY_TYPE)) {
      EquityOptionSecurity equityOption = (EquityOptionSecurity) security;
      addUnderlyingSecurity(out, equityOption.getUnderlyingId());
    }
    if (security.getSecurityType().equals(IRFutureOptionSecurity.SECURITY_TYPE)) {
      IRFutureOptionSecurity irFutureOption = (IRFutureOptionSecurity) security;
      addUnderlyingSecurity(out, irFutureOption.getUnderlyingId());
    }
    if (security.getSecurityType().equals(SwaptionSecurity.SECURITY_TYPE)) {
      SwaptionSecurity swaptionSecurity = (SwaptionSecurity) security;
      addUnderlyingSecurity(out, swaptionSecurity.getUnderlyingId());
    }
    if (security.getSecurityType().equals(EquityBarrierOptionSecurity.SECURITY_TYPE)) {
      EquityBarrierOptionSecurity equityBarrierOptionSecurity =
          (EquityBarrierOptionSecurity) security;
      addUnderlyingSecurity(out, equityBarrierOptionSecurity.getUnderlyingId());
    }
    if (security.getSecurityType().equals(CapFloorSecurity.SECURITY_TYPE)) {
      CapFloorSecurity capFloorSecurity = (CapFloorSecurity) security;
      addUnderlyingSecurity(out, capFloorSecurity.getUnderlyingId());
    }
    if (security.getSecurityType().equals(CapFloorCMSSpreadSecurity.SECURITY_TYPE)) {
      CapFloorCMSSpreadSecurity capFloorCMSSpreadSecurity = (CapFloorCMSSpreadSecurity) security;
      Security shortUnderlying = getSecurity(capFloorCMSSpreadSecurity.getShortId());
      Security longUnderlying = getSecurity(capFloorCMSSpreadSecurity.getLongId());
      if (shortUnderlying != null) {
        out.put("shortSecurity", shortUnderlying);
      }
      if (longUnderlying != null) {
        out.put("longSecurity", longUnderlying);
      }
    }
    if (security.getSecurityType().equals(EquityIndexOptionSecurity.SECURITY_TYPE)) {
      EquityIndexOptionSecurity equityIndxOption = (EquityIndexOptionSecurity) security;
      addUnderlyingSecurity(out, equityIndxOption.getUnderlyingId());
    }
    if (security.getSecurityType().equals(FRASecurity.SECURITY_TYPE)) {
      FRASecurity fraSecurity = (FRASecurity) security;
      addUnderlyingSecurity(out, fraSecurity.getUnderlyingId());
    }
    if (security.getSecurityType().equals(SecurityEntryData.EXTERNAL_SENSITIVITIES_SECURITY_TYPE)) {
      RawSecurity rawSecurity = (RawSecurity) security;
      FudgeMsgEnvelope msg =
          OpenGammaFudgeContext.getInstance().deserialize(rawSecurity.getRawData());
      SecurityEntryData securityEntryData =
          OpenGammaFudgeContext.getInstance()
              .fromFudgeMsg(SecurityEntryData.class, msg.getMessage());

      out.put("securityEntryData", securityEntryData);
      out.put("securityAttributes", security.getAttributes());
      RawSecurity underlyingRawSecurity =
          (RawSecurity) getSecurity(securityEntryData.getFactorSetId());
      if (underlyingRawSecurity != null) {
        FudgeMsgEnvelope factorIdMsg =
            OpenGammaFudgeContext.getInstance().deserialize(underlyingRawSecurity.getRawData());
        @SuppressWarnings("unchecked")
        List<FactorExposureData> factorExposureDataList =
            OpenGammaFudgeContext.getInstance().fromFudgeMsg(List.class, factorIdMsg.getMessage());
        s_logger.error(factorExposureDataList.toString());
        List<FactorExposure> factorExposuresList = convertToFactorExposure(factorExposureDataList);
        out.put("factorExposuresList", factorExposuresList);
      } else {
        s_logger.error("Couldn't find security");
      }
    }
    if (security
        .getSecurityType()
        .equals(FactorExposureData.EXTERNAL_SENSITIVITIES_RISK_FACTORS_SECURITY_TYPE)) {
      RawSecurity rawSecurity = (RawSecurity) security;
      FudgeMsgEnvelope msg =
          OpenGammaFudgeContext.getInstance().deserialize(rawSecurity.getRawData());
      @SuppressWarnings("unchecked")
      List<FactorExposureData> factorExposureDataList =
          OpenGammaFudgeContext.getInstance().fromFudgeMsg(List.class, msg.getMessage());
      List<FactorExposure> factorExposuresList = convertToFactorExposure(factorExposureDataList);
      out.put("factorExposuresList", factorExposuresList);
    }
  }
 @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 = snapshotClock.zonedDateTime();
   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 forwardCurveName =
       desiredValue.getConstraint(YieldCurveFunction.PROPERTY_FORWARD_CURVE);
   final String fundingCurveName =
       desiredValue.getConstraint(YieldCurveFunction.PROPERTY_FUNDING_CURVE);
   final String curveCalculationMethod =
       desiredValue.getConstraint(ValuePropertyNames.CURVE_CALCULATION_METHOD);
   final String surfaceName = desiredValue.getConstraint(ValuePropertyNames.SURFACE);
   final Object forwardCurveObject =
       inputs.getValue(
           YieldCurveFunction.getCurveRequirement(
               currency,
               forwardCurveName,
               forwardCurveName,
               fundingCurveName,
               curveCalculationMethod));
   if (forwardCurveObject == null) {
     throw new OpenGammaRuntimeException("Could not get forward curve");
   }
   final Object fundingCurveObject =
       inputs.getValue(
           YieldCurveFunction.getCurveRequirement(
               currency,
               fundingCurveName,
               forwardCurveName,
               fundingCurveName,
               curveCalculationMethod));
   if (fundingCurveObject == null) {
     throw new OpenGammaRuntimeException("Could not get funding curve");
   }
   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 YieldAndDiscountCurve forwardCurve = (YieldAndDiscountCurve) forwardCurveObject;
   final YieldAndDiscountCurve fundingCurve = (YieldAndDiscountCurve) fundingCurveObject;
   final InstrumentDefinition<?> definition = security.accept(_visitor);
   final InstrumentDerivative swaption =
       definition.toDerivative(now, new String[] {fundingCurveName, forwardCurveName});
   final ValueProperties properties =
       getResultProperties(
           currency.getCode(),
           forwardCurveName,
           fundingCurveName,
           curveCalculationMethod,
           surfaceName);
   final ValueSpecification spec =
       new ValueSpecification(_valueRequirementName, target.toSpecification(), properties);
   final YieldCurveBundle curves =
       new YieldCurveBundle(
           new String[] {fundingCurveName, forwardCurveName},
           new YieldAndDiscountCurve[] {fundingCurve, forwardCurve});
   final BlackSwaptionParameters parameters =
       new BlackSwaptionParameters(
           volatilitySurface.getSurface(),
           SwaptionUtils.getSwapGenerator(security, definition, securitySource));
   final YieldCurveWithBlackSwaptionBundle data =
       new YieldCurveWithBlackSwaptionBundle(parameters, curves);
   return getResult(swaption, data, spec);
 }