/* 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 Outlined for this purpose by Jonathan Goodman, instructor. Modified October 8 to calibrate the tree to given market parameters For Assignment 4 */ #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 S0, // The spot price of the underlying stock double T, // The time until expiration double K, // The strike price double r, // The risk free rate, in percent per year double sig, // The volatility, on a yearly basis int n // The number of time steps in the binomial tree ); int main() { ofstream csvFile; // The file for output, assumed to be csv format for Excel. csvFile.open ("option.csv"); double V; double Del; double r = .03; double sig = .2; double K = 100; double S0min = 20; double S0max = 150; double dS0 = 1; double K = 100; int n = 200; // Write the parameters of this run into the output file, in the top two rows of the spreadsheet csvFile << " Strike, r, vol, T " << endl; csvFile << K << ", " << r << ", " << vol << ", " << T << endl; csvfile << endl; // leave a blank row before the data // Write column headers for the output data csvFile << "spot, option value, option delta" << endl; // The actual computation loop for ( double S0 = S0min; S0 < S0max; S0+= dS0) { // Compute the option price for a range of spot prices if ( // check the return code. bTree( &V, &Del, S0, T, K, r, sig, n) ) { cout << "Got an error in bTree. Stopping. " << endl; return 1;} csvFile << S0 << ", " << 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 S0, // The spot price of the underlying stock double T, // The time until expiration double K, // The strike price double r, // The risk free rate double sig, // The volatility int n // The number of time steps in the binomial tree ) { // A bunch of error checking. Probably pointless here, but a good habit to keep up. // ADD YOUR OWN CHECKS double dt = T/n; // The time step size if there are n time steps to time T double u = exp( sig*sqrt(dt) ); // The up and down steps, now found from sigma and dt double d = exp(-sig*sqrt(dt) ); double B = exp(-r*dt); // CONTINUE AS BEFORE ..... }