Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
equityasianoption.cpp File Reference
#include <boost/test/unit_test.hpp>
#include <oret/toplevelfixture.hpp>
#include <boost/make_shared.hpp>
#include <ql/currencies/america.hpp>
#include <ql/instruments/asianoption.hpp>
#include <ql/math/interpolations/linearinterpolation.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ored/marketdata/marketimpl.hpp>
#include <ored/portfolio/builders/equityasianoption.hpp>
#include <ored/portfolio/optiondata.hpp>
#include <ored/portfolio/asianoption.hpp>
#include <ored/portfolio/portfolio.hpp>
#include <ored/portfolio/schedule.hpp>
#include <ored/utilities/to_string.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testEquityAsianOptionTradeBuilding)
 
 BOOST_AUTO_TEST_CASE (testEquityAsianOptionAverageStrikeTradeBuilding)
 
 BOOST_AUTO_TEST_CASE (testEquityAsianOptionFromXml)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/3]

BOOST_AUTO_TEST_CASE ( testEquityAsianOptionTradeBuilding  )

Definition at line 99 of file equityasianoption.cpp.

99 {
100
101 BOOST_TEST_MESSAGE("Testing equity Asian option trade building with constant vol term structure");
102
103 // Data from "Asian Option", Levy, 1997 in "Exotic Options: The State of the Art",
104 // edited by Clewlow, Strickland
105 // Tests with > 100 fixings are skipped here for speed, QL already tests these
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}};
128
129 Date asof = Date(01, Feb, 2021);
130 Envelope env("CP1");
131 QuantLib::ext::shared_ptr<EngineFactory> engineFactory;
132 QuantLib::ext::shared_ptr<Market> market;
133
134 for (const auto& a : asians) {
135 Time deltaT = a.length / (a.fixings - 1);
136 Date expiry;
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]);
142 }
143 expiry = fixingDates[a.fixings - 1];
144
145 ScheduleDates scheduleDates("NullCalendar", "", "", strFixingDates);
146 ScheduleData scheduleData(scheduleDates);
147
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);
157
158 // Set evaluation date
159 Settings::instance().evaluationDate() = market->asofDate();
160
161 // Test the building of a equity Asian option doesn't throw
162 PremiumData premiumData;
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,
166 boost::none);
167
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));
172
173 // Check the underlying instrument was built as expected
174 QuantLib::ext::shared_ptr<Instrument> qlInstrument = asianOption->instrument()->qlInstrument();
175
176 QuantLib::ext::shared_ptr<DiscreteAveragingAsianOption> discreteAsian =
177 QuantLib::ext::dynamic_pointer_cast<DiscreteAveragingAsianOption>(qlInstrument);
178
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);
183
184 QuantLib::ext::shared_ptr<TypePayoff> payoff = QuantLib::ext::dynamic_pointer_cast<TypePayoff>(discreteAsian->payoff());
185 BOOST_CHECK(payoff);
186 BOOST_CHECK_EQUAL(payoff->optionType(), a.type);
187
188 Real expectedPrice = a.expectedNPV;
189
190 // Check the price
191 BOOST_CHECK_SMALL(asianOption->instrument()->NPV() - expectedPrice, 2e-2);
192 }
193 }
Serializable object holding generic trade data, reporting dimensions.
Definition: envelope.hpp:51
Serializable object holding option data.
Definition: optiondata.hpp:42
Serializable object holding premium data.
Definition: premiumdata.hpp:37
Serializable schedule data.
Definition: schedule.hpp:202
Serializable object holding schedule Dates data.
Definition: schedule.hpp:110
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/3]

BOOST_AUTO_TEST_CASE ( testEquityAsianOptionAverageStrikeTradeBuilding  )

Definition at line 195 of file equityasianoption.cpp.

195 {
196
197 BOOST_TEST_MESSAGE(
198 "Testing equity Asian option trade building with constant vol term structure with average-strike");
199
200 // Data from "Asian Option", Levy, 1997 in "Exotic Options: The State of the Art",
201 // edited by Clewlow, Strickland
202 // Tests with > 100 fixings are skipped here for speed, QL already tests these
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}};
225
226 Date asof = Date(01, Feb, 2021);
227 Envelope env("CP1");
228 QuantLib::ext::shared_ptr<EngineFactory> engineFactory;
229 QuantLib::ext::shared_ptr<Market> market;
230
231 for (const auto& a : asians) {
232 Time deltaT = a.length / (a.fixings - 1);
233 Date expiry;
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]);
239 }
240 expiry = fixingDates[a.fixings - 1];
241
242 ScheduleDates scheduleDates("NullCalendar", "", "", strFixingDates);
243 ScheduleData scheduleData(scheduleDates);
244
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);
256
257 // Set evaluation date
258 Settings::instance().evaluationDate() = market->asofDate();
259
260 // Test the building of a equity Asian option doesn't throw
261 PremiumData premiumData;
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,
265 boost::none);
266
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));
271
272 // Check the underlying instrument was built as expected
273 QuantLib::ext::shared_ptr<Instrument> qlInstrument = asianOption->instrument()->qlInstrument();
274
275 QuantLib::ext::shared_ptr<DiscreteAveragingAsianOption> discreteAsian =
276 QuantLib::ext::dynamic_pointer_cast<DiscreteAveragingAsianOption>(qlInstrument);
277
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);
282
283 QuantLib::ext::shared_ptr<TypePayoff> payoff = QuantLib::ext::dynamic_pointer_cast<TypePayoff>(discreteAsian->payoff());
284 BOOST_CHECK(payoff);
285 BOOST_CHECK_EQUAL(payoff->optionType(), a.type);
286
287 Real expectedPrice = a.expectedNPV;
288
289 // Check the price
290 BOOST_CHECK_SMALL(asianOption->instrument()->NPV() - expectedPrice, 2e-2);
291 }
292 }
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [3/3]

BOOST_AUTO_TEST_CASE ( testEquityAsianOptionFromXml  )

Definition at line 294 of file equityasianoption.cpp.

294 {
295
296 BOOST_TEST_MESSAGE("Testing parsing of equity Asian option trade from XML");
297
298 // Create an XML string representation of the trade
299 string tradeXml;
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>");
357
358 // Load portfolio from XML string
359 Portfolio portfolio;
360 portfolio.fromXMLString(tradeXml);
361
362 // Extract EquityAsianOption trade from 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);
366
367 // Check fields after checking that the cast was successful
368 BOOST_CHECK(option);
369 BOOST_CHECK_EQUAL(option->tradeType(), "EquityAsianOption");
370 BOOST_CHECK_EQUAL(option->id(), "EquityAsianOption_Company");
371 // BOOST_CHECK_EQUAL(option->asset(), "COMPANY"); // only available after build
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());
381
382 BOOST_CHECK_EQUAL(option->option().payoffType(), "Asian");
383 BOOST_CHECK_EQUAL(option->option().payoffType2(), "Arithmetic");
384 }
Serializable portfolio.
Definition: portfolio.hpp:43
const std::map< std::string, QuantLib::ext::shared_ptr< Trade > > & trades() const
Return the map tradeId -> trade.
Definition: portfolio.cpp:162
void fromXMLString(const std::string &xml)
Parse from XML string.
Definition: xmlutils.cpp:162
+ Here is the call graph for this function: