-
Notifications
You must be signed in to change notification settings - Fork 3
/
quantlib.cpp
134 lines (117 loc) · 5.06 KB
/
quantlib.cpp
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
#include "quantlib.h"
#include <boost/shared_ptr.hpp>
#include <ql/exercise.hpp>
#include <ql/experimental/processes/extendedblackscholesprocess.hpp>
#include <ql/experimental/processes/vegastressedblackscholesprocess.hpp>
#include <ql/instruments/vanillaoption.hpp>
#include <ql/pricingengines/vanilla/baroneadesiwhaleyengine.hpp>
#include <ql/pricingengines/vanilla/bjerksundstenslandengine.hpp>
#include <ql/pricingengines/vanilla/fdamericanengine.hpp>
#include <ql/pricingengines/vanilla/fddividendamericanengine.hpp>
#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/calendars/nullcalendar.hpp>
#include <ql/time/daycounters/actual360.hpp>
namespace opcalc {
namespace quantlib {
QuantLib::VanillaOption value(
std::string const& engine,
std::string const& process,
OptionInput const& input)
{
using namespace QuantLib;
Date today = Date::todaysDate();
DayCounter dc = Actual360();
boost::shared_ptr<SimpleQuote> spot(new SimpleQuote(input.spot));
boost::shared_ptr<YieldTermStructure>
dividendTermStruct(
new FlatForward(
today,
Handle<Quote>(
boost::shared_ptr<SimpleQuote>(
new SimpleQuote(input.dividend) ) ),
dc) );
boost::shared_ptr<YieldTermStructure>
riskFreeRateTermStruct(
new FlatForward(
today,
Handle<Quote>(
boost::shared_ptr<SimpleQuote>(
new SimpleQuote(input.riskFreeRate) ) ),
dc) );
boost::shared_ptr<BlackVolTermStructure>
volTS(
new BlackConstantVol(
today,
NullCalendar(),
Handle<Quote>(
boost::shared_ptr<SimpleQuote>(
new SimpleQuote(input.volatility) ) ),
dc) );
boost::shared_ptr<StrikedTypePayoff> payoff(
new PlainVanillaPayoff(input.type, input.strike));
// FLOATING_POINT_EXCEPTION
boost::shared_ptr<Exercise>
exercise(
new AmericanExercise(
today, today + Integer(input.timeToMaturity*360+0.5) ) );
boost::shared_ptr<GeneralizedBlackScholesProcess> pStochProcess;
if ( process == "BlackScholesMerton" )
pStochProcess
= boost::shared_ptr<GeneralizedBlackScholesProcess>(
new BlackScholesMertonProcess(
Handle<Quote>(spot),
Handle<YieldTermStructure>(dividendTermStruct),
Handle<YieldTermStructure>(riskFreeRateTermStruct),
Handle<BlackVolTermStructure>(volTS)));
else if ( process == "ExtendedBlackScholesMerton" )
pStochProcess
= boost::shared_ptr<GeneralizedBlackScholesProcess>(
new ExtendedBlackScholesMertonProcess(
Handle<Quote>(spot),
Handle<YieldTermStructure>(dividendTermStruct),
Handle<YieldTermStructure>(riskFreeRateTermStruct),
Handle<BlackVolTermStructure>(volTS)));
else if ( process == "GarmanKohlagen" )
pStochProcess
= boost::shared_ptr<GeneralizedBlackScholesProcess>(
new GarmanKohlagenProcess(
Handle<Quote>(spot),
Handle<YieldTermStructure>(dividendTermStruct),
Handle<YieldTermStructure>(riskFreeRateTermStruct),
Handle<BlackVolTermStructure>(volTS)));
else if ( process == "VegaStressedBlackScholesProcess" )
pStochProcess
= boost::shared_ptr<GeneralizedBlackScholesProcess>(
new VegaStressedBlackScholesProcess(
Handle<Quote>(spot),
Handle<YieldTermStructure>(dividendTermStruct),
Handle<YieldTermStructure>(riskFreeRateTermStruct),
Handle<BlackVolTermStructure>(volTS)));
else
throw std::runtime_error("Unknown stochastic process type '"
+process+"'");
boost::shared_ptr<PricingEngine> pEngine;
if ( engine == "BaroneAdesiWhaley" )
pEngine =
boost::shared_ptr<PricingEngine>(
new BaroneAdesiWhaleyApproximationEngine(pStochProcess));
else if ( engine == "FDAmericanCrankNicolson" )
pEngine =
boost::shared_ptr<PricingEngine>(
new FDAmericanEngine<CrankNicolson>(pStochProcess));
else if ( engine == "FDDividendAmericanCrankNicolson" )
pEngine =
boost::shared_ptr<PricingEngine>(
new FDDividendAmericanEngine<CrankNicolson>(pStochProcess));
else if ( engine == "BjerksundStensland" )
pEngine =
boost::shared_ptr<PricingEngine>(
new BjerksundStenslandApproximationEngine(pStochProcess));
else
throw std::runtime_error("Unknown engine type '"+engine+"'");
VanillaOption option(payoff, exercise);
option.setPricingEngine(pEngine);
return option;
}
}}