/src/quantlib/fuzz-test-suite/fuzzeuropeanoption.cpp
Line | Count | Source |
1 | | /* |
2 | | Copyright (C) 2026 David Korczynski |
3 | | |
4 | | This file is part of QuantLib, a free-software/open-source library |
5 | | for financial quantitative analysts and developers - http://quantlib.org/ |
6 | | |
7 | | QuantLib is free software: you can redistribute it and/or modify it |
8 | | under the terms of the QuantLib license. You should have received a |
9 | | copy of the license along with this program; if not, please email |
10 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
11 | | <https://www.quantlib.org/license.shtml>. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, but WITHOUT |
14 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
15 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
16 | | */ |
17 | | |
18 | | #include <ql/exercise.hpp> |
19 | | #include <ql/instruments/vanillaoption.hpp> |
20 | | #include <ql/pricingengines/vanilla/analyticeuropeanengine.hpp> |
21 | | #include <ql/pricingengines/vanilla/fdblackscholesvanillaengine.hpp> |
22 | | #include <ql/pricingengines/vanilla/integralengine.hpp> |
23 | | #include <ql/settings.hpp> |
24 | | #include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp> |
25 | | #include <ql/termstructures/yield/flatforward.hpp> |
26 | | #include <ql/time/calendars/nullcalendar.hpp> |
27 | | #include <ql/time/daycounters/actual360.hpp> |
28 | | #include <fuzzer/FuzzedDataProvider.h> |
29 | | |
30 | | using namespace QuantLib; |
31 | | |
32 | 916 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
33 | 916 | FuzzedDataProvider fdp(data, size); |
34 | 916 | SavedSettings saved_settings; |
35 | | |
36 | 916 | try { |
37 | 916 | auto type = fdp.PickValueInArray({Option::Put, Option::Call}); |
38 | 916 | Real strike = fdp.ConsumeFloatingPointInRange<Real>(1.0, 500.0); |
39 | 916 | Real spot = fdp.ConsumeFloatingPointInRange<Real>(1.0, 500.0); |
40 | 916 | Rate r = fdp.ConsumeFloatingPointInRange<Real>(-0.02, 0.15); |
41 | 916 | Rate q = fdp.ConsumeFloatingPointInRange<Real>(0.0, 0.10); |
42 | 916 | Volatility vol = fdp.ConsumeFloatingPointInRange<Volatility>(0.01, 3.0); |
43 | 916 | auto maturityDays = fdp.ConsumeIntegralInRange<int>(7, 3650); |
44 | 916 | auto engineChoice = fdp.ConsumeIntegralInRange<int>(0, 2); |
45 | | |
46 | 916 | Date today(15, January, 2025); |
47 | 916 | Settings::instance().evaluationDate() = today; |
48 | 916 | DayCounter dc = Actual360(); |
49 | | |
50 | 916 | auto spotQuote = ext::make_shared<SimpleQuote>(spot); |
51 | 916 | auto rTS = ext::make_shared<FlatForward>(today, r, dc); |
52 | 916 | auto qTS = ext::make_shared<FlatForward>(today, q, dc); |
53 | 916 | auto volTS = ext::make_shared<BlackConstantVol>( |
54 | 916 | today, NullCalendar(), vol, dc); |
55 | | |
56 | 916 | auto bsmProcess = ext::make_shared<BlackScholesMertonProcess>( |
57 | 916 | Handle<Quote>(spotQuote), |
58 | 916 | Handle<YieldTermStructure>(qTS), |
59 | 916 | Handle<YieldTermStructure>(rTS), |
60 | 916 | Handle<BlackVolTermStructure>(volTS)); |
61 | | |
62 | | // Select pricing engine |
63 | 916 | ext::shared_ptr<PricingEngine> engine; |
64 | 916 | switch (engineChoice) { |
65 | 189 | case 0: |
66 | 189 | engine = ext::make_shared<AnalyticEuropeanEngine>(bsmProcess); |
67 | 189 | break; |
68 | 662 | case 1: |
69 | 662 | engine = ext::make_shared<FdBlackScholesVanillaEngine>( |
70 | 662 | bsmProcess, 25, 25); |
71 | 662 | break; |
72 | 65 | case 2: |
73 | 65 | engine = ext::make_shared<IntegralEngine>(bsmProcess); |
74 | 65 | break; |
75 | 916 | } |
76 | | |
77 | 916 | auto payoff = ext::make_shared<PlainVanillaPayoff>(type, strike); |
78 | 916 | Date exDate = today + maturityDays; |
79 | 916 | auto exercise = ext::make_shared<EuropeanExercise>(exDate); |
80 | | |
81 | 916 | VanillaOption option(payoff, exercise); |
82 | 916 | option.setPricingEngine(engine); |
83 | | |
84 | 916 | (void)option.NPV(); |
85 | 916 | (void)option.delta(); |
86 | 916 | (void)option.gamma(); |
87 | 916 | (void)option.vega(); |
88 | 916 | (void)option.theta(); |
89 | 916 | (void)option.rho(); |
90 | | |
91 | 916 | } catch (const std::exception&) { |
92 | 727 | } |
93 | 916 | return 0; |
94 | 916 | } |