CFA Exam Clock

2012 CFA Level II Exam Study Days 100

Login Form



Prep Materials Provided by Allen Resources

Binomial Method & Black-Sholes Options Pricing in C++

The following is a program I've written in C++ to price European Call / Put Options using both the Binomial Options Pricing model (via Cox Rubinstein) or the Black-Scholes model.

Please note that the program is separated into four separate source-code files:

 

  • BSF.cpp > Contains Black-Scholes Options Pricing Functions and Cumulative Normal Distribution Approximation Function
  • BT.cpp > Contains Binomial Options Pricing model (via Cox Rubinstein) Functions
  • MAIN.cpp > Contains User-Interface for calling Black-Scholes and Binomial Options Pricing Model Functions and displaying output
  • HEADER.h > Contains C++ libraries required to run the program as well as function prototypes.
Note that the parameter "delta_t" in the Binomial Option Pricing Model is defined as "years" divided by the number of steps required to build the Binomial lattice. Using Delta_t in the following example, the Absolute value of Relative Error is strictly less than 0.05 when Delta_T is less than or equal to  0.004. This is the point at which convergence is strictly less than 0.05.
Portfolio of Options:
Option 1: Short Put with strike price = 1080
Option 2: Long Call with strike price = 1200
Option 3: Short Call with strike price = 1344
Number of steps = 500, Risk Free Rate = 0.02%, Time = 2 years, Volatility = 33%
Aaron J. Glover

This program calculates the future value of $1,000,000 over the period of one year using user provided inputs of compounding frequency (how many times per year) and stated annual rate of interest. The program also calculates how many compounding cycles are necessary to bring the periodic compounding future value within $0.005 of the continuously compounded future value of $1,000,000 using a provided annual rate of interest.

The program accomplishes its computation by using a globally declared "e" constant. Additionally, numerous successive "for" loops are used to quickly "bound" the number of required periodic compounding cycles. The for loops begin by bounding the number of compounding cycles within the correct 10,000 neighborhood, then again into the correct 10s neighborhood, and then eventually in the correct 1s neighborhood.

While the code is not as aesthetically pleasing as I had hoped -- my future assignments will however look much nicer and will include copious amounts of comments. My code was created and compiled using the BloodShed Dev C++ IDE.

/*
Program Written By Aaron J. Glover
Georgia Institute of Tehcnology
MS Quantiative & Computational Finance Program
Course: Math / ISYE 6767
Created Date: 2011-08-27
Revised Date: 2011-09-04
Version Number: 1.4
http://www.TheGlover.net
Aaron.Glover at GaTech.edu
Purpose: This program displays the future value of $1,000,000 by
using the inupts of annual interest rate and number of annual
compounding cycles per year.
*/
 
#include <iostream>  // for cout and cin
#include <iomanip>
#include <string>
#include <cmath> // access to aboslute value function
 
double e = 2.718281828459045235360287471352662497757247093699959574966;
 
using namespace std;
 
// Function uses loops to do a fast count from 1000s, then 10s, then 1s
 
int quickly_bound_value(double rate, double principal, double fv_target)
{
int n=1;
double value = (principal * pow( (1+(rate/n)),n));
for(n=1; value <= fv_target ; n=n+1000)
{
value = (principal * pow( (1+(rate/n)),n));
//    cout << "Iteration: " << n << " Value: " << value << endl;
}
 
n = n - 2001; // counter adjuster
value = 0;  // value reset
 
for(n=n; value <= fv_target ; n=n+10)
{
value = (principal * pow( (1+(rate/n)),n));
 
//   cout << "Iteration: " << n << " Value: " << value << endl;
}
 
n = n - 20; // counter adjuster
value = 0;
 
for(n=n; value <= fv_target ; n=n+1)
{
value = (principal * pow( (1+(rate/n)),n));
 
//    cout << "Iteration: " << n << " Value: " << value << endl;
}
n=n-1;  // counter adjuster
return n;
 
}
 
 
 
// Currency Formatting Function used from http://www.arachnoid.com/cpptutor/student3.html
void showCurrency(double dv, int width = 14)
{
 const string radix = ".";
 const string thousands = ",";
 const string unit = "$";
 unsigned long v = (unsigned long) ((dv * 100.0) + .5);
 string fmt,digit;
 int i = -2;
 do {
 if(i == 0) {
 fmt = radix + fmt;
 }
 if((i > 0) && (!(i % 3))) {
 fmt = thousands + fmt;
 }
 digit = (v % 10) + '0';
 fmt = digit + fmt;
 v /= 10;
 i++;
 }
 while((v) || (i < 1));
 cout << unit << setw(width) << fmt.c_str();
}
 
