-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFormModel.t.cpp
202 lines (173 loc) · 5.59 KB
/
FormModel.t.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "FormModel.h"
#include <gtest/gtest.h>
#include <Wt/WApplication>
#include <Wt/WEnvironment>
#include <Wt/WObject>
#include <Wt/WStringListModel>
#include "StringSetModel.h"
namespace { using opcalc::FormModel; }
TEST(FormModel, instantiation)
{
FormModel model(nullptr);
ASSERT_EQ(15, model.fields().size());
ASSERT_EQ(2, model.callPutModel->stringList().size());
ASSERT_EQ(4, model. engineModel->stringList().size());
ASSERT_EQ(4, model.processModel->stringList().size());
ASSERT_FALSE(model.isVisible(FormModel::DeltaField));
ASSERT_FALSE(model.isVisible(FormModel::GammaField));
ASSERT_FALSE(model.isVisible(FormModel::ThetaField));
ASSERT_FALSE(model.isVisible(FormModel::VegaField));
ASSERT_FALSE(model.isVisible(FormModel::RhoField));
}
TEST(FormModel, engineFieldSticks)
{
FormModel model(NULL);
model.setValue(
FormModel::EngineField,
model.engineModel->stringList()[0]);
ASSERT_EQ(
model.engineModel->stringList()[0],
boost::any_cast<Wt::WString>(
model.value(FormModel::EngineField) ) );
}
TEST(FormModel, spotFieldSticks)
{
double value=100;
FormModel model(nullptr);
model.setValue(FormModel::SpotField, double(value));
ASSERT_EQ(
value,
boost::any_cast<double>(
model.value(FormModel::SpotField) ) );
}
TEST(FormModel, dividendFieldSticks)
{
double value=0.3;
FormModel model(nullptr);
model.setValue(FormModel::DividendField, value);
ASSERT_EQ(
value,
boost::any_cast<double>(
model.value(FormModel::DividendField) ) );
}
TEST(FormModel, interestFieldSticks)
{
double value=0.02;
FormModel model(nullptr);
model.setValue(FormModel::InterestField, value);
ASSERT_EQ(
value,
boost::any_cast<double>(
model.value(FormModel::InterestField) ) );
}
TEST(FormModel, volatilityFieldSticks)
{
double value=0.3;
FormModel model(nullptr);
model.setValue(FormModel::VolatilityField, value);
ASSERT_EQ(
value,
boost::any_cast<double>(
model.value(FormModel::VolatilityField) ) );
}
TEST(FormModel, strikeFieldSticks)
{
double value=125;
FormModel model(nullptr);
model.setValue(FormModel::StrikeField, value);
ASSERT_EQ(
value,
boost::any_cast<double>(
model.value(FormModel::StrikeField) ) );
}
TEST(FormModel, termFieldSticks)
{
double value=0.5;
FormModel model(nullptr);
model.setValue(FormModel::TermField, value);
ASSERT_EQ(
value,
boost::any_cast<double>(
model.value(FormModel::TermField) ) );
}
TEST(FormModel, callPutFieldSticks)
{
FormModel model(nullptr);
model.setValue(FormModel::CallPutField, std::string("Call"));
ASSERT_EQ(
std::string("Call"),
boost::any_cast<std::string>(
model.value(FormModel::CallPutField) ) );
model.setValue(FormModel::CallPutField, std::string("Put"));
ASSERT_EQ(
std::string("Put"),
boost::any_cast<std::string>(
model.value(FormModel::CallPutField) ) );
}
TEST(FormModel, valuesFromLiterature)
{
/* test case taken from QuantLib test suite, which itself cites "the
* literature" as the source of these values */
FormModel model(nullptr);
model.setValue(FormModel::SpotField, 90.0);
model.setValue(FormModel::DividendField, 0.1);
model.setValue(FormModel::InterestField, 0.1);
model.setValue(FormModel::VolatilityField, 0.15);
model.setValue(FormModel::StrikeField, 100.0);
model.setValue(FormModel::TermField, 0.1);
model.setValue(FormModel::CallPutField, std::string("Call"));
model.calculate();
EXPECT_NEAR(
0.0206,
boost::any_cast<double>(model.value(FormModel::ResultField)),
3.0e-3);
}
TEST(FormModel, valuesFromBloomberg)
{
// test case values taken from OVME on the Bloomberg Terminal
FormModel model(nullptr);
model.setValue(FormModel::SpotField, 90.0);
model.setValue(FormModel::DividendField, 0.1);
model.setValue(FormModel::InterestField, 0.1);
model.setValue(FormModel::VolatilityField, 0.15);
model.setValue(FormModel::StrikeField, 100.0);
model.setValue(FormModel::TermField, 0.1);
model.setValue(FormModel::CallPutField, std::string("Put"));
// select an engine that supports delta & gamma
model.setValue(
FormModel::EngineField,
Wt::WString("Finite-differences pricing engine for American one"
" asset options, using Crank-Nicolson scheme"));
model.calculate();
EXPECT_NEAR(
10.04,
boost::any_cast<double>(model.value(FormModel::ResultField)),
0.04);
ASSERT_TRUE(model.isVisible(FormModel::DeltaField));
EXPECT_NEAR(
-0.9943,
boost::any_cast<double>(model.value(FormModel::DeltaField)),
0.0026);
ASSERT_TRUE(model.isVisible(FormModel::GammaField));
EXPECT_NEAR(
0.00691,
boost::any_cast<double>(model.value(FormModel::GammaField)),
0.0037); // quite far off!
}
TEST(FormModel, atTheMoneyCallDelta)
{
// a simple sanity check
FormModel model(NULL);
model.setValue(
FormModel::EngineField,
Wt::WString("Finite-differences pricing engine for American one"
" asset options, using Crank-Nicolson scheme"));
model.setValue(FormModel::SpotField, 90.0);
model.setValue(FormModel::StrikeField, 90.0);
model.setValue(FormModel::CallPutField, std::string("Call"));
model.calculate();
EXPECT_NEAR(
0.5,
boost::any_cast<double>(model.value(FormModel::DeltaField)),
0.01);
}