/* A simple code for binomial tree option valuation for the class Derivative Securities, fall 2009 http://www.math.nyu.edu/faculty/goodman/teaching/DerivSec09/index.html Written for this purpose by Jonathan Goodman, instructor. Corrected Oct. 5, 2009 to correct the dimension statement and some loop ranges. */ #include #include #include #define MAXN 100 /* The max number of tree steps allowed */ /* A program to compute a simple binomial tree price for a European style put option */ using namespace std; int bTree // The return value is an error code. 0 means it was OK, not 0 means it was bad. ( double* V, // A return value, the computed option price for S0 double* Del, // A return value, the computed value of Delta double B, // The zero coupon bond price for one period double u, // The up step: S0 --> u*S0 in one step double d, // The down step: S0 --> d*S0 in one step double S0, // The current stock price double K, // The strike price int n // The number of steps in the binomial tree ); int main() { cout << "hello world." << endl; ofstream csvFile; // The file for output, assumed to be csv format for Excel. csvFile.open ("option.csv"); double V; double Del; double B = .9; double u = 1.2; double d = .8; double S0 = 100; double Kmin = 90; double Kstep = 10; double K; int nK = 8; int n = 10; for ( int i = 0; i < nK; i++) { // Compute the option price for a range of strikes K = Kmin + i*Kstep; // Get the strike for this computation if ( // check the return code. bTree( &V, &Del, B, u, d, S0, K, n) ) { cout << "Got an error in bTree. Stopping. " << endl; return 1;} csvFile << V << ", " << Del << endl; } csvFile.close(); // Close up the file ... return 0; // ... and go home. } int bTree // The return value is an error code. 0 means it was OK, not 0 means it was bad. ( double* V, // A return value, the computed option price for S0 double* Del, // A return value, the computed value of Delta double B, // The zero coupon bond price for one period double u, // The up step: S0 --> u*S0 in one step double d, // The down step: S0 --> d*S0 in one step double S0, // The current stock price double K, // The strike price int n // The number of steps in the binomial tree ){ // A bunch of error checking. Probably pointless here, but a good habit to keep up. if ( n < 1 ) { cout << "bTree got number of tree steps n = " << n << ", returning." << endl; return 1; } if ( u <= 1/B ) { cout << "Arb opportunity because u <= 1/B. u = " << u << ", B = " << B << ", returning." << endl; return 1; } if ( d >= 1/B ) { cout << "Arb opportunity because d >= 1/B. d = " << d << ", B = " << B << ", returning." << endl; return 1; } if ( u < d ) { cout << "Got u < d. u = " << u << ", d = " << d << ", returning." << endl; return 1; } if (n > MAXN ) { cout << "Got n > MAXN. You asked for too big a tree. n = " << n << ", MAXN = " << MAXN << ", returning." << endl; return 1; } // Define and initialize variables double* Vt = new double[n+1]; // The option values at a given level in the tree. They go from 0 to n, with n+1 total. double S; // A generic stock price int k; // The level in the tree, starting from n-1 and counting down to 0 int j; // number of up steps to get here, somewhere between 0 and k double pu = .5; // The risk neutral up probability -- MUST FIX double pd = .5; // The risk neutral down probability -- MUST FIX // Set the final values to be the option payout. S = S0*pow( d, n); // Start at the bottom of the tree at the final time. for ( j = 0; j <= n; j++) { // Work from the bottom to the top Vt[j] = S; // The payout for this value of S, will depend on K in the real code S = S * u / d; // Go from S0*d^(k-j)*u^j to the value with j one greater. } // The binomial tree calculation itself for ( k = n - 1; k >= 0; k--) { // Work from the final time ( k = n) back to the initial time (k = 0) for ( j = 0; j <=k; j++ ) { // Work from the all down state up to the all up state Vt[j] = B*( pd*Vt[j] + pu*Vt[j+1] ) ; // Check that this does not overwrite variables before we're done with them. } if ( k == 1 ) // On the next to last (i.e. first) step of the binomial tree *Del = ( Vt[1] - Vt[0] ) / ( S0*( u - d ) ); // Compute the option Delta } *V = Vt[0]; delete Vt; // Clean up memory leaks ... return 0; // ... and go home. }