@SuppressWarnings("synthetic-access")
 @Override
 public Set<ValueRequirement> getTimeSeriesRequirements(
     final SwapSecurity security, final String[] curveNames) {
   Validate.notNull(security, "security");
   final SwapLeg payLeg = security.getPayLeg();
   final SwapLeg receiveLeg = security.getReceiveLeg();
   final ZonedDateTime swapStartDate = security.getEffectiveDate();
   final ZonedDateTime swapStartLocalDate =
       ZonedDateTime.of(swapStartDate.toLocalDate(), LocalTime.of(0, 0), TimeZone.UTC);
   final ValueRequirement payLegTS =
       getIndexTimeSeriesRequirement(
           InterestRateInstrumentType.getInstrumentTypeFromSecurity(security),
           payLeg,
           swapStartLocalDate);
   final ValueRequirement receiveLegTS =
       getIndexTimeSeriesRequirement(
           InterestRateInstrumentType.getInstrumentTypeFromSecurity(security),
           receiveLeg,
           swapStartLocalDate);
   final Set<ValueRequirement> requirements = new HashSet<ValueRequirement>();
   if (payLegTS != null) {
     requirements.add(payLegTS);
   }
   if (receiveLegTS != null) {
     requirements.add(receiveLegTS);
   }
   return requirements;
 }
Пример #2
0
 public static boolean payFixed(final SwapSecurity security) {
   final SwapLeg payLeg = security.getPayLeg();
   final SwapLeg receiveLeg = security.getReceiveLeg();
   if (payLeg instanceof FixedInterestRateLeg && receiveLeg instanceof FloatingInterestRateLeg) {
     return true;
   }
   if (payLeg instanceof FloatingInterestRateLeg && receiveLeg instanceof FixedInterestRateLeg) {
     return false;
   }
   throw new OpenGammaRuntimeException("Swap was not fixed / floating");
 }
 protected SwapSecurity createUnderlying(
     final ZonedDateTime earliestMaturity, final ZonedDateTime swaptionExpiry) {
   SwapSecurity security;
   do {
     do {
       getUnderlyingGenerator().setSwationExpiry(swaptionExpiry.toLocalDate());
       security = getUnderlyingGenerator().createSecurity();
     } while (security == null);
   } while ((FinancialSecurityUtils.getCurrency(security) == null)
       || security.getMaturityDate().isBefore(earliestMaturity));
   return security;
 }
 static InstrumentDerivative getDerivative(
     final FinancialSecurity security,
     final ZonedDateTime now,
     final HistoricalTimeSeriesBundle timeSeries,
     final String[] curveNames,
     final InstrumentDefinition<?> definition,
     final FixedIncomeConverterDataProvider definitionConverter) {
   final InstrumentDerivative derivative;
   if (security instanceof SwapSecurity) {
     final SwapSecurity swapSecurity = (SwapSecurity) security;
     final InterestRateInstrumentType type = SwapSecurityUtils.getSwapType(swapSecurity);
     if (type == InterestRateInstrumentType.SWAP_FIXED_IBOR
         || type == InterestRateInstrumentType.SWAP_FIXED_IBOR_WITH_SPREAD
         || type == InterestRateInstrumentType.SWAP_FIXED_OIS) {
       final Frequency resetFrequency;
       if (swapSecurity.getPayLeg() instanceof FloatingInterestRateLeg) {
         resetFrequency = ((FloatingInterestRateLeg) swapSecurity.getPayLeg()).getFrequency();
       } else {
         resetFrequency = ((FloatingInterestRateLeg) swapSecurity.getReceiveLeg()).getFrequency();
       }
       derivative =
           definitionConverter.convert(
               security,
               definition,
               now,
               FixedIncomeInstrumentCurveExposureHelper.getCurveNamesForSecurity(
                   security, curveNames, resetFrequency),
               timeSeries);
     } else {
       derivative =
           definitionConverter.convert(
               security,
               definition,
               now,
               FixedIncomeInstrumentCurveExposureHelper.getCurveNamesForSecurity(
                   security, curveNames),
               timeSeries);
     }
   } else {
     derivative =
         definitionConverter.convert(
             security,
             definition,
             now,
             FixedIncomeInstrumentCurveExposureHelper.getCurveNamesForSecurity(
                 security, curveNames),
             timeSeries);
   }
   return derivative;
 }
 @Override
 public List<ExternalId> visitSwapSecurity(final SwapSecurity security) {
   final List<ExternalId> result = new ArrayList<>();
   final SwapLeg payLeg = security.getPayLeg();
   final SwapLeg receiveLeg = security.getReceiveLeg();
   final String securityType = security.getSecurityType();
   if (payLeg.getRegionId().equals(receiveLeg.getRegionId())) {
     return Arrays.asList(
         ExternalId.of(
             SECURITY_IDENTIFIER, securityType + SEPARATOR + payLeg.getRegionId().getValue()));
   }
   result.add(
       ExternalId.of(
           SECURITY_IDENTIFIER, securityType + SEPARATOR + payLeg.getRegionId().getValue()));
   result.add(
       ExternalId.of(
           SECURITY_IDENTIFIER, securityType + SEPARATOR + receiveLeg.getRegionId().getValue()));
   return result;
 }
 @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;
 }
