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>
36 using namespace boost::unit_test_framework;
37 using namespace boost::algorithm;
46 TestMarket(
const Real spot,
const Date& expiry,
const Rate riskFreeRate,
const Rate dividendYield,
47 const Volatility flatVolatility)
50 asof_ = Date(01, Feb, 2021);
52 DayCounter dayCounter = Actual360();
55 Handle<YieldTermStructure> discount(QuantLib::ext::make_shared<FlatForward>(asof_, riskFreeRate, dayCounter));
60 Handle<YieldTermStructure> dividendYTS(QuantLib::ext::make_shared<FlatForward>(asof_, dividendYield, dayCounter));
65 Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(spot));
71 yieldCurve(YieldCurveType::EquityDividend,
"COMPANY")));
74 Handle<BlackVolTermStructure> volatility(
75 QuantLib::ext::make_shared<BlackConstantVol>(asof_, TARGET(), flatVolatility, dayCounter));
80 struct DiscreteAsianTestData {
95 BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
97 BOOST_AUTO_TEST_SUITE(EquityAsianOptionTests)
101 BOOST_TEST_MESSAGE(
"Testing equity Asian option trade building with constant vol term structure");
106 std::vector<DiscreteAsianTestData> asians = {
107 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 2, 0.13, 1.3942835683},
108 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 4, 0.13, 1.5852442983},
109 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 8, 0.13, 1.66970673},
110 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 12, 0.13, 1.6980019214},
111 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 26, 0.13, 1.7255070456},
112 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 52, 0.13, 1.7401553533},
113 {Option::Put, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 100, 0.13, 1.7478303712},
114 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 2, 0.13, 1.8496053697},
115 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 4, 0.13, 2.0111495205},
116 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 8, 0.13, 2.0852138818},
117 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 12, 0.13, 2.1105094397},
118 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 26, 0.13, 2.1346526695},
119 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 52, 0.13, 2.147489651},
120 {Option::Put, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 100, 0.13, 2.154728109},
121 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 2, 0.13, 2.63315092584},
122 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 4, 0.13, 2.76723962361},
123 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 8, 0.13, 2.83124836881},
124 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 12, 0.13, 2.84290301412},
125 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 26, 0.13, 2.88179560417},
126 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 52, 0.13, 2.88447044543},
127 {Option::Put, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 100, 0.13, 2.89985329603}};
129 Date asof = Date(01, Feb, 2021);
131 QuantLib::ext::shared_ptr<EngineFactory> engineFactory;
132 QuantLib::ext::shared_ptr<Market> market;
134 for (
const auto& a : asians) {
135 Time deltaT = a.length / (a.fixings - 1);
137 vector<Date> fixingDates(a.fixings);
138 vector<std::string> strFixingDates(a.fixings);
139 for (Size i = 0; i < a.fixings; ++i) {
140 fixingDates[i] = (asof +
static_cast<Integer
>((a.firstFixing + i * deltaT) * 360 + 0.5));
141 strFixingDates[i] =
to_string(fixingDates[i]);
143 expiry = fixingDates[a.fixings - 1];
145 ScheduleDates scheduleDates(
"NullCalendar",
"",
"", strFixingDates);
148 market = QuantLib::ext::make_shared<TestMarket>(a.spot, expiry, a.riskFreeRate, a.dividendYield, a.volatility);
149 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
150 std::string productName =
"EquityAsianOptionArithmeticPrice";
151 engineData->model(productName) =
"BlackScholesMerton";
152 engineData->engine(productName) =
"MCDiscreteArithmeticAPEngine";
153 engineData->engineParameters(productName) = {{
"ProcessType",
"Discrete"}, {
"BrownianBridge",
"True"},
154 {
"AntitheticVariate",
"False"}, {
"ControlVariate",
"True"},
155 {
"RequiredSamples",
"2047"}, {
"Seed",
"0"}};
156 engineFactory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
159 Settings::instance().evaluationDate() = market->asofDate();
163 OptionData optionData(
"Long",
to_string(a.type),
"European",
true, {to_string(expiry)},
"Cash",
"",
164 premiumData, vector<Real>(), vector<Real>(),
"",
"",
"", vector<string>(),
165 vector<string>(),
"",
"",
"",
"Asian",
"Arithmetic", boost::none, boost::none,
168 QuantLib::ext::shared_ptr<EquityAsianOption> asianOption = QuantLib::ext::make_shared<EquityAsianOption>(
169 env,
"EquityAsianOption", 1.0,
TradeStrike(a.strike,
"USD"), optionData, scheduleData,
170 QuantLib::ext::make_shared<EquityUnderlying>(
"COMPANY"), Date(),
"USD");
171 BOOST_CHECK_NO_THROW(asianOption->build(engineFactory));
174 QuantLib::ext::shared_ptr<Instrument> qlInstrument = asianOption->instrument()->qlInstrument();
176 QuantLib::ext::shared_ptr<DiscreteAveragingAsianOption> discreteAsian =
177 QuantLib::ext::dynamic_pointer_cast<DiscreteAveragingAsianOption>(qlInstrument);
179 BOOST_CHECK(discreteAsian);
180 BOOST_CHECK_EQUAL(discreteAsian->exercise()->type(), Exercise::Type::European);
181 BOOST_CHECK_EQUAL(discreteAsian->exercise()->dates().size(), 1);
182 BOOST_CHECK_EQUAL(discreteAsian->exercise()->dates()[0], expiry);
184 QuantLib::ext::shared_ptr<TypePayoff> payoff = QuantLib::ext::dynamic_pointer_cast<TypePayoff>(discreteAsian->payoff());
186 BOOST_CHECK_EQUAL(payoff->optionType(), a.type);
188 Real expectedPrice = a.expectedNPV;
191 BOOST_CHECK_SMALL(asianOption->instrument()->NPV() - expectedPrice, 2e-2);
198 "Testing equity Asian option trade building with constant vol term structure with average-strike");
203 std::vector<DiscreteAsianTestData> asians = {
204 {Option::Call, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 2, 0.13, 1.51917595129},
205 {Option::Call, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 4, 0.13, 1.67940165674},
206 {Option::Call, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 8, 0.13, 1.75371215251},
207 {Option::Call, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 12, 0.13, 1.77595318693},
208 {Option::Call, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 26, 0.13, 1.81430536630},
209 {Option::Call, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 52, 0.13, 1.82269246898},
210 {Option::Call, 90.0, 87.0, 0.06, 0.025, 0.0, 11.0 / 12.0, 100, 0.13, 1.83822402464},
211 {Option::Call, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 2, 0.13, 1.51154400089},
212 {Option::Call, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 4, 0.13, 1.67103508506},
213 {Option::Call, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 8, 0.13, 1.74529684070},
214 {Option::Call, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 12, 0.13, 1.76667074564},
215 {Option::Call, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 26, 0.13, 1.80528400613},
216 {Option::Call, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 52, 0.13, 1.81400883891},
217 {Option::Call, 90.0, 87.0, 0.06, 0.025, 1.0 / 12.0, 11.0 / 12.0, 100, 0.13, 1.82922901451},
218 {Option::Call, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 2, 0.13, 1.49648170891},
219 {Option::Call, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 4, 0.13, 1.65443100462},
220 {Option::Call, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 8, 0.13, 1.72817806731},
221 {Option::Call, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 12, 0.13, 1.74877367895},
222 {Option::Call, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 26, 0.13, 1.78733801988},
223 {Option::Call, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 52, 0.13, 1.79624826757},
224 {Option::Call, 90.0, 87.0, 0.06, 0.025, 3.0 / 12.0, 11.0 / 12.0, 100, 0.13, 1.81114186876}};
226 Date asof = Date(01, Feb, 2021);
228 QuantLib::ext::shared_ptr<EngineFactory> engineFactory;
229 QuantLib::ext::shared_ptr<Market> market;
231 for (
const auto& a : asians) {
232 Time deltaT = a.length / (a.fixings - 1);
234 vector<Date> fixingDates(a.fixings);
235 vector<std::string> strFixingDates(a.fixings);
236 for (Size i = 0; i < a.fixings; ++i) {
237 fixingDates[i] = (asof +
static_cast<Integer
>((a.firstFixing + i * deltaT) * 360 + 0.5));
238 strFixingDates[i] =
to_string(fixingDates[i]);
240 expiry = fixingDates[a.fixings - 1];
242 ScheduleDates scheduleDates(
"NullCalendar",
"",
"", strFixingDates);
245 market = QuantLib::ext::make_shared<TestMarket>(a.spot, expiry, a.riskFreeRate, a.dividendYield, a.volatility);
246 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
247 std::string productName =
"EquityAsianOptionArithmeticStrike";
248 engineData->model(productName) =
"BlackScholesMerton";
249 engineData->engine(productName) =
"MCDiscreteArithmeticASEngine";
250 engineData->engineParameters(productName) = {{
"ProcessType",
"Discrete"},
251 {
"BrownianBridge",
"True"},
252 {
"AntitheticVariate",
"False"},
253 {
"RequiredSamples",
"1000"},
254 {
"Seed",
"3456789"}};
255 engineFactory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
258 Settings::instance().evaluationDate() = market->asofDate();
262 OptionData optionData(
"Long",
to_string(a.type),
"European",
true, {to_string(expiry)},
"Cash",
"",
263 premiumData, vector<Real>(), vector<Real>(),
"",
"",
"", vector<string>(),
264 vector<string>(),
"",
"",
"",
"AverageStrike",
"Arithmetic", boost::none, boost::none,
267 QuantLib::ext::shared_ptr<EquityAsianOption> asianOption = QuantLib::ext::make_shared<EquityAsianOption>(
268 env,
"EquityAsianOption", 1.0,
TradeStrike(a.strike,
"USD"), optionData, scheduleData,
269 QuantLib::ext::make_shared<EquityUnderlying>(
"COMPANY"), Date(),
"USD");
270 BOOST_CHECK_NO_THROW(asianOption->build(engineFactory));
273 QuantLib::ext::shared_ptr<Instrument> qlInstrument = asianOption->instrument()->qlInstrument();
275 QuantLib::ext::shared_ptr<DiscreteAveragingAsianOption> discreteAsian =
276 QuantLib::ext::dynamic_pointer_cast<DiscreteAveragingAsianOption>(qlInstrument);
278 BOOST_CHECK(discreteAsian);
279 BOOST_CHECK_EQUAL(discreteAsian->exercise()->type(), Exercise::Type::European);
280 BOOST_CHECK_EQUAL(discreteAsian->exercise()->dates().size(), 1);
281 BOOST_CHECK_EQUAL(discreteAsian->exercise()->dates()[0], expiry);
283 QuantLib::ext::shared_ptr<TypePayoff> payoff = QuantLib::ext::dynamic_pointer_cast<TypePayoff>(discreteAsian->payoff());
285 BOOST_CHECK_EQUAL(payoff->optionType(), a.type);
287 Real expectedPrice = a.expectedNPV;
290 BOOST_CHECK_SMALL(asianOption->instrument()->NPV() - expectedPrice, 2e-2);
296 BOOST_TEST_MESSAGE(
"Testing parsing of equity Asian option trade from XML");
300 tradeXml.append(
"<Portfolio>");
301 tradeXml.append(
" <Trade id=\"EquityAsianOption_Company\">");
302 tradeXml.append(
" <TradeType>EquityAsianOption</TradeType>");
303 tradeXml.append(
" <Envelope>");
304 tradeXml.append(
" <CounterParty>CPTY_A</CounterParty>");
305 tradeXml.append(
" <NettingSetId>CPTY_A</NettingSetId>");
306 tradeXml.append(
" <AdditionalFields/>");
307 tradeXml.append(
" </Envelope>");
308 tradeXml.append(
" <EquityAsianOptionData>");
309 tradeXml.append(
" <OptionData>");
310 tradeXml.append(
" <LongShort>Long</LongShort>");
311 tradeXml.append(
" <OptionType>Call</OptionType>");
312 tradeXml.append(
" <Style>European</Style>");
313 tradeXml.append(
" <Settlement>Cash</Settlement>");
314 tradeXml.append(
" <PayOffAtExpiry>false</PayOffAtExpiry>");
315 tradeXml.append(
" <PayoffType>Asian</PayoffType>");
316 tradeXml.append(
" <PayoffType2>Arithmetic</PayoffType2>");
317 tradeXml.append(
" <ExerciseDates>");
318 tradeXml.append(
" <ExerciseDate>2021-02-26</ExerciseDate>");
319 tradeXml.append(
" </ExerciseDates>");
320 tradeXml.append(
" </OptionData>");
321 tradeXml.append(
" <ObservationDates>");
322 tradeXml.append(
" <Dates>");
323 tradeXml.append(
" <Dates>");
324 tradeXml.append(
" <Date>2021-02-01</Date>");
325 tradeXml.append(
" <Date>2021-02-02</Date>");
326 tradeXml.append(
" <Date>2021-02-03</Date>");
327 tradeXml.append(
" <Date>2021-02-04</Date>");
328 tradeXml.append(
" <Date>2021-02-05</Date>");
329 tradeXml.append(
" <Date>2021-02-08</Date>");
330 tradeXml.append(
" <Date>2021-02-09</Date>");
331 tradeXml.append(
" <Date>2021-02-10</Date>");
332 tradeXml.append(
" <Date>2021-02-11</Date>");
333 tradeXml.append(
" <Date>2021-02-12</Date>");
334 tradeXml.append(
" <Date>2021-02-15</Date>");
335 tradeXml.append(
" <Date>2021-02-16</Date>");
336 tradeXml.append(
" <Date>2021-02-17</Date>");
337 tradeXml.append(
" <Date>2021-02-18</Date>");
338 tradeXml.append(
" <Date>2021-02-19</Date>");
339 tradeXml.append(
" <Date>2021-02-22</Date>");
340 tradeXml.append(
" <Date>2021-02-23</Date>");
341 tradeXml.append(
" <Date>2021-02-24</Date>");
342 tradeXml.append(
" <Date>2021-02-25</Date>");
343 tradeXml.append(
" <Date>2021-02-26</Date>");
344 tradeXml.append(
" </Dates>");
345 tradeXml.append(
" </Dates>");
346 tradeXml.append(
" </ObservationDates>");
347 tradeXml.append(
" <Underlying>");
348 tradeXml.append(
" <Type>Equity</Type>");
349 tradeXml.append(
" <Name>COMPANY</Name>");
350 tradeXml.append(
" </Underlying>");
351 tradeXml.append(
" <Currency>USD</Currency>");
352 tradeXml.append(
" <Strike>2270</Strike>");
353 tradeXml.append(
" <Quantity>1</Quantity>");
354 tradeXml.append(
" </EquityAsianOptionData>");
355 tradeXml.append(
" </Trade>");
356 tradeXml.append(
"</Portfolio>");
363 QuantLib::ext::shared_ptr<Trade> trade = portfolio.
trades().begin()->second;
364 QuantLib::ext::shared_ptr<EquityAsianOption> option = QuantLib::ext::dynamic_pointer_cast<ore::data::EquityAsianOption>(trade);
365 BOOST_CHECK(option !=
nullptr);
369 BOOST_CHECK_EQUAL(option->tradeType(),
"EquityAsianOption");
370 BOOST_CHECK_EQUAL(option->id(),
"EquityAsianOption_Company");
372 BOOST_CHECK_EQUAL(option->payCurrency(),
"USD");
373 BOOST_CHECK_EQUAL(option->strike().value(), 2270);
374 BOOST_CHECK_EQUAL(option->quantity(), 1);
375 BOOST_CHECK_EQUAL(option->option().longShort(),
"Long");
376 BOOST_CHECK_EQUAL(option->option().callPut(),
"Call");
377 BOOST_CHECK_EQUAL(option->option().style(),
"European");
378 BOOST_CHECK_EQUAL(option->option().exerciseDates().size(), 1);
379 BOOST_CHECK_EQUAL(option->option().exerciseDates()[0],
"2021-02-26");
380 BOOST_CHECK(option->observationDates().hasData());
382 BOOST_CHECK_EQUAL(option->option().payoffType(),
"Asian");
383 BOOST_CHECK_EQUAL(option->option().payoffType2(),
"Arithmetic");
386 BOOST_AUTO_TEST_SUITE_END()
388 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 >, QuantLib::Handle< QuantExt::EquityIndex2 > > equityCurves_
map< tuple< string, YieldCurveType, string >, Handle< YieldTermStructure > > yieldCurves_
map< pair< string, string >, Handle< Quote > > equitySpots_
Handle< Quote > equitySpot(const string &eqName, const string &configuration=Market::defaultConfiguration) const override
Equity curves.
map< pair< string, string >, Handle< BlackVolTermStructure > > equityVols_
Handle< YieldTermStructure > yieldCurve(const YieldCurveType &type, const string &ccy, const string &configuration=Market::defaultConfiguration) const override
Yield Curves.
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 equity Asian options.
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
An implementation of the Market class that stores the required objects in maps.
std::string to_string(const LocationInfo &l)
trade option data model and serialization
trade schedule data model and serialization
BOOST_AUTO_TEST_CASE(testEquityAsianOptionTradeBuilding)
string conversion utilities