Back Next

The Black and Scholes Model
(Option Pricing Theory)

for an explanation see: 

http://www.apnet.com/catalog/0125649150/chp7-1.html 

http://www.contingencyanalysis.com/glossaryblackscholestheory.htm 

http://bradley.edu/~arr/bsm/model.html 

Credits: The bulk of this program was written by someone on the comp.lang.java.help newsgroup. All I did was debug it for them (this involved translating from Italian, doing some research, spotting obvious mistakes and the inevitable system.out.print.lns).

///////////////////////////////////////////////////////////////
// scholes.java

import java.awt.*; 
import java.awt.event.*;
import java.lang.Math;
import javax.swing.*;
import java.text.*;

public class scholes extends JApplet implements ActionListener

private final int FRAME_WIDTH=700;
private final int FRAME_HEIGHT=100;
private double K, So, rf, t, sigma, S, d1, d2, Nd1, Nd2;
private JTextField SoField, KField, rfField, tField, sigmaField,resultField;
/*
sigma=standard deviation of stock returns
K=option striking price
S=current stock price
rf=risk-free interest rate
N=cumulative standard normal distribution
t is the time to option expiration
Nd1 is the density function of the normal distribution function 
Nd2 is the (calculus) derivative of Nd1
SN(d1) derives the expected benefit from acquiring a stock outright
So*e to the power of(-rt)N(d2) gives the present value of paying the exercise price on the expiration day
So=option striking price
*/
private JButton calcButton;
private JPanel inputPanel;
private Container contentPane;
private DecimalFormat precisionThree;

public void init()

precisionThree=new DecimalFormat("0.000");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
SoField = new JTextField("1200",10);
KField = new JTextField("1360",10);
rfField = new JTextField("0.1",10);
tField = new JTextField("0.5",10);
sigmaField = new JTextField("0.15",10);
resultField = new JTextField (20);
resultField.setEditable(false);
calcButton = new JButton("Calculate");
calcButton.addActionListener(this);
inputPanel = new JPanel ();
inputPanel.add (new JLabel("So:"));
inputPanel.add (SoField);
inputPanel.add (new JLabel("K:"));
inputPanel.add (KField);
inputPanel.add (new JLabel("rf:"));
inputPanel.add (rfField);
inputPanel.add (new JLabel("t:"));
inputPanel.add (tField);
inputPanel.add (new JLabel("sigma:"));
inputPanel.add (sigmaField);
inputPanel.add (new JLabel("The price is:"));
inputPanel.add (resultField);
inputPanel.add (calcButton);
contentPane = getContentPane();
contentPane.add(inputPanel, "Center");
}

private double calculateprice()

try
{
d1 = (Math.log(So/(K/(1+rf*t)))/(sigma*Math.sqrt(t)))+(1/2*sigma*Math.sqrt(t));
if (d1>=2) throw new InvalidNumberException();
d2 = d1-(sigma*Math.sqrt(t)); 
if (d2>=2) throw new InvalidNumberException();
Nd1=1/2+(1/Math.sqrt(2*Math.PI))*(d1/(1+(d1*d1)/6));
Nd2=1/2+(1/Math.sqrt(2*Math.PI))*(d2/(1+(d2*d2)/6));
S=(So*Nd1)-((K/(1+(rf*t)))*Nd2);
return S; 
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.toString());
}
return 0.0;
}

public void actionPerformed(ActionEvent event)

try
{
So = (double)Double.valueOf(SoField.getText()).doubleValue(); 
K=(double)Double.valueOf(KField.getText()).doubleValue();
rf = (double)Double.valueOf(rfField.getText()).doubleValue();
t = (double)Double.valueOf(tField.getText()).doubleValue();
sigma = (double)Double.valueOf(sigmaField.getText()).doubleValue();
S = calculateprice();
resultField.setText(""+precisionThree.format(S));
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.toString());
}
}
}

///////////////////////////////////////////////////////////////
// InvalidNumberException.java
public class InvalidNumberException extends ArithmeticException
{
    public InvalidNumberException()
        {
            super("number out of range");
        }
}
////////////////////////////THE HTML////////////////////
<HTML>
<HEAD>
<TITLE>HTML</TITLE>
</HEAD>
<BODY>
<p align="center">
<APPLET CODE="scholes.class" WIDTH=724 HEIGHT=106></APPLET>
</BODY>
</HTML>
///////////////////////////////////////////////////////////////

Up