Пример #7
0
 @Override
 public boolean equals(Object obj) {
   if (obj == this) {
     return true;
   }
   if (obj != null && obj.getClass() == this.getClass()) {
     SwapSecurity other = (SwapSecurity) obj;
     return JodaBeanUtils.equal(getTradeDate(), other.getTradeDate())
         && JodaBeanUtils.equal(getEffectiveDate(), other.getEffectiveDate())
         && JodaBeanUtils.equal(getMaturityDate(), other.getMaturityDate())
         && JodaBeanUtils.equal(getCounterparty(), other.getCounterparty())
         && JodaBeanUtils.equal(getPayLeg(), other.getPayLeg())
         && JodaBeanUtils.equal(getReceiveLeg(), other.getReceiveLeg())
         && super.equals(obj);
   }
   return false;
 }
 @Override
 @SuppressWarnings({"unchecked", "synthetic-access"})
 public InstrumentDerivative convert(
     final SwapSecurity security,
     final SwapDefinition definition,
     final ZonedDateTime now,
     final String[] curveNames,
     final HistoricalTimeSeriesBundle timeSeries) {
   Validate.notNull(security, "security");
   final SwapLeg payLeg = security.getPayLeg();
   final SwapLeg receiveLeg = security.getReceiveLeg();
   final ZonedDateTime fixingSeriesStartDate =
       security.getEffectiveDate().isBefore(now) ? security.getEffectiveDate() : now;
   final ZonedDateTime fixingSeriesStartLocalDate =
       ZonedDateTime.of(
           fixingSeriesStartDate.toLocalDate(), LocalTime.of(0, 0), TimeZone.UTC);
   final boolean includeCurrentDatesFixing = true;
   final DoubleTimeSeries<ZonedDateTime> payLegTS =
       getIndexTimeSeries(
           payLeg, fixingSeriesStartLocalDate, now, includeCurrentDatesFixing, timeSeries);
   final DoubleTimeSeries<ZonedDateTime> receiveLegTS =
       getIndexTimeSeries(
           receiveLeg,
           fixingSeriesStartLocalDate,
           now,
           includeCurrentDatesFixing,
           timeSeries);
   if (payLegTS != null) {
     if (receiveLegTS != null) {
       try {
         return definition.toDerivative(
             now, new DoubleTimeSeries[] {payLegTS, receiveLegTS}, curveNames);
       } catch (final OpenGammaRuntimeException e) {
         final ExternalId id =
             ((FloatingInterestRateLeg) payLeg).getFloatingReferenceRateId();
         throw new OpenGammaRuntimeException(
             "Could not get fixing value for series with identifier " + id, e);
       }
     }
     if (InterestRateInstrumentType.getInstrumentTypeFromSecurity(security)
         == InterestRateInstrumentType.SWAP_FIXED_CMS) {
       return definition.toDerivative(
           now, new DoubleTimeSeries[] {payLegTS, payLegTS}, curveNames);
     }
     try {
       return definition.toDerivative(now, new DoubleTimeSeries[] {payLegTS}, curveNames);
     } catch (final OpenGammaRuntimeException e) {
       final ExternalId id = ((FloatingInterestRateLeg) payLeg).getFloatingReferenceRateId();
       throw new OpenGammaRuntimeException(
           "Could not get fixing value for series with identifier " + id, e);
     }
   }
   if (receiveLegTS != null) {
     if (InterestRateInstrumentType.getInstrumentTypeFromSecurity(security)
         == InterestRateInstrumentType.SWAP_FIXED_CMS) {
       try {
         return definition.toDerivative(
             now, new DoubleTimeSeries[] {receiveLegTS, receiveLegTS}, curveNames);
       } catch (final OpenGammaRuntimeException e) {
         final ExternalId id =
             ((FloatingInterestRateLeg) payLeg).getFloatingReferenceRateId();
         throw new OpenGammaRuntimeException(
             "Could not get fixing value for series with identifier " + id, e);
       }
     }
     try {
       return definition.toDerivative(
           now, new DoubleTimeSeries[] {receiveLegTS}, curveNames);
     } catch (final OpenGammaRuntimeException e) {
       final ExternalId id =
           ((FloatingInterestRateLeg) receiveLeg).getFloatingReferenceRateId();
       throw new OpenGammaRuntimeException(
           "Could not get fixing value for series with identifier " + id, e);
     }
   }
   throw new OpenGammaRuntimeException(
       "Could not get fixing series for either the pay or receive leg");
 }