// Function calcualtes periodic compounding interest given inputs
double future_value(double rate, int frequency, double principal)
{
return (principal * pow(((rate/frequency) + 1),frequency));
}
 
// Function calculates numer of compounding cycles required to get within a specified amount of
// continiously compounded Future Falue
 
int find_compounding_cycles(double rate, double principal)
{
double fv_continious_compounding;       // variable to hold continiously compounded FV
double fv_target;                       // variable to hold target rate (cc rate - level of percision)
double precision = 0.005;               // difference between FV_cc and our target FV
 
fv_continious_compounding = principal * pow(e, rate);
fv_target = fv_continious_compounding - precision;
 
return quickly_bound_value(rate, principal, fv_target);
 
}
 
// Main Program | Returns the Value of $1,000,000 given interest rate and n periods
 
int main(){
double interest;                // variable to hold interest rate | ex: 12.00)
int frequency;                // variable to hold annual compounding persiods)
double principal = 1000000;     // variable to hold initial principal amount
 
cout << "\n";
cout << "\n";
cout << "\n";
cout << "****************************************************" << endl;
cout << "** Future Value of $1,000,000 after 1 year when   **" << endl;
cout << "** provided with annual interest rate and number  **" << endl;
cout << "** of compounding periods per year                **" << endl;
cout << "****************************************************" << endl;
cout << "\n";
cout << "Enter annual interest rate (in dec format): ";    // ex: 12.56 (percent) per year would be 0.1256
cin >> interest;
cout << "Enter number of annual compounding cycles: ";      // ex: 6
cin >> frequency;
cout << "\n";
// Display Current Formatted Result
cout << "Account Balance will be ";
showCurrency(future_value(interest, frequency, principal));
cout << " in 1 year" << endl;
cout << "\n";
cout.precision(0);
cout << "The number of compounding cycles required per year"<< endl;
cout << "to make the diffrence between the continiously compounded" << endl;
cout << "future-value and the periodic compounded future-value FV < 0.005: " << find_compounding_cycles(interest, principal) << endl;
cout << "\n";
}

The following program calculates the future value of 1,000,000 USD in ($) after 1 year provided the: (1) annual rate of interest (%) (2) number of compounding periods within the year.

 

Formula is:
FV = PV (1+(r/n))^(n)

/*
Program Written By Aaron J. Glover
Georgia Institute of Tehcnology
MS Quantiative & Computational Finance Program

Course: Math / ISYE 6767

Created Date: 2011-08-27
Revised Date: 2011-08-27
Version Number: 1.1

http://www.TheGlover.net

Aaron.Glover at GaTech.edu

Purpose: This program displays the future value of $1,000,000 by
using the inupts of annual interest rate and number of annual
compounding cycles per year.

*/


#include   // for cout and cin
#include 
#include 
#include 

using namespace std;


// Currency Formatting Function used from http://www.arachnoid.com/cpptutor/student3.html
void showCurrency(double dv, int width = 14)
{
	const string radix = ".";
	const string thousands = ",";
	const string unit = "$";
	unsigned long v = (unsigned long) ((dv * 100.0) + .5);
	string fmt,digit;
	int i = -2;
	do {
		if(i == 0) {
			fmt = radix + fmt;
		}
		if((i > 0) && (!(i % 3))) {
			fmt = thousands + fmt;
		}
		digit = (v % 10) + '0';
		fmt = digit + fmt;
		v /= 10;
		i++;
	}
	while((v) || (i < 1));
	cout << unit << setw(width) << fmt.c_str() << endl;
}

// Main Program | Returns the Value of $1,000,000 given interest rate and n periods

int main(){
    double interest;                // variable to hold interest rate | ex: 12.00)
    int n_compounds;                // variable to hold annual compounding persiods)
    double principal = 1000000;     // variable to hold initial principal amount
    double final_value = 0;         // assignment varibale to hold calculations
    
   cout << "\n";
   cout << "\n";
   cout << "\n";
   cout << "****************************************************" << endl;
   cout << "** Future Value of $1,000,000 after 1 year when   **" << endl;
   cout << "** provided with annual interest rate and number  **" << endl;
   cout << "** of compounding periods per year                **" << endl;
   cout << "****************************************************" << endl;
   cout << "\n";
   cout  interest;
   cout  n_compounds;
    
    interest = interest/100;       // converts stated interest to decimal
    
    // Present_Value*(1+(r/n))^n = Future Value
    final_value = principal * pow(((interest/n_compounds) + 1),n_compounds);
    
   cout << "\n"; 
   // Display Current Formatted Result  
   cout << "Account Balance is ";
   showCurrency(final_value);
   cout << "\n";
   cout << "\n";
}