C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_simple_example_21.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * Simple Example 21 *
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
33 const std::string european_option_binomial_model_program =
34 " var dt := t / n; "
35 " var z := exp(r * dt); "
36 " var z_inv := 1 / z; "
37 " var u := exp(v * sqrt(dt)); "
38 " var u_inv := 1 / u; "
39 " var p_up := (z - u_inv) / (u - u_inv); "
40 " var p_down := 1 - p_up; "
41 " "
42 " var option_price[n + 1] := [0]; "
43 " "
44 " for (var i := 0; i <= n; i += 1) "
45 " { "
46 " var base_price := s * u^(n - 2i); "
47 " option_price[i] := "
48 " switch "
49 " { "
50 " case callput_flag == 'call' : max(base_price - k, 0); "
51 " case callput_flag == 'put' : max(k - base_price, 0); "
52 " }; "
53 " }; "
54 " "
55 " for (var j := n - 1; j >= 0; j -= 1) "
56 " { "
57 " for (var i := 0; i <= j; i += 1) "
58 " { "
59 " option_price[i] := z_inv * "
60 " (p_up * option_price[i] + p_down * option_price[i + 1]); "
61 " } "
62 " }; "
63 " "
64 " option_price[0]; ";
65
66 T s = T( 100.00); // Spot / Stock / Underlying / Base price
67 T k = T( 110.00); // Strike price
68 T v = T( 0.30); // Volatility
69 T t = T( 2.22); // Years to maturity
70 T r = T( 0.05); // Risk free rate
71 T n = T(1000.00); // Number of time steps
72
73 std::string callput_flag;
74
75 symbol_table_t symbol_table;
76 symbol_table.add_variable("s",s);
77 symbol_table.add_variable("k",k);
78 symbol_table.add_variable("t",t);
79 symbol_table.add_variable("r",r);
80 symbol_table.add_variable("v",v);
81 symbol_table.add_constant("n",n);
82 symbol_table.add_stringvar("callput_flag",callput_flag);
83
84 expression_t expression;
85 expression.register_symbol_table(symbol_table);
86
87 parser_t parser;
88 parser.compile(european_option_binomial_model_program,expression);
89
90 callput_flag = "call";
91
92 const T binomial_call_option_price = expression.value();
93
94 callput_flag = "put";
95
96 const T binomial_put_option_price = expression.value();
97
98 printf("BinomialPrice(call, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f) = %10.6f\n",
99 s, k, t, r, v,
100 binomial_call_option_price);
101
102 printf("BinomialPrice(put , %5.3f, %5.3f, %5.3f, %5.3f, %5.3f) = %10.6f\n",
103 s, k, t, r, v,
104 binomial_put_option_price);
105
106 const T put_call_parity_diff =
107 (binomial_call_option_price - binomial_put_option_price) -
108 (s - k * std::exp(-r * t));
109
110 printf("Put-Call parity difference: %20.17f\n", put_call_parity_diff);
111}
112
113int main()
114{
115 binomial_option_pricing_model<double>();
116 return 0;
117}
void binomial_option_pricing_model()