@Override
public ForwardSwapSecurity createSecurity() {
final Currency ccy = getRandomCurrency();
final ZonedDateTime now = ZonedDateTime.now();
final ZonedDateTime tradeDate = previousWorkingDay(now.minusDays(getRandom(getDaysTrading())), ccy);
final Tenor forward = getRandom(FORWARDS);
final ZonedDateTime forwardDate = nextWorkingDay(now.plus(forward.getPeriod()), ccy);
ConventionBundle swapConvention = getConventionBundleSource().getConventionBundle(ExternalId.of(InMemoryConventionBundleMaster.SIMPLE_NAME_SCHEME, ccy.getCode() + "_SWAP"));
if (swapConvention == null) {
s_logger.error("Couldn't get swap convention for {}", ccy.getCode());
return null;
}
final Tenor maturity = getRandom(TENORS);
// get the convention of the identifier of the initial rate
ConventionBundle liborConvention = getConventionBundleSource().getConventionBundle(swapConvention.getSwapFloatingLegInitialRate());
if (liborConvention == null) {
s_logger.error("Couldn't get libor convention for {}", swapConvention.getSwapFloatingLegInitialRate());
return null;
}
// look up the rate timeseries identifier out of the bundle
final ExternalId tsIdentifier = getTimeSeriesIdentifier(liborConvention);
// look up the value on our chosen trade date
final HistoricalTimeSeries initialRateSeries = getHistoricalSource().getHistoricalTimeSeries(MarketDataRequirementNames.MARKET_VALUE, tsIdentifier.toBundle(), null, tradeDate.toLocalDate(),
true, tradeDate.toLocalDate(), true);
if (initialRateSeries == null || initialRateSeries.getTimeSeries().isEmpty()) {
s_logger.error("couldn't get series for {} on {}", tsIdentifier, tradeDate);
return null;
}
Double initialRate = initialRateSeries.getTimeSeries().getEarliestValue();
// get the identifier for the swap rate for the maturity we're interested in (assuming the fixed rate will be =~ swap rate)
final ExternalId swapRateForMaturityIdentifier = getSwapRateFor(ccy, tradeDate.toLocalDate(), maturity, forward);
if (swapRateForMaturityIdentifier == null) {
s_logger.error("Couldn't get swap rate identifier for {} [{}] from {}", new Object[] {ccy, maturity, tradeDate });
return null;
}
final HistoricalTimeSeries fixedRateSeries = getHistoricalSource().getHistoricalTimeSeries(MarketDataRequirementNames.MARKET_VALUE, swapRateForMaturityIdentifier.toBundle(),
null, tradeDate.toLocalDate(), true,
tradeDate.toLocalDate(), true);
if (fixedRateSeries == null) {
s_logger.error("can't find time series for {} on {}", swapRateForMaturityIdentifier, tradeDate);
return null;
}
Double fixedRate = (fixedRateSeries.getTimeSeries().getEarliestValue() + getRandom().nextDouble()) / 100d;
Double notional = (double) getRandom(100000) * 1000;
ZonedDateTime maturityDate = forwardDate.plus(maturity.getPeriod());
String counterparty = "CParty";
SwapLeg fixedLeg = new FixedInterestRateLeg(swapConvention.getSwapFixedLegDayCount(),
swapConvention.getSwapFixedLegFrequency(),
swapConvention.getSwapFixedLegRegion(),
swapConvention.getSwapFixedLegBusinessDayConvention(),
new InterestRateNotional(ccy, notional),
false, fixedRate);
FloatingInterestRateLeg floatingLeg = new FloatingInterestRateLeg(swapConvention.getSwapFloatingLegDayCount(),
swapConvention.getSwapFloatingLegFrequency(),
swapConvention.getSwapFloatingLegRegion(),
swapConvention.getSwapFloatingLegBusinessDayConvention(),
new InterestRateNotional(ccy, notional),
false, tsIdentifier,
FloatingRateType.IBOR);
floatingLeg.setInitialFloatingRate(initialRate);
String fixedLegDescription = RATE_FORMATTER.format(fixedRate);
String floatingLegDescription = swapConvention.getSwapFloatingLegInitialRate().getValue();
boolean isPayFixed = getRandom().nextBoolean();
SwapLeg payLeg;
String payLegDescription;
SwapLeg receiveLeg;
String receiveLegDescription;
if (isPayFixed) {
payLeg = fixedLeg;
payLegDescription = fixedLegDescription;
receiveLeg = floatingLeg;
receiveLegDescription = floatingLegDescription;
} else {
payLeg = floatingLeg;
payLegDescription = floatingLegDescription;
receiveLeg = fixedLeg;
receiveLegDescription = fixedLegDescription;
}
final ForwardSwapSecurity swap = new ForwardSwapSecurity(tradeDate, tradeDate, maturityDate, counterparty, payLeg, receiveLeg, forwardDate);
swap.setName("IR Forward Swap " + ccy + " " + NOTIONAL_FORMATTER.format(notional) + " " + maturity.getPeriod() + " from " + forwardDate.toString(DATE_FORMATTER) + " - " + payLegDescription +
" / " + receiveLegDescription);
return swap;
}