test
C++ Binomial Option Pricer
Page 1 of 1 [ 2 posts ]
My c++ is rusty so I coded up a binomial option pricer for practice
S = the current price of the underlying asset
K = the strike price of the option
R = the risk free rate
V = the volatility
T = the time left until expiration
N = the number of iterations per time step
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
float european_binomial(float s,float k, float r,float v,int t, int N, bool verbose = false){
int steps = N*t;
float tree[steps+1];
float u,d;
float dt = (float)t/(float)N;
u=exp(v*sqrt(dt));
d=1.0/u;
float p = (exp(r*dt)-d)/(u-d);
float q = 1.0-p;
if(verbose){
cout << "p: " << p << endl;
cout << "q: " << q << endl << endl;
}
for(int n=0;n<steps+1;n++){
tree[n]=fmax(s*pow(u,steps-n)*pow(d,n)-k,0.0);
if (verbose) cout << tree[n] << '\n';
}
if (verbose) cout << endl;
for(int i=steps;i>0;i--){
for(int j=0;j<i;j++){
tree[j]=p*tree[j]+q*tree[j+1];
}
}
return tree[0]*exp(-r*t);
}
int main()
{
cout << "The European binomial option price is $";
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
cout << european_binomial(20.0,17.00,.02,.1,3,10000);
return 0;
}
#include <cmath>
#include <iomanip>
using namespace std;
float european_binomial(float s,float k, float r,float v,int t, int N, bool verbose = false){
int steps = N*t;
float tree[steps+1];
float u,d;
float dt = (float)t/(float)N;
u=exp(v*sqrt(dt));
d=1.0/u;
float p = (exp(r*dt)-d)/(u-d);
float q = 1.0-p;
if(verbose){
cout << "p: " << p << endl;
cout << "q: " << q << endl << endl;
}
for(int n=0;n<steps+1;n++){
tree[n]=fmax(s*pow(u,steps-n)*pow(d,n)-k,0.0);
if (verbose) cout << tree[n] << '\n';
}
if (verbose) cout << endl;
for(int i=steps;i>0;i--){
for(int j=0;j<i;j++){
tree[j]=p*tree[j]+q*tree[j+1];
}
}
return tree[0]*exp(-r*t);
}
int main()
{
cout << "The European binomial option price is $";
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
cout << european_binomial(20.0,17.00,.02,.1,3,10000);
return 0;
}
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double american_binomial(double s,double k, double r,double v,int t, int N, bool verbose = false){
int steps = N*t;
//cout << steps << endl;
double dt = (double)t/(double)N;
//cout << "dt: " << dt << endl;
double E[steps][steps+1];
double u,d;
u=exp(v*sqrt(dt));
d=1.0/u;
double disc=exp(r*dt);
double p = (disc-d)/(u-d);
double q = 1.0-p;
if(verbose){
cout << "p: " << p << endl;
cout << "q: " << q << endl << endl;
}
double temp;
for(int i=steps-1;i>=0;i--){
for(int n=0; n<i+1;n++){
temp=s*pow(u,i-2*n)-k;
E[i][n]=(temp>0.0)?temp:0.0;
//cout << E[i][n] << endl;
}
//cout << endl;
}
for(int i=steps-2;i>=0;i--){
for(int n=0; n<i+1;n++){
E[i][n]=fmax(E[i][n],(p*E[i+1][n]+q*E[i+1][n+1])*disc);
//cout << "E[" << i << "][" << n << "] " << E[i][n] << endl;
}
//cout << endl;
}
double answer=E[0][0];
delete[] E;
return answer;
}
int main()
{
cout << "The American binomial option price is $";
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
cout << american_binomial(20.0,17.00,.02,.1,3,100);
return 0;
}
#include <cmath>
#include <iomanip>
using namespace std;
double american_binomial(double s,double k, double r,double v,int t, int N, bool verbose = false){
int steps = N*t;
//cout << steps << endl;
double dt = (double)t/(double)N;
//cout << "dt: " << dt << endl;
double E[steps][steps+1];
double u,d;
u=exp(v*sqrt(dt));
d=1.0/u;
double disc=exp(r*dt);
double p = (disc-d)/(u-d);
double q = 1.0-p;
if(verbose){
cout << "p: " << p << endl;
cout << "q: " << q << endl << endl;
}
double temp;
for(int i=steps-1;i>=0;i--){
for(int n=0; n<i+1;n++){
temp=s*pow(u,i-2*n)-k;
E[i][n]=(temp>0.0)?temp:0.0;
//cout << E[i][n] << endl;
}
//cout << endl;
}
for(int i=steps-2;i>=0;i--){
for(int n=0; n<i+1;n++){
E[i][n]=fmax(E[i][n],(p*E[i+1][n]+q*E[i+1][n+1])*disc);
//cout << "E[" << i << "][" << n << "] " << E[i][n] << endl;
}
//cout << endl;
}
double answer=E[0][0];
delete[] E;
return answer;
}
int main()
{
cout << "The American binomial option price is $";
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
cout << american_binomial(20.0,17.00,.02,.1,3,100);
return 0;
}
For some reason the american version won't work for N>100. Can someone help me out?
Fixed both
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double european_binomial(double s,double k, double r,double v,int t, int N, bool verbose = false){
int steps = N;
double tree[steps+1];
double u,d;
double dt = (double)t/(double)N;
u=exp(v*sqrt(dt));
d=1.0/u;
double disc=exp(-r*dt);
double p = (exp(r*dt)-d)/(u-d);
double q = 1.0-p;
if(verbose){
cout << "p: " << p << endl;
cout << "q: " << q << endl;
cout << "dt: " << dt << endl;
cout << "disc: " << disc << endl;
cout << "u: " << u << endl;
cout << "d: " << d << endl << endl;
cout << "Value at expiration:" << endl;
}
for(int i=0;i<=steps;i++){
tree[i]=fmax(s*pow(u,steps-2*i)-k,0.0);
if(verbose) cout << "[" << steps << "][" << i << "] " << tree[i] << endl;
}
if(verbose) cout << endl << "Value at time t and node n [t][n]:" << endl;
for(int i=steps-1;i>=0;i--){
for(int j=0;j<=i;j++){
tree[j]=(p*tree[j]+q*tree[j+1])*disc;
if(verbose) cout << "[" << i << "][" << j << "] " << tree[j] << endl;
}
if(verbose) cout << endl;
}
return tree[0];
}
int main()
{
cout << "The European binomial option price is $";
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
cout << european_binomial(20.0,17.00,.02,.1,1,4);
return 0;
}
#include <cmath>
#include <iomanip>
using namespace std;
double european_binomial(double s,double k, double r,double v,int t, int N, bool verbose = false){
int steps = N;
double tree[steps+1];
double u,d;
double dt = (double)t/(double)N;
u=exp(v*sqrt(dt));
d=1.0/u;
double disc=exp(-r*dt);
double p = (exp(r*dt)-d)/(u-d);
double q = 1.0-p;
if(verbose){
cout << "p: " << p << endl;
cout << "q: " << q << endl;
cout << "dt: " << dt << endl;
cout << "disc: " << disc << endl;
cout << "u: " << u << endl;
cout << "d: " << d << endl << endl;
cout << "Value at expiration:" << endl;
}
for(int i=0;i<=steps;i++){
tree[i]=fmax(s*pow(u,steps-2*i)-k,0.0);
if(verbose) cout << "[" << steps << "][" << i << "] " << tree[i] << endl;
}
if(verbose) cout << endl << "Value at time t and node n [t][n]:" << endl;
for(int i=steps-1;i>=0;i--){
for(int j=0;j<=i;j++){
tree[j]=(p*tree[j]+q*tree[j+1])*disc;
if(verbose) cout << "[" << i << "][" << j << "] " << tree[j] << endl;
}
if(verbose) cout << endl;
}
return tree[0];
}
int main()
{
cout << "The European binomial option price is $";
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
cout << european_binomial(20.0,17.00,.02,.1,1,4);
return 0;
}
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double american_binomial(double s,double k, double r,double v,int t, int N, bool verbose = false){
int steps = N;
double tree[steps+1];
double u,d;
double dt = (double)t/(double)N;
u=exp(v*sqrt(dt));
d=1.0/u;
double disc=exp(-r*dt);
double p = (exp(r*dt)-d)/(u-d);
double q = 1.0-p;
if(verbose){
cout << "p: " << p << endl;
cout << "q: " << q << endl;
cout << "dt: " << dt << endl;
cout << "disc: " << disc << endl;
cout << "u: " << u << endl;
cout << "d: " << d << endl << endl;
cout << "Value at expiration:" << endl;
}
for(int i=0;i<=steps;i++){
tree[i]=fmax(s*pow(u,steps-2*i)-k,0.0);
if(verbose) cout << "[" << steps << "][" << i << "] " << tree[i] << endl;
}
if(verbose) cout << endl << "Value at time t and node n [t][n]:" << endl;
for(int i=steps;i>=0;i--){
for(int j=0;j<=i;j++){
tree[j]=fmax(s*pow(u,i-2*j)-k,(p*tree[j]+q*tree[j+1])*disc);
if(verbose) cout << "[" << i << "][" << j << "] " << tree[j] << endl;
}
if(verbose) cout << endl;
}
return tree[0];
}
int main()
{
cout << "The American binomial option price is $";
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
cout << american_binomial(20.0,17.00,.02,.1,1,4,true);
return 0;
}
#include <cmath>
#include <iomanip>
using namespace std;
double american_binomial(double s,double k, double r,double v,int t, int N, bool verbose = false){
int steps = N;
double tree[steps+1];
double u,d;
double dt = (double)t/(double)N;
u=exp(v*sqrt(dt));
d=1.0/u;
double disc=exp(-r*dt);
double p = (exp(r*dt)-d)/(u-d);
double q = 1.0-p;
if(verbose){
cout << "p: " << p << endl;
cout << "q: " << q << endl;
cout << "dt: " << dt << endl;
cout << "disc: " << disc << endl;
cout << "u: " << u << endl;
cout << "d: " << d << endl << endl;
cout << "Value at expiration:" << endl;
}
for(int i=0;i<=steps;i++){
tree[i]=fmax(s*pow(u,steps-2*i)-k,0.0);
if(verbose) cout << "[" << steps << "][" << i << "] " << tree[i] << endl;
}
if(verbose) cout << endl << "Value at time t and node n [t][n]:" << endl;
for(int i=steps;i>=0;i--){
for(int j=0;j<=i;j++){
tree[j]=fmax(s*pow(u,i-2*j)-k,(p*tree[j]+q*tree[j+1])*disc);
if(verbose) cout << "[" << i << "][" << j << "] " << tree[j] << endl;
}
if(verbose) cout << endl;
}
return tree[0];
}
int main()
{
cout << "The American binomial option price is $";
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
cout << american_binomial(20.0,17.00,.02,.1,1,4,true);
return 0;
}
Page 1 of 1 [ 2 posts ]
| Similar Topics | |
|---|---|
| What if being yourself isn't the best option. |
15 Jan 2007, 9:37 pm |
| Is withdrawal the best option after all? |
26 Sep 2014, 2:02 pm |
| is silence the better option? |
03 Jun 2011, 9:14 am |
| seperatism an option? |
22 Apr 2014, 4:40 am |
