using System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Diagnostics;
using
System.Web;
using
System.Web.Services;
namespace
SyncadiaWeb
{
/// <summary>
/// Summary
description for BlackScholes.
/// </summary>
[WebService(Description="This
Syncadia quick sample, demonstrates the Black Scholes
equationimplemented as a web service. Both methods of
this service take S (the stock price), X (the strike price), T (the time frame, a value of
0.25 for 3 months), r (risk free interest rate,
say 8% represented as 0.08), v (the volatility, say
25% expressed as 0.25)",Namespace="www.syncadia.com")]
public class
BlackScholes : System.Web.Services.WebService
{
public enum
OptionType {Call,Put};
private const
Double a1 = 0.31938153;
private const
Double a2 = -0.356563782;
private const
Double a3 = 1.781477937;
private const
Double a4 = -1.821255978;
private const
Double a5 = 1.330274429;
public BlackScholes()
{
}
public static
Double CumNormDist(Double x)
{
Double res = 0D;
Double L = Math.Abs(x);
Double k = 1/(1 + 0.2316419 * L);
res = 1 - 1/Math.Sqrt(2 * Math.PI) * Math.Exp(-Math.Pow(L,2)/2) *
(a1 * k + a2 * Math.Pow(k,2) +
a3 * Math.Pow(k,3) +
a4 * Math.Pow(k,4) +
a5 * Math.Pow(k,5));
return x < 0 ? 1 - res :
res;
}
[WebMethod(Description="Calculates the result of the Black Scholes
equation for a <B>Call</B> option")]
public Double BlackScholesCall(Double S, Double
X, Double T, Double r, Double v)
{
return
CalculateBlackScholes(OptionType.Call,S,X,T,r,v);
}
[WebMethod(Description="Calculates the result of the Black Scholes
equation for a <B>Put</B> option")]
public Double BlackScholesPut(Double S, Double
X, Double T, Double r, Double v)
{
return
CalculateBlackScholes(OptionType.Put,S,X,T,r,v);
}
public Double CalculateBlackScholes(OptionType
otType, Double S, Double X,
Double T, Double r, Double v)
{
Double d1 = (Math.Log(S/X) + (r + Math.Pow(v,2)/2)*T)/ (v *
Math.Sqrt(T));
Double d2 = d1 - v * Math.Sqrt(T);
if (otType ==
OptionType.Call)
return S * CumNormDist(d1) - X *
Math.Exp(-r * T) * CumNormDist(d2);
else if (otType ==
OptionType.Put)
return X * Math.Exp(-r * T) *
CumNormDist(-d2) - S * CumNormDist(-d1);
else
return -1;
}
|