-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_interface.cpp
127 lines (115 loc) · 4.48 KB
/
matrix_interface.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
// matrix_interface.cpp
//
// Implementation of matrix interface
//
// @author Mark Bogorad
// @version 1.0
#include "matrix_interface.hpp"
#include <iostream>
#include <iomanip> // For formatted output
#include <memory> // For smart pointers
matrix_interface::matrix_interface(const std::string& variable_to_vary, double begin, double end, double h)
: variable_to_vary(variable_to_vary) {
// Hardcoded values for other parameters
spot = 60.0;
strike = 65.0;
rate = 0.08;
volatility = 0.30;
maturity = 0.25; // 1 year
cost_of_carry = 0.08;
option_type = 1; // 1: European, 2: American, 3: Asian
call_put_type = 2; // 1: Call, 2: Put
nSimulations = 10000;
nTimeSteps = 252;
// Generate varying values
generate_varying_values(begin, end, h);
}
void matrix_interface::generate_varying_values(double begin, double end, double h) {
for (double value = begin; value <= end; value += h) {
varying_values.push_back(value);
}
}
void matrix_interface::set_parameters(double value) {
if (variable_to_vary == "spot") {
spot = value;
} else if (variable_to_vary == "strike") {
strike = value;
} else if (variable_to_vary == "rate") {
rate = value;
} else if (variable_to_vary == "volatility") {
volatility = value;
} else if (variable_to_vary == "maturity") {
maturity = value;
} else if (variable_to_vary == "cost_of_carry") {
cost_of_carry = value;
}
}
void matrix_interface::console_pricing() {
results_matrix.clear();
american_results_matrix.clear();
for (double value : varying_values) {
set_parameters(value);
std::unique_ptr<option> opt;
double price = 0.0;
if (option_type == 1) { // European
auto european_opt = std::make_unique<european_option>(spot, strike, rate, maturity, volatility, cost_of_carry, (call_put_type == 1) ? option::CALL : option::PUT);
price = european_opt->price();
double delta = european_opt->delta();
double gamma = european_opt->gamma();
double vega = european_opt->vega();
double theta = european_opt->theta();
double rho = european_opt->rho();
double pcp_price = 0.0;
if (call_put_type == 1) {
pcp_price = european_opt->pcp_put_price(price);
} else {
pcp_price = european_opt->pcp_call_price(price);
}
results_matrix.push_back({value, price, delta, gamma, vega, theta, rho, pcp_price});
} else if (option_type == 2) { // American
auto american_opt = std::make_unique<american_option>(spot, strike, rate, volatility, cost_of_carry, (call_put_type == 1) ? option::CALL : option::PUT);
price = american_opt->price();
american_results_matrix.push_back({value, price});
} else if (option_type == 3) { // Asian
auto asian_opt = std::make_unique<asian_option>(spot, strike, rate, maturity, volatility, cost_of_carry, (call_put_type == 1) ? option::CALL : option::PUT, nSimulations, nTimeSteps);
price = asian_opt->price();
results_matrix.push_back({value, price});
} else {
std::cerr << "Invalid option type selected." << std::endl;
return;
}
}
print_results_matrix();
}
void matrix_interface::print_results_matrix() {
if (option_type == 2) { // American option
std::cout << std::setw(15) << "Varying Value"
<< std::setw(15) << "Option Price"
<< std::endl;
for (const auto& row : american_results_matrix) {
for (double val : row) {
std::cout << std::setw(15) << val;
}
std::cout << std::endl;
}
} else { // European and Asian options
std::cout << std::setw(15) << "Varying Value"
<< std::setw(15) << "Option Price"
<< std::setw(15) << "Delta"
<< std::setw(15) << "Gamma"
<< std::setw(15) << "Vega"
<< std::setw(15) << "Theta"
<< std::setw(15) << "Rho"
<< std::setw(15) << "PCP Price"
<< std::endl;
for (const auto& row : results_matrix) {
for (double val : row) {
std::cout << std::setw(15) << val;
}
std::cout << std::endl;
}
}
}
void matrix_interface::display_results() {
console_pricing();
}