Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET
Article

Black-Scholes Option Pricing

Rate me:
Please Sign up or sign in to vote.
2.82/5 (9 votes)
9 Jun 20041 min read 97.3K   757   31   16
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. Image 1

  6. First time test: enter the first 5 numbers shown in the screen snapshot above.

Code Listing

C#
<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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionConfusing variable names Pin
Jeson Martajaya17-Mar-14 10:36
Jeson Martajaya17-Mar-14 10:36 
GeneralMy vote of 1 Pin
Dmitri Nеstеruk28-Feb-12 0:03
Dmitri Nеstеruk28-Feb-12 0:03 
GeneralValue of put option Pin
~MyXa~26-Oct-08 18:11
~MyXa~26-Oct-08 18:11 
GeneralReferences “Financial Engineering” ... Pin
Serge Barbeau16-Aug-06 14:24
Serge Barbeau16-Aug-06 14:24 
QuestionEuropean or American options? Pin
eyoung7010-Jun-04 14:59
eyoung7010-Jun-04 14:59 
AnswerRe: European or American options? Pin
pwesson27-Sep-04 6:11
pwesson27-Sep-04 6:11 
GeneralError checking Pin
Labrat00210-Jun-04 13:27
Labrat00210-Jun-04 13:27 
GeneralRe: Error checking Pin
Landon M. Kelsey III10-Jun-04 13:37
Landon M. Kelsey III10-Jun-04 13:37 
GeneralRe: Error checking Pin
Labrat00210-Jun-04 13:45
Labrat00210-Jun-04 13:45 
GeneralRe: Error checking Pin
Landon M. Kelsey III10-Jun-04 13:49
Landon M. Kelsey III10-Jun-04 13:49 
GeneralRe: Error checking Pin
Peter Hancock10-Jun-04 15:31
Peter Hancock10-Jun-04 15:31 
GeneralRe: Error checking Pin
ifrcurrent10-Jun-04 15:45
ifrcurrent10-Jun-04 15:45 
GeneralRe: Error checking Pin
Peter Hancock10-Jun-04 16:00
Peter Hancock10-Jun-04 16:00 
GeneralGood Idea Pin
Daniel Petersen10-Jun-04 8:27
Daniel Petersen10-Jun-04 8:27 
GeneralRe: Good Idea Pin
Landon M. Kelsey III10-Jun-04 8:46
Landon M. Kelsey III10-Jun-04 8:46 
GeneralRe: Good Idea Pin
l a u r e n10-Jun-04 11:55
l a u r e n10-Jun-04 11:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.