Created
March 30, 2010 17:11
-
-
Save ecounysis/349313 to your computer and use it in GitHub Desktop.
Black-Scholes Option Pricing Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "bs.h" | |
int main(int argc, char *argv[]) | |
{ | |
double strike = atof(argv[1]); | |
double asset = atof(argv[2]); | |
double std = atof(argv[3]); | |
double rfr = atof(argv[4]); | |
double dte = atof(argv[5]); | |
printf("Strike Price: %f \n", strike); | |
printf("Asset Price: %f \n", asset); | |
printf("Std Dev: %f \n", std); | |
printf("Risk Free: %f \n", rfr); | |
printf("Days to Exp: %f \n", dte); | |
printf("Put Value: %f \n", putvalue(strike, asset, std, rfr, dte)); | |
printf("Call Value: %f \n", callvalue(strike, asset, std, rfr, dte)); | |
printf("Delta Ratio: %f \n", delta(strike, asset, std, rfr, dte)); | |
printf("T-Bills: %f \n", bond(strike, asset, std, rfr, dte)); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math.h> | |
#include "bs.h" | |
#define DAYS_IN_YEAR 365 | |
static double N(double, double, double, double, double); | |
static double delta2(double, double, double); | |
static double ND2(double, double, double); | |
static double N(double strike, double s, double sd, double r, double days) | |
{ | |
double ls = log(s); | |
double lx = log(strike); | |
double t = days / DAYS_IN_YEAR; | |
double sd2 = pow(sd, 2); | |
return ls - lx + r * t + sd2 * t / 2; | |
} | |
double delta(double strike, double s, double sd, double r, double days) | |
{ | |
double n = N(strike, s, sd, r, days); | |
double sqT = sqrt(days / DAYS_IN_YEAR); | |
double d = sd * sqT; | |
double d1 = n / d; | |
return normal(d1); | |
} | |
static double delta2(double n, double sd, double days) | |
{ | |
double sqT = sqrt(days / DAYS_IN_YEAR); | |
double d = sd * sqT; | |
double d1 = n / d; | |
return normal(d1); | |
} | |
static double ND2(double n, double sd, double days) | |
{ | |
double sqrtT = sqrt(days / DAYS_IN_YEAR); | |
double d = sd * sqrtT; | |
double d1 = n / d; | |
double d2 = d1 - sd * sqrtT; | |
return normal(d2); | |
} | |
double bond(double strike, double s, double sd, double r, double days) | |
{ | |
double n = N(strike, s, sd, r, days); | |
double t = days / DAYS_IN_YEAR; | |
double nd1 = delta2(n, sd, days); | |
double nd2 = ND2(n, sd, days); | |
return -strike * exp(-r*t) * nd2; | |
} | |
double callvalue(double strike, double s, double sd, double r, double days) | |
{ | |
double n = N(strike, s, sd, r, days); | |
double t = days / DAYS_IN_YEAR; | |
double nd1 = delta2(n, sd, days); | |
double b = bond(strike, s, sd, r, days); | |
return s * nd1 + b; | |
} | |
double putvalue(double strike, double s, double sd, double r, double days) | |
{ | |
double t = days / DAYS_IN_YEAR; | |
double call = callvalue(strike, s, sd, r, days); | |
return strike * exp(-r * t) - s + call; | |
} | |
double normal(double zz) | |
{ | |
//cdf of 0 is 0.5 | |
if (zz == 0) | |
{ | |
return 0.5; | |
} | |
double z = zz; //zz is input variable, use z for calculations | |
if (zz < 0) | |
z = -zz; //change negative values to positive | |
//set constants | |
double p = 0.2316419; | |
double b1 = 0.31938153; | |
double b2 = -0.356563782; | |
double b3 = 1.781477937; | |
double b4 = -1.821255978; | |
double b5 = 1.330274428; | |
//CALCULATIONS | |
double f = 1 / sqrt(2 * M_PI); | |
double ff = exp(-pow(z, 2) / 2) * f; | |
double s1 = b1 / (1 + p * z); | |
double s2 = b2 / pow((1 + p * z), 2); | |
double s3 = b3 / pow((1 + p * z), 3); | |
double s4 = b4 / pow((1 + p * z), 4); | |
double s5 = b5 / pow((1 + p * z), 5); | |
//sz is the right-tail approximation | |
double sz = ff * (s1 + s2 + s3 + s4 + s5); | |
double rz; | |
//cdf of negative input is right-tail of input's absolute value | |
if (zz < 0) | |
rz = sz; | |
//cdf of positive input is one minus right-tail | |
if (zz > 0) | |
rz = (1 - sz); | |
return rz; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double delta(double, double, double, double, double); | |
double bond(double, double, double, double, double); | |
double putvalue(double, double, double, double, double); | |
double callvalue(double, double, double, double, double); | |
double normal(double); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (C) 2011, Eric Christensen | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ERIC | |
CHRISTENSEN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | |
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
Except as contained in this notice, the name of Eric Christensen shall not be | |
used in advertising or otherwise to promote the sale, use or other dealings in | |
this Software without prior written authorization from Eric Christensen. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment