C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_simple_example_22.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * Simple Example 22 *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <cstdio>
21#include <string>
22
23#include "exprtk.hpp"
24
25
26template <typename T>
28{
29 typedef exprtk::symbol_table<T> symbol_table_t;
30 typedef exprtk::expression<T> expression_t;
31 typedef exprtk::parser<T> parser_t;
32 typedef exprtk::function_compositor<T> compositor_t;
33 typedef typename compositor_t::function function_t;
34
35 const std::string option_implied_volatility_program =
36 " const var epsilon := 0.0000001; "
37 " const var max_iters := 1000; "
38 " "
39 " var vola := 0.5; /* Initial volatility guess */ "
40 " var itr := 0; "
41 " "
42 " while ((itr += 1) <= max_iters) "
43 " { "
44 " var price := "
45 " switch "
46 " { "
47 " case callput_flag == 'call' : bsm_call(s, k, r, t, vola); "
48 " case callput_flag == 'put' : bsm_put (s, k, r, t, vola); "
49 " }; "
50 " "
51 " var price_diff := price - target_price; "
52 " "
53 " if (abs(price_diff) <= epsilon) "
54 " { "
55 " break; "
56 " }; "
57 " "
58 " var vega := bsm_vega(s, k, r, t, vola); "
59 " "
60 " if (vega < epsilon) "
61 " { "
62 " itr := max_iters + 1; "
63 " break; "
64 " }; "
65 " "
66 " vola -= price_diff / vega; "
67 " }; "
68 " "
69 " itr <= max_iters ? vola : null; ";
70
71 T s = T(100.00); // Spot / Stock / Underlying / Base price
72 T k = T(110.00); // Strike price
73 T t = T( 2.22); // Years to maturity
74 T r = T( 0.05); // Risk free rate
75 T target_price = T( 0.00);
76
77 std::string callput_flag;
78
79 symbol_table_t symbol_table(symbol_table_t::e_immutable);
80 symbol_table.add_variable("s",s);
81 symbol_table.add_variable("k",k);
82 symbol_table.add_variable("t",t);
83 symbol_table.add_variable("r",r);
84 symbol_table.add_stringvar("callput_flag",callput_flag);
85 symbol_table.add_variable ("target_price",target_price);
86 symbol_table.add_pi();
87
88 compositor_t compositor(symbol_table);
89
90 compositor.add(
91 function_t("bsm_call")
92 .vars("s", "k", "r", "t", "v")
93 .expression
94 (
95 " var d1 := (log(s / k) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
96 " var d2 := d1 - v * sqrt(t); "
97 " s * ncdf(d1) - k * exp(-r * t) * ncdf(d2); "
98 ));
99
100 compositor.add(
101 function_t("bsm_put")
102 .vars("s", "k", "r", "t", "v")
103 .expression
104 (
105 " var d1 := (log(s / k) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
106 " var d2 := d1 - v * sqrt(t); "
107 " k * exp(-r * t) * ncdf(-d2) - s * ncdf(-d1); "
108 ));
109
110 compositor.add(
111 function_t("bsm_vega")
112 .vars("s", "k", "r", "t", "v")
113 .expression
114 (
115 " var d1 := (log(s / k) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
116 " s * exp(-d1^2 / 2) / sqrt(2pi) * sqrt(t); "
117 ));
118
119 expression_t expression;
120 expression.register_symbol_table(symbol_table);
121
122 parser_t parser;
123 parser.compile(option_implied_volatility_program, expression);
124
125 {
126 callput_flag = "call";
127 target_price = T(18.339502);
128
129 const T call_option_implied_vola = expression.value();
130
131 printf("Call Option(s: %5.3f, k: %5.3f, t: %5.3f, r: %5.3f) "
132 "@ $%8.6f Implied volatility = %10.8f\n",
133 s, k, t, r, target_price, call_option_implied_vola);
134 }
135
136 {
137 callput_flag = "put";
138 target_price = T(16.782764);
139
140 const T put_option_implied_vola = expression.value();
141
142 printf("Put Option(s: %5.3f, k: %5.3f, t: %5.3f, r: %5.3f) "
143 "@ $%8.6f Implied volatility = %10.8f\n",
144 s, k, t, r, target_price, put_option_implied_vola);
145 }
146}
147
148int main()
149{
150 compute_european_option_implied_volatility<double>();
151 return 0;
152}
void compute_european_option_implied_volatility()