65.9K
CodeProject is changing. Read more.
Home

Black-Scholes Option Pricing

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.82/5 (9 votes)

Jun 10, 2004

1 min read

viewsIcon

98461

downloadIcon

757

Web ready Black-Scholes Option Pricing Program

Introduction

This is a web ready .aspx Black-Scholes Option Pricing program. I have yet to spend the time to add error checking and display of error messages (an easy task: add another display box) and perform some analysis of inputs. I plan to add more option sensitivities.

Background

  • “Financial Engineering” by Kolb and Overdahl (3rd edition) - ISBN: 0471232327 see page 118
  • “Option, Futures and Other Derivatives” by John C. Hull

Using the code

This is a scientific program and is web capable. There is no way to display errors (need another slot below...takes a minute or two). Available in Visual C++ also!

INSTALL

  1. Need .NET SDK framework installed (downloadable free I believe). This may be unnecessary if you are using Netscape 7.1 or IE 6
  2. Put the code into C:\ASPX\blackscholes.aspx
  3. Create a virtual directory "aspx" using IIS 5.1 connected to C:\ASPX and make sure the IIS web server is started.
  4. Use "http://localhost/aspx/blackscholes.aspx" in a browser URL slot in either IE6 or Netscape 7.1.
  5. First time test: enter the first 5 numbers shown in the screen snapshot above.

Code Listing

<script runat="server" Language="c#">
public class blackscholes {
  public static int ITMAX = 100;
  public static double EPS = 3.0e-7; // don't use 3.0d-7