Пример #9
0
  // TODO doesn't handle cross-currency swaps yet
  public static InterestRateInstrumentType getSwapType(final SwapSecurity security) {
    final SwapLeg payLeg = security.getPayLeg();
    final SwapLeg receiveLeg = security.getReceiveLeg();
    if (!payLeg.getRegionId().equals(receiveLeg.getRegionId())) {
      throw new OpenGammaRuntimeException("Pay and receive legs must be from same region");
    }
    if (payLeg instanceof FixedInterestRateLeg && receiveLeg instanceof FloatingInterestRateLeg) {

      final FloatingInterestRateLeg floatingLeg = (FloatingInterestRateLeg) receiveLeg;
      if (floatingLeg instanceof FloatingSpreadIRLeg) {
        return InterestRateInstrumentType.SWAP_FIXED_IBOR_WITH_SPREAD;
      }
      final FloatingRateType floatingRateType = floatingLeg.getFloatingRateType();
      switch (floatingRateType) {
        case IBOR:
          return InterestRateInstrumentType.SWAP_FIXED_IBOR;
        case CMS:
          return InterestRateInstrumentType.SWAP_FIXED_CMS;
        case OIS:
          return InterestRateInstrumentType.SWAP_FIXED_OIS;
        default:
          throw new OpenGammaRuntimeException(
              "Unsupported Floating rate type: " + floatingRateType);
      }
    } else if (payLeg instanceof FloatingInterestRateLeg
        && receiveLeg instanceof FixedInterestRateLeg) {
      final FloatingInterestRateLeg floatingLeg = (FloatingInterestRateLeg) payLeg;

      if (floatingLeg instanceof FloatingSpreadIRLeg) {
        return InterestRateInstrumentType.SWAP_FIXED_IBOR_WITH_SPREAD;
      }
      final FloatingRateType floatingRateType = floatingLeg.getFloatingRateType();
      switch (floatingRateType) {
        case IBOR:
          return InterestRateInstrumentType.SWAP_FIXED_IBOR;
        case CMS:
          return InterestRateInstrumentType.SWAP_FIXED_CMS;
        case OIS:
          return InterestRateInstrumentType.SWAP_FIXED_OIS;
        default:
          throw new OpenGammaRuntimeException(
              "Unsupported Floating rate type: " + floatingRateType);
      }
    }
    if (payLeg instanceof FloatingInterestRateLeg
        && receiveLeg instanceof FloatingInterestRateLeg) {
      final FloatingInterestRateLeg payLeg1 = (FloatingInterestRateLeg) payLeg;
      final FloatingInterestRateLeg receiveLeg1 = (FloatingInterestRateLeg) receiveLeg;
      if (payLeg1.getFloatingRateType().isIbor()) {
        if (receiveLeg1.getFloatingRateType().isIbor()) {
          return InterestRateInstrumentType.SWAP_IBOR_IBOR;
        }
        return InterestRateInstrumentType.SWAP_IBOR_CMS;
      }
      if (receiveLeg1.getFloatingRateType().isIbor()) {
        return InterestRateInstrumentType.SWAP_IBOR_CMS;
      }
      return InterestRateInstrumentType.SWAP_CMS_CMS;
    }
    throw new OpenGammaRuntimeException(
        "Can only handle fixed-floating (pay and receive) swaps and floating-floating swaps");
  }
Пример #10
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 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;
  }