-
Notifications
You must be signed in to change notification settings - Fork 5
/
BinomialTree.cpp
150 lines (146 loc) · 3.29 KB
/
BinomialTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* C++ Program to Implement Binomial Tree
*/
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
/*
* Node Declaration
*/
struct Node
{
double price, time, optionvalue;
};
/*
* Class Declaration
*/
class BinomialTree
{
private:
Node **tree;
int n;
double S, volatility, upfactor, tfin, tstep;
double getValue(int level, int node, double K, double R);
void initNode (int level, int node);
public:
BinomialTree(double S, double volatility, int n, double tstep);
double getValue(double K, double R);
void print();
};
/*
* Constructor
*/
BinomialTree::BinomialTree(double price , double vol, int _n, double _tstep)
{
n = _n;
S = price;
volatility = vol;
tstep = _tstep;
tfin = n * tstep;
upfactor = exp (volatility * sqrt (tstep));
tree = new Node * [n];
for (int i = 0; i < n; i++)
tree[i] = new Node [i+1] ;
tree[0][0].price = S;
tree[0][0].time = 0.0;
double currtime = 0.0;
for (int i = 1; i < n; i++)
{
Node * currnode = tree[i];
currtime += tstep;
for (int j = 0; j <= i; j++, currnode++)
{
if (!j)
{
currnode->price = tree[i-1][j].price / upfactor ;
currnode->time = currtime;
}
else
{
currnode->price = tree[i-1][j-1].price * upfactor ;
currnode->time = currtime;
}
}
}
}
/*
* Get Value Function
*/
double BinomialTree::getValue(int l, int node, double K, double df)
{
if (l == (n-1))
{
if (K < tree[l][node].price)
return tree[l][node].optionvalue = tree[l][node].price - K;
else
return tree[l][node].optionvalue = 0.0;
}
else
{
double g1 = getValue(l + 1, node + 1, K, df);
double g2 = getValue(l + 1, node, K, df);
return tree[l][node].optionvalue = 0.5 * df * (g1 + g2);
}
}
/*
* Get Value Function
*/
double BinomialTree::getValue(double K, double R)
{
double discountfactor = exp (-R * tstep);
return getValue(0, 0, K, discountfactor);
}
/*
* Display optimal values
*/
void BinomialTree::print()
{
for (int i = 0; i < n; i++)
{
for( int j = 0; j <= i; j++ )
{
cout<< "[" << tree[i][j].price << "," << tree[i][j].time << ",";
cout<< tree[i][j].optionvalue << "]\t";
}
cout <<endl;
}
}
/*
* Main Contains Menu
*/
int main()
{
double S, V, K, T, R, N;
cout<<"Enter Security Price: ";
cin>>S;
cout<<"Enter Volatility: ";
cin>>V;
cout<<"Enter Call Strike Price: ";
cin>>K;
cout<<"Enter Time To Expiry: ";
cin>>T;
cout<<"Enter Risk Free Rate: ";
cin>>R;
cout<<"Enter levels: ";
cin>>N;
BinomialTree bt(S, V, N, T / N);
double value = bt.getValue(K, R);
bt.print();
cout<< "OPTION VALUE = " << value <<endl;
return 0;
}
/*
Enter Security Price: 10
Enter Volatility: 0.5
Enter Call Strike Price: 100
Enter Time To Expiry: 5
Enter Risk Free Rate: 0.6
Enter levels: 3
[10,0,0]
[5.24402,1.66667,0] [19.0693,1.66667,0]
[2.74997,3.33333,0] [10,3.33333,0] [36.364,3.33333,0]
OPTION VALUE = 0
Process returned 0 (0x0) execution time : 14.829 s
Press any key to continue.
*/