  static public double gammln(double xx) {
  double [] cof={76.18009173,-86.50532033,24.01409822,
  -1.231739516,0.120858003e-2,-0.536382e-5};
  int j;
  double x = xx - 1.0;
  double tmp = x + 5.5;
  tmp -= (x+0.5)*Math.Log(tmp);
  double ser=1.0;
  for (j=0;j<=5;j++) {
    x += 1.0;
    ser += cof[j]/x;
  }
return -tmp+Math.Log(2.50662827465*ser);
}
static public void gser(ref double gamser, double a, 
  double x, ref double gln) {
int n;
double sum,del,ap;
gln=gammln(a);
if (x <= 0.0) {
// if (x < 0.0) sumtout.Text = "An error occurred";

gamser=0.0;
return;
} else {
  ap=a;
  sum=1.0/a;
  del=sum;
  for (n=1;n<=ITMAX;n++) {
  ap += 1.0;
  del *= x/ap;
  sum += del;
  if (Math.Abs(del) < (Math.Abs(sum)*EPS)) {
    gamser=sum*Math.Exp(-x+a*(Math.Log(x))-gln);
   return;
  }
}
// sumtout.Text = "a too large, ITMAX too small in routine GSER";

return;
}
}
static public void gcf(ref double gammcf, double a, 
  double x, ref double gln) {
int n;
double gold=0.0,g,fac=1.0,b1=1.0;
double b0=0.0,anf,ana,an,a1,a0=1.0;
gln=gammln(a);
a1=x;
for (n=1;n<=ITMAX;n++) {
  an=(double) n;
  ana=an-a;
  a0=(a1+a0*ana)*fac;
  b0=(b1+b0*ana)*fac;
  anf=an*fac;
  a1=x*a0+anf*a1;
  b1=x*b0+anf*b1;
  if (a1 > 0.0 || a1 <0.0) {
    fac=1.0/a1;
  g=b1*fac;
  if (Math.Abs((g-gold)/g) < EPS) {
    gammcf=Math.Exp(-x+a*Math.Log(x)-(gln))*g;
  return;
  }
   gold=g;
 }
}
// cout<<"a too large, ITMAX too small in routine GCF"<<endl;

}
static public double gammp(double a,double x) {
double gamser = 0.0;
double gammcf = 0.0;
double gln = 0.0;
if (x < 0.0 || a <= 0.0) return 0.0; 
  //cout<<"Invalid arguments in routine GAMMP"<<endl;

if (x < (a+1.0)) {
gser(ref gamser,a,x,ref gln);
return gamser;
} else {
gcf(ref gammcf,a,x,ref gln);
return 1.0-gammcf;
}
}
static public double gammq(double a,double x)
{
   double gamser = 0.0;
   double gammcf = 0.0;
   double gln = 0.0;
   if (x < 0.0 || a <= 0.0) return 0.0; 
    //cout<<"Invalid arguments in routine GAMMQ"<<endl;

if (x < (a+1.0)) {
  gser(ref gamser,a,x,ref gln);
  return 1.0-gamser;
} else {
  gcf(ref gammcf,a,x,ref gln);
  return gammcf;
 }
}
static public double erf(double x)
{
  return x < 0.0 ? -gammp(0.5,x*x) : gammp(0.5,x*x);
}
static public double myerf(double argin) { 
return .5*(1.0+erf(argin/Math.Sqrt(2.0))); 
} 
static public double black_scholes(double s, double e, double rf, 
  double sigma, double time, ref double nd1, ref double nd2) {
  double num = Math.Log(s/e)+time*(rf+.5*sigma*sigma);
  double d1 = num/(sigma*Math.Sqrt(time));
  double d2 = d1 - sigma*Math.Sqrt(time);
  double c = s*myerf(d1) - e * myerf(d2) * Math.Exp(-rf*time);
  nd1 = myerf(d1);
  nd2 = myerf(d2);
return c;
}
/*
static public double erfc(double x){
return x < 0.0 ? 1.0+gammp(0.5,x*x) : gammq(0.5,x*x);
}
*/
}
void Page_Load() {
  FunctionPickL();
}
void PickL (object sender, EventArgs e) {
  FunctionPickL();
}
void FunctionPickL() {
try
{
  double sp = Convert.ToDouble(spt.Text);
  double ep = Convert.ToDouble(ept.Text);
  double rfr = Convert.ToDouble(rfrt.Text);
  double sigma = Convert.ToDouble(sigmat.Text);
  double time = Convert.ToDouble(timet.Text);
  double nd1=0.0;
  double nd2=0.0;
  double timey = time/365.0;
  double callp = blackscholes.black_scholes(sp, ep, 
    rfr, sigma, timey, ref nd1, ref nd2);
  double putp = ep / Math.Pow(1.0+rfr, timey) - sp + callp;
  double deltacall = nd1;
  double deltaput = nd1 - 1.0;
  valuecallt.Text = callp.ToString();
  valueputt.Text = putp.ToString();
  valuedeltacallt.Text = deltacall.ToString();
  valuedeltaputt.Text = deltaput.ToString();
} 
catch
{
  // lblAnswer.Text = "An error occurred";

}
}
</script>
<html>
<form runat="server">
<asp:button id="Pick" runat="server" 
  text=" Compute " Onclick="PickL" />
<img src="joblogo.bmp">
<body>
<br>
Stock Price
<td><asp:textbox id="spt" runat="server" /> </td>
</br>
<br>
Exercise Price
<td><asp:textbox id="ept" runat="server" /> </td>
</br>
<br>
Risk Free Rate of Interest
<td><asp:textbox id="rfrt" runat="server" /> </td>
</br>
<br>
Instantaneous Variance Rate of Stock's Return
<td><asp:textbox id="sigmat" runat="server" /> </td>
</br>
<br>
Time to Expiration of the Option(days)
<td><asp:textbox id="timet" runat="server" /> </td>
</br>
<br>
Value of Call Option
<td><asp:textbox id="valuecallt" runat="server" /> </td>
</br> 
<br>
Value of Put Option
<td><asp:textbox id="valueputt" runat="server" /> </td>
</br> 
<br>
Delta(calls)
<td><asp:textbox id="valuedeltacallt" runat="server" /> </td>
</br> 
<br>
Delta(puts)
<td><asp:textbox id="valuedeltaputt" runat="server" /> </td>
</br> 
</body>
</form>
</html>

Points of Interest

I learned that I have a lot to learn about writing web programs. I plan another version with :-

  1. enhanced error checking
  2. better display