#include #include #include #include using namespace std; /* Template code for the binomial tree * Instructions to build the code are here * http://www.maths.manchester.ac.uk/~pjohnson/Tutorials/node1.html */ int main() { // declare and initialise Black Scholes parameters // declare and initialise Black Scholes parameters double S0=100.,X=100.,T=1.,r=0.06,sigma=0.2; // declare and initialise tree paramaters (steps in tree) int n=3; // declare and initialise local variables (u,d,q) double dt,u,d,q; dt = T/n; u = exp(sigma*sqrt(dt)); d = exp(-sigma*sqrt(dt)); q = (exp(r*dt)-d)/(u-d); // create storage for the stock price tree and option price tree vector> stockTree(n+1,vector(n+1)); // setup and initialise the stock price tree for(int i=0;i<=n;i++) { for(int j=0;j<=i;j++) { stockTree[i][j]=S0*pow(u,j)*pow(d,i-j); cout << i << " " << j << " " << stockTree[i][j] << endl; } cout << endl; } /** OUTPUT 0 0 100 1 0 89.0947 1 1 112.24 2 0 79.3787 2 1 100 2 2 125.978 3 0 70.7222 3 1 89.0947 3 2 112.24 3 3 141.398 */ }