/
DiscountingIborFutureTradePricerTest.java
171 lines (145 loc) · 8.37 KB
/
DiscountingIborFutureTradePricerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (C) 2015 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.pricer.index;
import static com.opengamma.strata.collect.TestHelper.assertThrowsIllegalArg;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.time.LocalDate;
import org.testng.annotations.Test;
import com.opengamma.strata.basics.ReferenceData;
import com.opengamma.strata.basics.currency.CurrencyAmount;
import com.opengamma.strata.market.sensitivity.PointSensitivities;
import com.opengamma.strata.pricer.rate.IborIndexRates;
import com.opengamma.strata.pricer.rate.SimpleRatesProvider;
import com.opengamma.strata.product.index.ResolvedIborFuture;
import com.opengamma.strata.product.index.ResolvedIborFutureTrade;
/**
* Test {@link DiscountingIborFutureTradePricer}.
*/
@Test
public class DiscountingIborFutureTradePricerTest {
private static final ReferenceData REF_DATA = ReferenceData.standard();
private static final DiscountingIborFutureTradePricer PRICER_TRADE = DiscountingIborFutureTradePricer.DEFAULT;
private static final DiscountingIborFutureProductPricer PRICER_PRODUCT = DiscountingIborFutureProductPricer.DEFAULT;
private static final ResolvedIborFutureTrade FUTURE_TRADE = IborFutureDummyData.IBOR_FUTURE_TRADE.resolve(REF_DATA);
private static final ResolvedIborFuture FUTURE = FUTURE_TRADE.getProduct();
private static final double RATE = 0.045;
private static final double TOLERANCE_PRICE = 1.0e-9;
private static final double TOLERANCE_PRICE_DELTA = 1.0e-9;
private static final double TOLERANCE_PV = 1.0e-4;
private static final double TOLERANCE_PV_DELTA = 1.0e-2;
//-------------------------------------------------------------------------
public void test_price() {
IborIndexRates mockIbor = mock(IborIndexRates.class);
SimpleRatesProvider prov = new SimpleRatesProvider();
prov.setIborRates(mockIbor);
when(mockIbor.rate(FUTURE.getIborRate().getObservation())).thenReturn(RATE);
assertEquals(PRICER_TRADE.price(FUTURE_TRADE, prov), 1.0 - RATE, TOLERANCE_PRICE);
}
//-------------------------------------------------------------------------
public void test_presentValue() {
double currentPrice = 0.995;
double referencePrice = 0.9925;
double currentPriceIndex = PRICER_PRODUCT.marginIndex(FUTURE_TRADE.getProduct(), currentPrice);
double referencePriceIndex = PRICER_PRODUCT.marginIndex(FUTURE_TRADE.getProduct(), referencePrice);
double presentValueExpected = (currentPriceIndex - referencePriceIndex) * FUTURE_TRADE.getQuantity();
CurrencyAmount presentValueComputed = PRICER_TRADE.presentValue(FUTURE_TRADE, currentPrice, referencePrice);
assertEquals(presentValueComputed.getAmount(), presentValueExpected, TOLERANCE_PV);
}
//-------------------------------------------------------------------------
public void test_reference_price_after_trade_date() {
LocalDate tradeDate = FUTURE_TRADE.getTradedPrice().get().getTradeDate();
LocalDate valuationDate = tradeDate.plusDays(1);
double settlementPrice = 0.995;
double referencePrice = PRICER_TRADE.referencePrice(FUTURE_TRADE, valuationDate, settlementPrice);
assertEquals(referencePrice, settlementPrice);
}
public void test_reference_price_on_trade_date() {
LocalDate tradeDate = FUTURE_TRADE.getTradedPrice().get().getTradeDate();
LocalDate valuationDate = tradeDate;
double settlementPrice = 0.995;
double referencePrice = PRICER_TRADE.referencePrice(FUTURE_TRADE, valuationDate, settlementPrice);
assertEquals(referencePrice, FUTURE_TRADE.getTradedPrice().get().getPrice());
}
public void test_reference_price_val_date_not_null() {
double settlementPrice = 0.995;
assertThrowsIllegalArg(() -> PRICER_TRADE.referencePrice(FUTURE_TRADE, null, settlementPrice));
}
//-------------------------------------------------------------------------
public void test_parSpread_after_trade_date() {
IborIndexRates mockIbor = mock(IborIndexRates.class);
SimpleRatesProvider prov = new SimpleRatesProvider();
prov.setIborRates(mockIbor);
prov.setValuationDate(FUTURE_TRADE.getTradedPrice().get().getTradeDate().plusDays(1));
when(mockIbor.rate(FUTURE.getIborRate().getObservation())).thenReturn(RATE);
double lastClosingPrice = 0.99;
double parSpreadExpected = PRICER_TRADE.price(FUTURE_TRADE, prov) - lastClosingPrice;
double parSpreadComputed = PRICER_TRADE.parSpread(FUTURE_TRADE, prov, lastClosingPrice);
assertEquals(parSpreadComputed, parSpreadExpected, TOLERANCE_PRICE);
}
public void test_parSpread_on_trade_date() {
IborIndexRates mockIbor = mock(IborIndexRates.class);
SimpleRatesProvider prov = new SimpleRatesProvider();
prov.setIborRates(mockIbor);
prov.setValuationDate(FUTURE_TRADE.getTradedPrice().get().getTradeDate());
when(mockIbor.rate(FUTURE.getIborRate().getObservation())).thenReturn(RATE);
double lastClosingPrice = 0.99;
double parSpreadExpected = PRICER_TRADE.price(FUTURE_TRADE, prov) - FUTURE_TRADE.getTradedPrice().get().getPrice();
double parSpreadComputed = PRICER_TRADE.parSpread(FUTURE_TRADE, prov, lastClosingPrice);
assertEquals(parSpreadComputed, parSpreadExpected, TOLERANCE_PRICE);
}
//-------------------------------------------------------------------------
public void test_presentValue_after_trade_date() {
IborIndexRates mockIbor = mock(IborIndexRates.class);
SimpleRatesProvider prov = new SimpleRatesProvider();
prov.setIborRates(mockIbor);
prov.setValuationDate(FUTURE_TRADE.getTradedPrice().get().getTradeDate().plusDays(1));
when(mockIbor.rate(FUTURE.getIborRate().getObservation())).thenReturn(RATE);
double lastClosingPrice = 1.025;
DiscountingIborFutureTradePricer pricerFn = DiscountingIborFutureTradePricer.DEFAULT;
double expected = ((1.0 - RATE) - lastClosingPrice) *
FUTURE.getAccrualFactor() * FUTURE.getNotional() * FUTURE_TRADE.getQuantity();
CurrencyAmount computed = pricerFn.presentValue(FUTURE_TRADE, prov, lastClosingPrice);
assertEquals(computed.getAmount(), expected, TOLERANCE_PV);
assertEquals(computed.getCurrency(), FUTURE.getCurrency());
}
public void test_presentValue_on_trade_date() {
IborIndexRates mockIbor = mock(IborIndexRates.class);
SimpleRatesProvider prov = new SimpleRatesProvider();
prov.setIborRates(mockIbor);
prov.setValuationDate(FUTURE_TRADE.getTradedPrice().get().getTradeDate());
when(mockIbor.rate(FUTURE.getIborRate().getObservation())).thenReturn(RATE);
double lastClosingPrice = 1.025;
DiscountingIborFutureTradePricer pricerFn = DiscountingIborFutureTradePricer.DEFAULT;
double expected = ((1.0 - RATE) - FUTURE_TRADE.getTradedPrice().get().getPrice()) *
FUTURE.getAccrualFactor() * FUTURE.getNotional() * FUTURE_TRADE.getQuantity();
CurrencyAmount computed = pricerFn.presentValue(FUTURE_TRADE, prov, lastClosingPrice);
assertEquals(computed.getAmount(), expected, TOLERANCE_PV);
assertEquals(computed.getCurrency(), FUTURE.getCurrency());
}
//-------------------------------------------------------------------------
public void test_presentValueSensitivity() {
IborIndexRates mockIbor = mock(IborIndexRates.class);
SimpleRatesProvider prov = new SimpleRatesProvider();
prov.setIborRates(mockIbor);
PointSensitivities sensiPrice = PRICER_PRODUCT.priceSensitivity(FUTURE, prov);
PointSensitivities sensiPresentValueExpected = sensiPrice.multipliedBy(
FUTURE.getNotional() * FUTURE.getAccrualFactor() * FUTURE_TRADE.getQuantity());
PointSensitivities sensiPresentValueComputed = PRICER_TRADE.presentValueSensitivity(FUTURE_TRADE, prov);
assertTrue(sensiPresentValueComputed.equalWithTolerance(sensiPresentValueExpected, TOLERANCE_PV_DELTA));
}
//-------------------------------------------------------------------------
public void test_parSpreadSensitivity() {
IborIndexRates mockIbor = mock(IborIndexRates.class);
SimpleRatesProvider prov = new SimpleRatesProvider();
prov.setIborRates(mockIbor);
PointSensitivities sensiExpected = PRICER_PRODUCT.priceSensitivity(FUTURE, prov);
PointSensitivities sensiComputed = PRICER_TRADE.parSpreadSensitivity(FUTURE_TRADE, prov);
assertTrue(sensiComputed.equalWithTolerance(sensiExpected, TOLERANCE_PRICE_DELTA));
}
}