C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_american_option_binomial_model.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * American Option Binomial Pricing Model *
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 american_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 " var discount := exp(-r * dt); "
42 " "
43 " var option_price[n + 1] := {0}; "
44 " "
45 " for (var i := 0; i <= n; i += 1) "
46 " { "
47 " var base_price := s * u^(n - 2i); "
48 " option_price[i] := "
49 " switch "
50 " { "
51 " case callput_flag == 'call' : max(base_price - k, 0); "
52 " case callput_flag == 'put' : max(k - base_price, 0); "
53 " }; "
54 " }; "
55 " "
56 " for (var j := n - 1; j >= 0; j -= 1) "
57 " { "
58 " for (var i := 0; i <= j; i += 1) "
59 " { "
60 " option_price[i] := discount * "
61 " (p_up * option_price[i] + p_down * option_price[i + 1]); "
62 " var base_price := s * u^(j - 2i); "
63 " var exercise_price := "
64 " switch "
65 " { "
66 " case callput_flag == 'call' : base_price - k; "
67 " case callput_flag == 'put' : k - base_price; "
68 " }; "
69 " "
70 " option_price[i] := max(option_price[i], exercise_price); "
71 " } "
72 " }; "
73 " "
74 " option_price[0]; ";
75
76 T s = T( 100.00); // Spot / Stock / Underlying / Base price
77 T k = T( 110.00); // Strike price
78 T v = T( 0.30); // Volatility
79 T t = T( 2.22); // Years to maturity
80 T r = T( 0.05); // Risk free rate
81 T n = T(1000.00); // Number of time steps
82
83 std::string callput_flag;
84
85 symbol_table_t symbol_table(symbol_table_t::e_immutable);
86 symbol_table.add_variable("s", s);
87 symbol_table.add_variable("k", k);
88 symbol_table.add_variable("t", t);
89 symbol_table.add_variable("r", r);
90 symbol_table.add_variable("v", v);
91 symbol_table.add_constant("n", n);
92 symbol_table.add_stringvar("callput_flag",callput_flag);
93
94 expression_t expression;
95 expression.register_symbol_table(symbol_table);
96
97 parser_t parser;
98 parser.compile(american_option_binomial_model_program,expression);
99
100 callput_flag = "call";
101
102 const T binomial_call_option_price = expression.value();
103
104 callput_flag = "put";
105
106 const T binomial_put_option_price = expression.value();
107
108 printf("American BinomialPrice(call, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f) = %10.6f\n",
109 s, k, t, r, v,
110 binomial_call_option_price);
111
112 printf("American BinomialPrice(put , %5.3f, %5.3f, %5.3f, %5.3f, %5.3f) = %10.6f\n",
113 s, k, t, r, v,
114 binomial_put_option_price);
115
116 // American option put-call 'parity': s - k < call - put < s - k * e^(-rt)
117 const T callput_diff = (binomial_call_option_price - binomial_put_option_price);
118 const T basestrike_diff = s - k;
119 const T basepv_diff = s - k * std::exp(-r * t);
120 const bool put_call_parity = (basestrike_diff < callput_diff) &&
121 (callput_diff < basepv_diff ) ;
122
123 const T call_price_r0 = binomial_put_option_price + basestrike_diff;
124 const T call_price_r1 = binomial_put_option_price + basepv_diff;
125
126 const T put_price_r0 = binomial_call_option_price - basepv_diff;
127 const T put_price_r1 = binomial_call_option_price - basestrike_diff;
128
129 printf("Put-Call parity: %s\n", put_call_parity ? "True" : "False");
130
131 printf("Call price range: %7.4f < %7.4f < %7.4f\n",
132 call_price_r0,
133 binomial_call_option_price,
134 call_price_r1);
135
136 printf("Put price range: %7.4f < %7.4f < %7.4f\n",
137 put_price_r0,
138 binomial_put_option_price,
139 put_price_r1);
140}
141
142int main()
143{
144 american_option_binomial_option_pricing_model<double>();
145 return 0;
146}
void american_option_binomial_option_pricing_model()