OpcUaCanOpen
CANopen OPC-UA server
exprtk_simple_example_15.cpp
Go to the documentation of this file.
1 /*
2  **************************************************************
3  * C++ Mathematical Expression Toolkit Library *
4  * *
5  * Simple Example 15 *
6  * Author: Arash Partow (1999-2019) *
7  * URL: http://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  * http://www.opensource.org/licenses/MIT *
14  * *
15  **************************************************************
16 */
17 
18 
19 #include <cstdio>
20 #include <string>
21 
22 #include "exprtk.hpp"
23 
24 
25 template <typename T>
27 {
31 
32  std::string bsm_model_program =
33  " var d1 := (log(s / x) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
34  " var d2 := d1 - v * sqrt(t); "
35  " "
36  " if (callput_flag == 'call') "
37  " s * ncdf(d1) - x * e^(-r * t) * ncdf(d2); "
38  " else if (callput_flag == 'put') "
39  " x * e^(-r * t) * ncdf(-d2) - s * ncdf(-d1); "
40  " ";
41 
42  T s = T(60.00); // Stock price
43  T x = T(65.00); // Strike price
44  T t = T( 0.25); // Years to maturity
45  T r = T( 0.08); // Risk free rate
46  T v = T( 0.30); // Volatility
47 
48  std::string callput_flag;
49 
51 
52  symbol_table_t symbol_table;
53  symbol_table.add_variable("s",s);
54  symbol_table.add_variable("x",x);
55  symbol_table.add_variable("t",t);
56  symbol_table.add_variable("r",r);
57  symbol_table.add_variable("v",v);
58  symbol_table.add_constant("e",e);
59  symbol_table.add_stringvar("callput_flag",callput_flag);
60 
61  expression_t expression;
62  expression.register_symbol_table(symbol_table);
63 
65  parser.compile(bsm_model_program,expression);
66 
67  {
68  callput_flag = "call";
69 
70  T bsm = expression.value();
71 
72  printf("BSM(%s,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f) = %10.6f\n",
73  callput_flag.c_str(),
74  s,x,t,r,v,
75  bsm);
76  }
77 
78  {
79  callput_flag = "put";
80 
81  T bsm = expression.value();
82 
83  printf("BSM(%s,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f) = %10.6f\n",
84  callput_flag.c_str(),
85  s,x,t,r,v,
86  bsm);
87  }
88 }
89 
90 int main()
91 {
92  black_scholes_merton_model<double>();
93  return 0;
94 }