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 implied_volatility_program =
36 " const var epsilon := 0.0000001; "
37 " const var max_iters := 1000; "
38 " "
39 " var v := 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, v); "
48 " case callput_flag == 'put' : bsm_put (s, k, r, t, v); "
49 " }; "
50 " "
51 " var price_diff := price - target_price; "
52 " "
53 " if (abs(price_diff) <= epsilon) "
54 " { "
55 " break; "
56 " }; "
57 " "
58 " v -= price_diff / vega(s, k, r, t, v); "
59 " }; "
60 " "
61 " itr <= max_iters ? v : null; ";
62
63 T s = T( 100.00); // Spot / Stock / Underlying / Base price
64 T k = T( 110.00); // Strike price
65 T t = T( 2.22); // Years to maturity
66 T r = T( 0.05); // Risk free rate
67 T target_price = T( 0.00);
68
69 std::string callput_flag;
70
71 symbol_table_t symbol_table(symbol_table_t::e_immutable);
72 symbol_table.add_variable("s",s);
73 symbol_table.add_variable("k",k);
74 symbol_table.add_variable("t",t);
75 symbol_table.add_variable("r",r);
76 symbol_table.add_stringvar("callput_flag",callput_flag);
77 symbol_table.add_variable ("target_price",target_price);
78 symbol_table.add_pi();
79
80 compositor_t compositor(symbol_table);
81
82 compositor.add(
83 function_t("bsm_call")
84 .vars("s", "k", "r", "t", "v")
85 .expression
86 (
87 " var d1 := (log(s / k) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
88 " var d2 := d1 - v * sqrt(t); "
89 " s * ncdf(d1) - k * exp(-r * t) * ncdf(d2); "
90 ));
91
92 compositor.add(
93 function_t("bsm_put")
94 .vars("s", "k", "r", "t", "v")
95 .expression
96 (
97 " var d1 := (log(s / k) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
98 " var d2 := d1 - v * sqrt(t); "
99 " k * exp(-r * t) * ncdf(-d2) - s * ncdf(-d1); "
100 ));
101
102 compositor.add(
103 function_t("vega")
104 .vars("s", "k", "r", "t", "v")
105 .expression
106 (
107 " var d1 := (log(s / k) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
108 " s * sqrt(t) * exp(-d1^2 / 2) / sqrt(2pi); "
109 ));
110
111 expression_t expression;
112 expression.register_symbol_table(symbol_table);
113
114 parser_t parser;
115 parser.compile(implied_volatility_program,expression);
116
117 {
118 callput_flag = "call";
119 target_price = T(18.339502);
120
121 const T implied_vol = expression.value();
122
123 printf("Call Option(s: %5.3f, k: %5.3f, t: %5.3f, r: %5.3f) "
124 "@ $%8.6f Implied volatility = %10.8f\n",
125 s, k, t, r, target_price, implied_vol);
126 }
127
128 {
129 callput_flag = "put";
130 target_price = T(16.782764);
131
132 const T implied_vol = expression.value();
133
134 printf("Put Option(s: %5.3f, k: %5.3f, t: %5.3f, r: %5.3f) "
135 "@ $%8.6f Implied volatility = %10.8f\n",
136 s, k, t, r, target_price, implied_vol);
137 }
138}
139
140int main()
141{
142 compute_implied_volatility<double>();
143 return 0;
144}
void compute_implied_volatility()