Skip to content

Pricer 2592 fix irregular dates yield #2655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static com.opengamma.strata.product.bond.FixedCouponBondYieldConvention.US_STREET;

import java.time.LocalDate;
import java.util.List;
import java.util.function.Function;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -624,20 +625,32 @@ private double dirtyPriceFromYieldStandard(
LocalDate settlementDate,
double yield) {

int nbCoupon = bond.getPeriodicPayments().size();
double factorOnPeriod = 1 + yield / ((double) bond.getFrequency().eventsPerYear());
List<FixedCouponBondPaymentPeriod> periodicPayments = bond.getPeriodicPayments();
int eventsPerYear = bond.getFrequency().eventsPerYear();
double factorOnPeriod = 1 + yield / ((double) eventsPerYear);
double fixedRate = bond.getFixedRate();
double pvAtFirstCoupon = 0;
int pow = 0;
for (int loopcpn = 0; loopcpn < nbCoupon; loopcpn++) {
FixedCouponBondPaymentPeriod period = bond.getPeriodicPayments().get(loopcpn);
double factor = 1;
boolean first = true;
for (FixedCouponBondPaymentPeriod period : periodicPayments) {
if ((period.hasExCouponPeriod() && !settlementDate.isAfter(period.getDetachmentDate())) ||
(!period.hasExCouponPeriod() && period.getPaymentDate().isAfter(settlementDate))) {
pvAtFirstCoupon += fixedRate * period.getYearFraction() / Math.pow(factorOnPeriod, pow);
++pow;
double yearFraction = period.getYearFraction();
if (first) {
first = false;
factor = 1;
} else {
if (period.isIsRegular()) {
factor *= factorOnPeriod;
} else {
factor *= Math.pow(factorOnPeriod, yearFraction * eventsPerYear);
}
}
pvAtFirstCoupon += yearFraction / factor;
}
}
pvAtFirstCoupon += 1d / Math.pow(factorOnPeriod, pow - 1);
pvAtFirstCoupon *= fixedRate;
pvAtFirstCoupon += 1d / factor;
return pvAtFirstCoupon * Math.pow(factorOnPeriod, -factorToNextCoupon(bond, settlementDate));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.assertj.core.api.Assertions.offset;

import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.List;

Expand Down Expand Up @@ -115,6 +116,27 @@ public class DiscountingFixedCouponBondProductPricerTest {
.yieldConvention(YIELD_CONVENTION)
.build()
.resolve(REF_DATA);
private static final PeriodicSchedule PERIOD_SCHEDULE_WITH_FIRST_REGULAR_START_DATE = PeriodicSchedule.builder()
.startDate(LocalDate.of(2021, Month.JUNE, 30))
.firstRegularStartDate(LocalDate.of(2021, Month.OCTOBER, 15))
.endDate(LocalDate.of(2026, Month.JUNE, 30))
.frequency(Frequency.P6M)
.businessDayAdjustment(BUSINESS_ADJUST)
.stubConvention(StubConvention.SMART_FINAL)
.build();
private static final ResolvedFixedCouponBond PRODUCT_WITH_FIRST_REGULAR_START_DATE = FixedCouponBond.builder()
.securityId(SECURITY_ID)
.dayCount(DayCounts.THIRTY_E_360_ISDA)
.fixedRate(0.085)
.legalEntityId(ISSUER_ID)
.currency(EUR)
.notional(1_000_000)
.accrualSchedule(PERIOD_SCHEDULE_WITH_FIRST_REGULAR_START_DATE)
.settlementDateOffset(DaysAdjustment.ofBusinessDays(2, EUR_CALENDAR))
.yieldConvention(FixedCouponBondYieldConvention.DE_BONDS)
.exCouponPeriod(EX_COUPON)
.build()
.resolve(REF_DATA);

// rates provider
private static final CurveInterpolator INTERPOLATOR = CurveInterpolators.LINEAR;
Expand Down Expand Up @@ -151,6 +173,63 @@ public class DiscountingFixedCouponBondProductPricerTest {
private static final RatesFiniteDifferenceSensitivityCalculator FD_CAL =
new RatesFiniteDifferenceSensitivityCalculator(EPS);

@Test
public void test_yield_act_act_isda() {
PeriodicSchedule period = PeriodicSchedule.builder()
.startDate(LocalDate.of(2021, Month.JUNE, 30))
.endDate(LocalDate.of(2026, Month.JUNE, 30))
.frequency(Frequency.P6M)
.businessDayAdjustment(BUSINESS_ADJUST)
.build();
ResolvedFixedCouponBond bond = FixedCouponBond.builder()
.securityId(SECURITY_ID)
.dayCount(DayCounts.ACT_ACT_ISDA)
.fixedRate(0.085)
.legalEntityId(ISSUER_ID)
.currency(EUR)
.notional(100)
.accrualSchedule(period)
.settlementDateOffset(DaysAdjustment.ofBusinessDays(2, EUR_CALENDAR))
.yieldConvention(FixedCouponBondYieldConvention.DE_BONDS)
.exCouponPeriod(EX_COUPON)
.build()
.resolve(REF_DATA);
double cleanPrice = 1.05;
LocalDate settlementDate = period.getStartDate();
double dirtyPrice = PRICER.dirtyPriceFromCleanPrice(bond, settlementDate, cleanPrice);
assertThat(dirtyPrice).isCloseTo(cleanPrice, offset(TOL)); // 2.x.
double yield = PRICER.yieldFromDirtyPrice(bond, settlementDate, dirtyPrice);
assertThat(yield).isCloseTo(0.07286881667273096, offset(TOL)); // 2.x.œœ
}

@Test
public void test_yield_act_act_icma() {
PeriodicSchedule period = PeriodicSchedule.builder()
.startDate(LocalDate.of(2021, Month.JUNE, 30))
.endDate(LocalDate.of(2026, Month.JUNE, 30))
.frequency(Frequency.P6M)
.businessDayAdjustment(BUSINESS_ADJUST)
.build();
ResolvedFixedCouponBond bond = FixedCouponBond.builder()
.securityId(SECURITY_ID)
.dayCount(DayCounts.ACT_ACT_ICMA)
.fixedRate(0.085)
.legalEntityId(ISSUER_ID)
.currency(EUR)
.notional(100)
.accrualSchedule(period)
.settlementDateOffset(DaysAdjustment.ofBusinessDays(2, EUR_CALENDAR))
.yieldConvention(FixedCouponBondYieldConvention.DE_BONDS)
.exCouponPeriod(EX_COUPON)
.build()
.resolve(REF_DATA);
double cleanPrice = 1.05;
LocalDate settlementDate = period.getStartDate();
double dirtyPrice = PRICER.dirtyPriceFromCleanPrice(bond, settlementDate, cleanPrice);
assertThat(dirtyPrice).isCloseTo(cleanPrice, offset(TOL)); // 2.x.
double yield = PRICER.yieldFromDirtyPrice(bond, settlementDate, dirtyPrice);
assertThat(yield).isCloseTo(0.07288818170674201, offset(TOL)); // 2.x.œœ
}
//-------------------------------------------------------------------------
@Test
public void test_presentValue() {
Expand Down Expand Up @@ -330,6 +409,21 @@ public void test_dirtyPriceFromCleanPrice_ukNewIssue() {
assertThat(cleanPrice).isCloseTo(dirtyPrice - accruedInterest / NOTIONAL, offset(NOTIONAL * TOL));
}

@Test
public void test_yieldWithFirstRegularStartDate() {
double cleanPrice = 1.0009;
LocalDate settlementDate = LocalDate.of(2023, Month.JUNE, 6);
double dirtyPrice = PRICER.dirtyPriceFromCleanPrice(PRODUCT_WITH_FIRST_REGULAR_START_DATE, settlementDate, cleanPrice);
assertThat(dirtyPrice).isCloseTo(1.0129416666666667, offset(TOL)); // 2.x.
double yield = PRICER.yieldFromDirtyPrice(PRODUCT_WITH_FIRST_REGULAR_START_DATE, settlementDate, dirtyPrice);
assertThat(yield).isCloseTo(0.08465593560577835, offset(TOL));
settlementDate = LocalDate.of(2024, Month.APRIL, 26);
double dirtyPriceRbt = PRICER.dirtyPriceFromYield(PRODUCT_WITH_FIRST_REGULAR_START_DATE, settlementDate, yield);
double cleanPriceRbt = PRICER.cleanPriceFromDirtyPrice(PRODUCT_WITH_FIRST_REGULAR_START_DATE, settlementDate, dirtyPriceRbt);
double delta = (cleanPriceRbt - cleanPrice) * PRODUCT_WITH_FIRST_REGULAR_START_DATE.getNotional();
assertThat(delta).isCloseTo(-100.27459341377387, offset(TOL));
}

//-------------------------------------------------------------------------
@Test
public void test_zSpreadFromCurvesAndPV_continuous() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public ResolvedFixedCouponBond resolve(ReferenceData refData) {
.currency(currency)
.fixedRate(fixedRate)
.yearFraction(unadjustedPeriod.yearFraction(dayCount, unadjustedSchedule))
.isRegular(period.isRegular(accrualSchedule.getFrequency(), accrualSchedule.calculatedRollConvention()))
.build());
}
ImmutableList<FixedCouponBondPaymentPeriod> periodicPayments = accrualPeriods.build();
Expand Down
Loading