Black Scholes formula

Modified on 2013/05/11 09:45 by Eugene — Categorized as: Community Components

Syntax


public enum CalcEx.CallPutFlag { Call, Put };
public static double BlackScholes(this double S, double X, double D, double R, double V, CallPutFlag flag)

public enum CallPutFlag { Call, Put }; public static double BlackScholes(CallPutFlag flag, double S, double X, double D, double R, double V)

Parameter Description

flagCall or Put option - pass either CallPutFlag.Call or CallPutFlag.Put
SStock price
XStrike price of option
DDays to expiry
RRisk-free rate, %
VVolatility, %

Description

This Black Scholes formula is courtesy espenhaug.com, slightly modified by Eugene.

Example

Code below illustrates a couple of examples illustrating the Black Scholes formula usage:

Example using C# extension methods:


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using Community.Components;

namespace WealthLab.Strategies { public class MyStrategy : WealthScript { private string str (double s) { return String.Format("{0:0.00}", s); } protected override void Execute() { double price = Bars.Close[Bars.Count-1]; if( price < 10 ) Abort(); double call = price.BlackScholes( price + 5.0d, 60, 1d, 20.0, CalcEx.CallPutFlag.Call ); // days=60, rate=1%, volatility=20%=0.20 double put = price.BlackScholes( price - 5.0d, 60, 1d, 20.0, CalcEx.CallPutFlag.Put ); // days=60, rate=1%, volatility=20%=0.20 PrintDebug( string.Concat( "Call = ", str(call), " Put = ", str(put) ) ); } } }

Legacy syntax example:


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using Community.Components;

namespace WealthLab.Strategies { public class MyStrategy : WealthScript { private string str (double s) { return String.Format("{0:0.00}", s); } protected override void Execute() { double price = Bars.Close[Bars.Count-1]; if( price < 10 ) Abort(); double call = Calculate.BlackScholes( Calculate.CallPutFlag.Call, price, price + 5.0d, 60, 1d, 20.0 ); // days=60, rate=1%, volatility=20%=0.20 double put = Calculate.BlackScholes( Calculate.CallPutFlag.Put, price, price - 5.0d, 60, 1d, 20.0 ); // days=60, rate=1%, volatility=20%=0.20 PrintDebug( string.Concat( "Call = ", str(call), " Put = ", str(put) ) ); } } }