16 #include <boost/test/unit_test.hpp>
17 #include <oret/toplevelfixture.hpp>
19 #include <boost/make_shared.hpp>
21 #include <ql/currencies/america.hpp>
22 #include <ql/instruments/asianoption.hpp>
23 #include <ql/math/interpolations/linearinterpolation.hpp>
24 #include <ql/termstructures/yield/flatforward.hpp>
25 #include <ql/time/daycounters/actual360.hpp>
34 using namespace boost::unit_test_framework;
35 using namespace boost::algorithm;
44 TestMarket(
const Real spot,
const Date& expiry,
const Rate domRate,
const Rate forRate,
45 const Volatility flatVolatility)
48 asof_ = Date(01, Feb, 2021);
49 DayCounter dayCounter = Actual360();
52 Handle<YieldTermStructure> domestic(QuantLib::ext::make_shared<FlatForward>(asof_, domRate, dayCounter));
53 Handle<YieldTermStructure> foreign(QuantLib::ext::make_shared<FlatForward>(asof_, forRate, dayCounter));
58 std::map<std::string, Handle<Quote>> quotes;
59 quotes[
"JPYUSD"] = Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(spot));
60 fx_ = QuantLib::ext::make_shared<FXTriangulation>(quotes);
63 Handle<BlackVolTermStructure> volatility(
64 QuantLib::ext::make_shared<BlackConstantVol>(asof_, TARGET(), flatVolatility, dayCounter));
69 struct DiscreteAsianTestData {
84 BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
86 BOOST_AUTO_TEST_SUITE(FxAsianOptionTests)
90 BOOST_TEST_MESSAGE(
"Testing FX Asian option trade building with constant vol term structure");
95 std::vector<DiscreteAsianTestData> asians = {
96 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 2, 0.13, 1.3942835683},
97 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 4, 0.13, 1.5852442983},
98 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 8, 0.13, 1.66970673},
99 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 12, 0.13, 1.6980019214},
100 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 26, 0.13, 1.7255070456},
101 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 52, 0.13, 1.7401553533},
102 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 100, 0.13, 1.7478303712},
103 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 2, 0.13, 1.8496053697},
104 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 4, 0.13, 2.0111495205},
105 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 8, 0.13, 2.0852138818},
106 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 12, 0.13, 2.1105094397},
107 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 26, 0.13, 2.1346526695},
108 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 52, 0.13, 2.147489651},
109 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 100, 0.13, 2.154728109},
110 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 2, 0.13, 2.63315092584},
111 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 4, 0.13, 2.76723962361},
112 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 8, 0.13, 2.83124836881},
113 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 12, 0.13, 2.84290301412},
114 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 26, 0.13, 2.88179560417},
115 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 52, 0.13, 2.88447044543},
116 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 100, 0.13, 2.89985329603}};
118 Date asof = Date(01, Feb, 2021);
120 QuantLib::ext::shared_ptr<EngineFactory> engineFactory;
121 QuantLib::ext::shared_ptr<Market> market;
123 for (
const auto& a : asians) {
124 Time deltaT = a.length / (a.fixings - 1);
126 vector<Date> fixingDates(a.fixings);
127 vector<std::string> strFixingDates(a.fixings);
128 for (Size i = 0; i < a.fixings; ++i) {
129 fixingDates[i] = (asof +
static_cast<Integer
>((a.firstFixing + i * deltaT) * 360 + 0.5));
130 strFixingDates[i] =
to_string(fixingDates[i]);
132 expiry = fixingDates[a.fixings - 1];
134 ScheduleDates scheduleDates(
"NullCalendar",
"",
"", strFixingDates);
137 market = QuantLib::ext::make_shared<TestMarket>(a.spot, expiry, a.domesticRate, a.foreignRate, a.volatility);
138 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
139 std::string productName =
"FxAsianOptionArithmeticPrice";
140 engineData->model(productName) =
"GarmanKohlhagen";
141 engineData->engine(productName) =
"MCDiscreteArithmeticAPEngine";
142 engineData->engineParameters(productName) = {{
"ProcessType",
"Discrete"}, {
"BrownianBridge",
"True"},
143 {
"AntitheticVariate",
"False"}, {
"ControlVariate",
"True"},
144 {
"RequiredSamples",
"2047"}, {
"Seed",
"0"}};
145 engineFactory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
148 Settings::instance().evaluationDate() = market->asofDate();
152 OptionData optionData(
"Long",
to_string(a.type),
"European",
true, {to_string(expiry)},
"Cash",
"",
154 vector<Real>(), vector<Real>(),
"",
"",
"", vector<string>(), vector<string>(),
"",
"",
155 "",
"Asian",
"Arithmetic", boost::none, boost::none, boost::none);
157 QuantLib::ext::shared_ptr<FxAsianOption> asianOption = QuantLib::ext::make_shared<FxAsianOption>(
158 env,
"FxAsianOption", 1.0,
TradeStrike(a.strike,
"USD"), optionData, scheduleData,
159 QuantLib::ext::make_shared<FXUnderlying>(
"FX",
"ECB-JPY-USD", 1.0), Date(),
"USD");
160 BOOST_CHECK_NO_THROW(asianOption->build(engineFactory));
163 QuantLib::ext::shared_ptr<Instrument> qlInstrument = asianOption->instrument()->qlInstrument();
165 QuantLib::ext::shared_ptr<DiscreteAveragingAsianOption> discreteAsian =
166 QuantLib::ext::dynamic_pointer_cast<DiscreteAveragingAsianOption>(qlInstrument);
168 BOOST_CHECK(discreteAsian);
169 BOOST_CHECK_EQUAL(discreteAsian->exercise()->type(), Exercise::Type::European);
170 BOOST_CHECK_EQUAL(discreteAsian->exercise()->dates().size(), 1);
171 BOOST_CHECK_EQUAL(discreteAsian->exercise()->dates()[0], expiry);
173 QuantLib::ext::shared_ptr<TypePayoff> payoff = QuantLib::ext::dynamic_pointer_cast<TypePayoff>(discreteAsian->payoff());
175 BOOST_CHECK_EQUAL(payoff->optionType(), a.type);
177 Real expectedPrice = a.expectedNPV;
180 BOOST_CHECK_SMALL(asianOption->instrument()->NPV() - expectedPrice, 2e-2);
186 BOOST_TEST_MESSAGE(
"Testing parsing of FX Asian option trade from XML");
190 tradeXml.append(
"<Portfolio>");
191 tradeXml.append(
" <Trade id=\"FxAsianOption_USDJPY\">");
192 tradeXml.append(
" <TradeType>FxAsianOption</TradeType>");
193 tradeXml.append(
" <Envelope>");
194 tradeXml.append(
" <CounterParty>CPTY_A</CounterParty>");
195 tradeXml.append(
" <NettingSetId>CPTY_A</NettingSetId>");
196 tradeXml.append(
" <AdditionalFields/>");
197 tradeXml.append(
" </Envelope>");
198 tradeXml.append(
" <FxAsianOptionData>");
199 tradeXml.append(
" <OptionData>");
200 tradeXml.append(
" <LongShort>Long</LongShort>");
201 tradeXml.append(
" <OptionType>Call</OptionType>");
202 tradeXml.append(
" <Style>European</Style>");
203 tradeXml.append(
" <Settlement>Cash</Settlement>");
204 tradeXml.append(
" <PayOffAtExpiry>false</PayOffAtExpiry>");
205 tradeXml.append(
" <PayoffType>Asian</PayoffType>");
206 tradeXml.append(
" <PayoffType2>Arithmetic</PayoffType2>");
207 tradeXml.append(
" <ExerciseDates>");
208 tradeXml.append(
" <ExerciseDate>2021-02-26</ExerciseDate>");
209 tradeXml.append(
" </ExerciseDates>");
210 tradeXml.append(
" </OptionData>");
211 tradeXml.append(
" <ObservationDates>");
212 tradeXml.append(
" <Dates>");
213 tradeXml.append(
" <Dates>");
214 tradeXml.append(
" <Date>2021-02-01</Date>");
215 tradeXml.append(
" <Date>2021-02-02</Date>");
216 tradeXml.append(
" <Date>2021-02-03</Date>");
217 tradeXml.append(
" <Date>2021-02-04</Date>");
218 tradeXml.append(
" <Date>2021-02-05</Date>");
219 tradeXml.append(
" <Date>2021-02-08</Date>");
220 tradeXml.append(
" <Date>2021-02-09</Date>");
221 tradeXml.append(
" <Date>2021-02-10</Date>");
222 tradeXml.append(
" <Date>2021-02-11</Date>");
223 tradeXml.append(
" <Date>2021-02-12</Date>");
224 tradeXml.append(
" <Date>2021-02-15</Date>");
225 tradeXml.append(
" <Date>2021-02-16</Date>");
226 tradeXml.append(
" <Date>2021-02-17</Date>");
227 tradeXml.append(
" <Date>2021-02-18</Date>");
228 tradeXml.append(
" <Date>2021-02-19</Date>");
229 tradeXml.append(
" <Date>2021-02-22</Date>");
230 tradeXml.append(
" <Date>2021-02-23</Date>");
231 tradeXml.append(
" <Date>2021-02-24</Date>");
232 tradeXml.append(
" <Date>2021-02-25</Date>");
233 tradeXml.append(
" <Date>2021-02-26</Date>");
234 tradeXml.append(
" </Dates>");
235 tradeXml.append(
" </Dates>");
236 tradeXml.append(
" </ObservationDates>");
237 tradeXml.append(
" <Name>FX-ECB-USD-JPY</Name>");
238 tradeXml.append(
" <Strike>104.6860</Strike>");
239 tradeXml.append(
" <Quantity>104.6860</Quantity>");
240 tradeXml.append(
" </FxAsianOptionData>");
241 tradeXml.append(
" </Trade>");
242 tradeXml.append(
"</Portfolio>");
249 QuantLib::ext::shared_ptr<Trade> trade = portfolio.
trades().begin()->second;
250 QuantLib::ext::shared_ptr<FxAsianOption> option = QuantLib::ext::dynamic_pointer_cast<ore::data::FxAsianOption>(trade);
251 BOOST_CHECK(option !=
nullptr);
255 BOOST_CHECK_EQUAL(option->tradeType(),
"FxAsianOption");
256 BOOST_CHECK_EQUAL(option->id(),
"FxAsianOption_USDJPY");
258 BOOST_CHECK_EQUAL(option->quantity(), 104.6860);
259 BOOST_CHECK_EQUAL(option->strike().value(), 104.6860);
260 BOOST_CHECK_EQUAL(option->indexName(),
"FX-ECB-USD-JPY");
261 BOOST_CHECK_EQUAL(option->option().longShort(),
"Long");
262 BOOST_CHECK_EQUAL(option->option().callPut(),
"Call");
263 BOOST_CHECK_EQUAL(option->option().style(),
"European");
264 BOOST_CHECK_EQUAL(option->option().exerciseDates().size(), 1);
265 BOOST_CHECK_EQUAL(option->option().exerciseDates()[0],
"2021-02-26");
266 BOOST_CHECK(option->observationDates().hasData());
268 BOOST_CHECK_EQUAL(option->option().payoffType(),
"Asian");
269 BOOST_CHECK_EQUAL(option->option().payoffType2(),
"Arithmetic");
272 BOOST_AUTO_TEST_SUITE_END()
274 BOOST_AUTO_TEST_SUITE_END()
Asian option representation.
Serializable object holding generic trade data, reporting dimensions.
static const string defaultConfiguration
Default configuration label.
map< pair< string, string >, Handle< BlackVolTermStructure > > fxVols_
QuantLib::ext::shared_ptr< FXTriangulation > fx_
map< tuple< string, YieldCurveType, string >, Handle< YieldTermStructure > > yieldCurves_
Serializable object holding option data.
const std::map< std::string, QuantLib::ext::shared_ptr< Trade > > & trades() const
Return the map tradeId -> trade.
Serializable object holding premium data.
Serializable schedule data.
Serializable object holding schedule Dates data.
void fromXMLString(const std::string &xml)
Parse from XML string.
Engine builder for fx Asian options.
An implementation of the Market class that stores the required objects in maps.
std::string to_string(const LocationInfo &l)
BOOST_AUTO_TEST_CASE(testFxAsianOptionTradeBuilding)
string conversion utilities