아래는 “Black-Scholes” in Multiple Languages에서 일부를 옮겨왔습니다. 블로그 운영자인 Espen Gaarder Haug이 직접 개발한 소스뿐 아니라 인터넷에서 도움을 받은 자료들까지 망라하고 있습니다. 많이 사용하는 언어뿐 아니라 F#, Objective-C, Javascript, Perl, Python, O’calm까지 소개하고 있습니다. 원문을 보시면 더 많은 자료가 있습니다.
Black-Scholes in Visual Basic By Espen Gaarder Haug
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
'// The Black and Scholes (1973) Stock option formula Public Function BlackScholes(CallPutFlag As String, S As Double, X _ As Double, T As Double, r As Double, v As Double) As Double Dim d1 As Double, d2 As Double d1 = (Log(S / X) + (r + v ^ 2 / 2) * T) / (v * Sqr(T)) d2 = d1 - v * Sqr(T) If CallPutFlag = "c" Then BlackScholes = S * CND(d1) - X * Exp(-r * T) * CND(d2) ElseIf CallPutFlag = "p" Then BlackScholes = X * Exp(-r * T) * CND(-d2) - S * CND(-d1) End If End Function '// The cumulative normal distribution function Public Function CND(X As Double) As Double Dim L As Double, K As Double Const a1 = 0.31938153: Const a2 = -0.356563782: Const a3 = 1.781477937: Const a4 = -1.821255978: Const a5 = 1.330274429 L = Abs(X) K = 1 / (1 + 0.2316419 * L) CND = 1 - 1 / Sqr(2 * Application.Pi()) * Exp(-L ^ 2 / 2) * (a1 * K + a2 * K ^ 2 + a3 * K ^ 3 + a4 * K ^ 4 + a5 * K ^ 5) If X < 0 Then CND = 1 - CND End If End Function |
Black-Scholes in C++ By Espen Gaarder Haug
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#ifndef Pi #define Pi 3.141592653589793238462643 #endif // The Black and Scholes (1973) Stock option formula double BlackScholes(char CallPutFlag, double S, double X, double T, double r, double v) { double d1, d2; d1=(log(S/X)+(r+v*v/2)*T)/(v*sqrt(T)); d2=d1-v*sqrt(T); if(CallPutFlag == 'c') return S *CND(d1)-X * exp(-r*T)*CND(d2); else if(CallPutFlag == 'p') return X * exp(-r * T) * CND(-d2) - S * CND(-d1); } // The cumulative normal distribution function double CND( double X ) { double L, K, w ; double const a1 = 0.31938153, a2 = -0.356563782, a3 = 1.781477937; double const a4 = -1.821255978, a5 = 1.330274429; L = fabs(X); K = 1.0 / (1.0 + 0.2316419 * L); w = 1.0 - 1.0 / sqrt(2 * Pi) * exp(-L *L / 2) * (a1 * K + a2 * K *K + a3 * pow(K,3) + a4 * pow(K,4) + a5 * pow(K,5)); if (X < 0 ){ w= 1.0 - w; } return w; } |
Black-Scholes in JAVA By Espen Gaarder Haug
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// The Black and Scholes (1973) Stock option formula public double BlackScholes(char CallPutFlag, double S, double X, double T, double r, double v) { double d1, d2; d1=(Math.log(S/X)+(r+v*v/2)*T)/(v*Math.sqrt(T)); d2=d1-v*Math.sqrt(T); if (CallPutFlag=='c') { return S*CND(d1)-X*Math.exp(-r*T)*CND(d2); } else { return X*Math.exp(-r*T)*CND(-d2)-S*CND(-d1); } } // The cumulative normal distribution function public double CND(double X) { double L, K, w ; double a1 = 0.31938153, a2 = -0.356563782, a3 = 1.781477937, a4 = -1.821255978, a5 = 1.330274429; L = Math.abs(X); K = 1.0 / (1.0 + 0.2316419 * L); w = 1.0 - 1.0 / Math.sqrt(2.0 * Math.PI) * Math.exp(-L *L / 2) * (a1 * K + a2 * K *K + a3 * Math.pow(K,3) + a4 * Math.pow(K,4) + a5 * Math.pow(K,5)); if (X < 0.0) { w= 1.0 - w; } return w; } |
Black-Scholes in Java Script By Espen Gaarder Haug
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* The Black and Scholes (1973) Stock option formula */ function BlackScholes(PutCallFlag, S, X, T, r, v) { var d1, d2; d1 = (Math.log(S / X) + (r + v * v / 2.0) * T) / (v * Math.sqrt(T)); d2 = d1 - v * Math.sqrt(T); if (PutCallFlag== "c") return S * CND(d1)-X * Math.exp(-r * T) * CND(d2); else return X * Math.exp(-r * T) * CND(-d2) - S * CND(-d1); } /* The cummulative Normal distribution function: */ function CND(x){ var a1, a2, a3, a4 ,a5, k ; a1 = 0.31938153, a2 =-0.356563782, a3 = 1.781477937, a4= -1.821255978 , a5= 1.330274429; if(x<0.0) return 1-CND(-x); else k = 1.0 / (1.0 + 0.2316419 * x); return 1.0 - Math.exp(-x * x / 2.0)/ Math.sqrt(2*Math.PI) * k * (a1 + k * (-0.356563782 + k * (1.781477937 + k * (-1.821255978 + k * 1.330274429)))) ; } |
Black-Scholes in Perl By Jerome V. Braun
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
=head2 BlackScholes Routine to implement the Black and Scholes (1973) option pricing formula. # usage $price = GBlackScholes($call_put_flag, $S, $X, $T, $r, $b, $v); Here C<$call_put_flag> is either 'c' or 'p' for a call or put respectively, =cut sub BlackScholes { my ($call_put_flag, $S, $X, $T, $r, $v) = @_; # calculate some auxiliary values my $d1 = ( log($S/$X) + ($r+$v**2/2)*$T ) / ( $v * $T**0.5 ); my $d2 = $d1 - $v * $T**0.5; if ($call_put_flag eq 'c') { return $S * &CND($d1) - $X * exp( -$r * $T ) * &CND($d2); } else { # ($call_put_flag eq 'p') return $X * exp( -$r * $T ) * &CND(-$d2) - $S * &CND(-$d1); } } =head2 CND Approximate the cumulative normal distribution. That is, the value of the integral of the standard normal density from minus infinity to C<$x>. # usage $p = &CND($x); =cut sub CND { my $x = shift; # the percentile under consideration my $Pi = 3.141592653589793238; # Taylor series coefficients my ($a1, $a2, $a3, $a4, $a5) = (0.319381530, -0.356563782, 1.781477937, -1.821255978, 1.330274429); # use symmetry to perform the calculation to the right of 0 my $L = abs($x); my $k = 1/( 1 + 0.2316419*$L); my $CND = 1 - 1/(2*$Pi)**0.5 * exp(-$L**2/2) * ($a1*$k + $a2*$k**2 + $a3*$k**3 + $a4*$k**4 + $a5*$k**5); # then return the appropriate value return ($x >= 0) ? $CND : 1-$CND; } |
Black-Scholes in Delphi/Pascal By Desmond Nolan, Advanced Business Continuity Systems, Inc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
function BlackScholes(CallPutFlag : string; S, X, T, r, v : Double) : Double; var d1, d2 : Double; begin Result := 0; d1 := (LN(S / X) + (r + Power(v, 2) / 2) * T) / (v * SqRt(T)); d2 := d1 - v * SqRt(T); if CallPutFlag = 'c' then Result := S * CND(d1) - X * Exp(-r * T) * CND(d2) else if CallPutFlag = 'p' then Result := X * Exp(-r * T) * CND(-d2) - S * CND(-d1); end; {The cumulative normal distribution function} function CND(X : Double) : Double; var L, K : Double; const a1 = 0.31938153; a2 = -0.356563782; a3 = 1.781477937; a4 = -1.821255978; a5 = 1.330274429; begin L := Abs(X); K := 1 / (1 + 0.2316419 * L); Result := 1 - 1 / SqRt(2 * Pi) * Exp(-Power(L, 2) / 2) * (a1 * K + a2 * Power(K, 2) + a3 * Power(K, 3) + a4 * Power(K, 4) + a5 * Power(K, 5)); if X < 0 then Result := (1 - Result) end; |
Black-Scholes in Python By Andy Smith
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
from math import * # Cumulative normal distribution def CND(X): (a1,a2,a3,a4,a5) = (0.31938153, -0.356563782, 1.781477937, -1.821255978, 1.330274429) L = abs(X) K = 1.0 / (1.0 + 0.2316419 * L) w = 1.0 - 1.0 / sqrt(2*pi)*exp(-L*L/2.) * (a1*K + a2*K*K + a3*pow(K,3) + a4*pow(K,4) + a5*pow(K,5)) if X<0: w = 1.0-w return w # Black Sholes Function def BlackSholes(CallPutFlag,S,X,T,r,v): d1 = (log(S/X)+(r+v*v/2.)*T)/(v*sqrt(T)) d2 = d1-v*sqrt(T) if CallPutFlag=='c': return S*CND(d1)-X*exp(-r*T)*CND(d2) else: return X*exp(-r*T)*CND(-d2)-S*CND(-d1) |
Black-Scholes in Fortran By Lance McKinzie and John Matovu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
! The Black and Scholes (1973) Stock option formula Real*8 Function BlackScholes(CallPutFlag, S, X, T, r, v) character*1 CallPutFlag real*8 S,X,T,r,v real*8 d1, d2 d1 = (Log(S / X) + (r + v**2. / 2.) * T) / (v * Sqrt(T)) d2 = d1 - v * Sqrt(T) If (CallPutFlag.eq.'c') Then BlackScholes = S * CND(d1) - X * Exp(-r * T) * CND(d2) ElseIf( CallPutFlag.eq.'p') Then BlackScholes = X * Exp(-r * T) * CND(-d2) - S * CND(-d1) End If Return End ! The cumulative normal distribution function Real*8 Function CND(X) PARAMETER (DPI=3.141592653589793238D0) real*8 X real*8 L, K real*8 a1,a2,a3,a4,a5 a1 = 0.31938153 a2 = -0.356563782 a3 = 1.781477937 a4 = -1.821255978 a5 = 1.330274429 L = Abs(X) K = 1. / (1. + 0.2316419 * L) CND = 1. -1./Sqrt(2. * DPI) * Exp(-L**2. / 2.) * 1 (a1 * K + a2 * K**2. + a3 * K**3. + a4 * K**4. + a5 * K**5.) If (X.lt.0.) Then CND = 1. - CND End If Return End |
Black-Scholes in php By Franiatte Xavier
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
function CND ($x) { $Pi = 3.141592653589793238; $a1 = 0.319381530; $a2 = -0.356563782; $a3 = 1.781477937; $a4 = -1.821255978; $a5 = 1.330274429; $L = abs($x); $k = 1 / ( 1 + 0.2316419 * $L); $p = 1 - 1 / pow(2 * $Pi, 0.5) * exp( -pow($L, 2) / 2 ) * ($a1 * $k + $a2 * pow($k, 2) + $a3 * pow($k, 3) + $a4 * pow($k, 4) + $a5 * pow($k, 5) ); if ($x >= 0) { return $p; } else { return 1-$p; } } function BlackScholes ($call_put_flag, $S, $X, $T, $r, $v) { $d1 = ( log($S / $X) + ($r + pow($v, 2) / 2) * $T ) / ( $v * pow($T, 0.5) ); $d2 = $d1 - $v * pow($T, 0.5); if ($call_put_flag == 'c') { return $S * CND($d1) - $X * exp( -$r * $T ) * CND($d2); } else { return $X * exp( -$r * $T ) * CND(-$d2) - $S * CND(-$d1); } |
Black-Scholes in O’Caml By Andrey A. Kolessa, OILspace inc. Moscow Office
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
(* Objective Caml is a fast modern type-inferring functional programming language descended from the ML (Meta Language) family. O'Caml is as fast as C/C++ http://www.ocaml.org/ *) let pow x n = exp ((float_of_int n) *. log(x) ) ;; (* The cumulative normal distribution function *) let cnd x = let a1 = 0.31938153 and a2= -0.356563782 and a3=1.781477937 and a4= -1.821255978 and a5=1.330274429 in let pi = 4.0 *. atan 1.0 in let l = abs_float(x) in let k = 1.0 /. (1.0 +. 0.2316419 *. l) in let w = ref (1.0-.1.0/.sqrt(2.0*.pi)*.exp(-.l*.l/.2.0)*.(a1*.k+.a2* .k*.k+.a3*. (pow k 3)+.a4*.(pow k 4)+.a5*.(pow k 5))) in if (x < 0.0) then w := 1.0 -. !w ; !w (* The Black and Scholes (1973) Stock option formula *) let black_scholes call_put_flag s x t r v = let d1=(log(s /. x) +. (r+.v*.v/.2.0)*.t)/.(v*.sqrt(t)) in let d2=d1-.v*.sqrt(t) in let res = ref 0.0 in if (call_put_flag == 'c') then res := s*.cnd(d1)-.x*.exp(-.r*.t)*.cnd(d2) else res := x*.exp(-.r*.t)*.cnd(-.d2)-.s*.cnd(-.d1); !res |
Black-Scholes in Transact SQL By Nazy Norouzy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
--Black Scholes Function: create Function BlackScholes(@CallPutFlag varchar(100), @S float, @X float, @T float, @r float, @v float) returns float as begin declare @d1 float declare @d2 float declare @BS float set @d1 = (Log(@S / @X) + (@r + power(@v,2) / 2) * @T) / (@v * Sqrt(@T)) set @d2 = @d1 - @v * Sqrt(@T) If @CallPutFlag = 'c' begin set @BS = @S * dbo.CND(@d1) - @X * Exp(-@r * @T) * dbo.CND(@d2) end else If @CallPutFlag = 'p' begin set @BS = @X * Exp(-@r * @T) * dbo.CND(-@d2) - @S * dbo.CND(-@d1) End return @BS End ------------------------------------------ -- The cumulative normal distribution function: create Function CND(@X float) returns float as begin declare @L float declare @K float declare @a1 float declare @a2 float declare @a3 float declare @a4 float declare @a5 float set @a1 = 0.31938153 set @a2 = -0.356563782 set @a3 = 1.781477937 set @a4 = -1.821255978 set @a5 = 1.330274429 set @L = Abs(@X) set @K = 1 / (1 + 0.2316419 * @L) declare @CND1 float set @CND1 = 1 - 1 / Sqrt(2 * Pi()) * Exp(-power(@L,2) / 2) * (@a1 * @K + @a2 * power(@K,2) + @a3 * power(@K,3) + @a4 * power(@K,4) + @a5 * power(@K,5)) If @X < 0 begin set @CND1 = 1 - @CND1 End return @CND1 End |
Black-Scholes in C# By Robert Derby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
namespace BlackScholes { /// <summary> /// Summary description for BlackSholes. /// </summary> public class BlackSholes { public BlackSholes() { // // TODO: Add constructor logic here // } /* The Black and Scholes (1973) Stock option formula * C# Implementation * uses the C# Math.PI field rather than a constant as in the C++ implementaion * the value of Pi is 3.14159265358979323846 */ public double BlackScholes(string CallPutFlag, double S, double X, double T, double r, double v) { double d1 = 0.0; double d2 = 0.0; double dBlackScholes = 0.0; d1 = (Math.Log(S / X) + (r + v * v / 2.0) * T) / (v * Math.Sqrt(T)); d2 = d1 - v * Math.Sqrt(T); if (CallPutFlag == "c") { dBlackScholes = S * CND(d1) - X * Math.Exp(-r * T) * CND(d2); } else if (CallPutFlag == "p") { dBlackScholes = X * Math.Exp(-r * T) * CND(-d2) - S * CND(-d1); } return dBlackScholes; } public double CND(double X) { double L = 0.0; double K = 0.0; double dCND = 0.0; const double a1 = 0.31938153; const double a2 = -0.356563782; const double a3 = 1.781477937; const double a4 = -1.821255978; const double a5 = 1.330274429; L = Math.Abs(X); K = 1.0 / (1.0 + 0.2316419 * L); dCND = 1.0 - 1.0 / Math.Sqrt(2 * Convert.ToDouble(Math.PI.ToString())) * Math.Exp(-L * L / 2.0) * (a1 * K + a2 * K * K + a3 * Math.Pow(K, 3.0) + a4 * Math.Pow(K, 4.0) + a5 * Math.Pow(K, 5.0)); if (X < 0) { return 1.0 - dCND; } else { return dCND; } } } } |
Black-Scholes in PL/SQL By Fernardo Casteras, Bunos Aires, Argentina
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
CREATE OR REPLACE FUNCTION BLACKSCHOLES ( CALLPUTFLAG IN VARCHAR2, S IN NUMBER, X IN NUMBER, T IN NUMBER, R IN NUMBER, V IN NUMBER ) RETURN NUMBER IS -- D1 NUMBER; D2 NUMBER; PI NUMBER := 3.141592653589793238462643; RESULT NUMBER; -- FUNCTION CND ( X NUMBER ) RETURN NUMBER IS -- L NUMBER; K NUMBER; A1 NUMBER := 0.31938153; A2 NUMBER := -0.356563782; A3 NUMBER := 1.781477937; A4 NUMBER := -1.821255978; A5 NUMBER := 1.330274429; RESULT NUMBER; -- BEGIN -- L := ABS(X); K := 1 / (1 + 0.2316419 * L); RESULT := 1 - 1 / SQRT(2 * PI) * EXP(-POWER(L, 2) / 2) * (A1 * K + A2 * POWER(K, 2) + A3 * POWER(K, 3) + A4 * POWER(K, 4) + A5 * POWER(K, 5)); IF ( X < 0 ) THEN RESULT := (1 - RESULT); END IF; -- RETURN RESULT; -- END CND; -- BEGIN -- RESULT := 0; D1 := (LN(S / X) + (R + POWER(V, 2) / 2) * T) / (V * SQRT(T)); D2 := D1 - V * SQRT(T); IF ( CALLPUTFLAG = 'C' ) THEN RESULT := S * CND(D1) - X * EXP(-R * T) * CND(D2); ELSIF ( CALLPUTFLAG = 'P' ) THEN RESULT := X * EXP(-R * T) * CND(-D2) - S * CND(-D1); END IF; -- RETURN RESULT; -- END; |
Black-Scholes in Ruby by Michael Neumann, Germany
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# one-to-one translation from Python example # Cumulative normal distribution def cnd(x) a1, a2, a3, a4, a5 = 0.31938153, -0.356563782, 1.781477937, -1.821255978, 1.330274429 l = x.abs k = 1.0 / (1.0 + 0.2316419 * l) w = 1.0 - 1.0 / Math.sqrt(2*Math::PI)*Math.exp(-l*l/2.0) * (a1*k + a2*k*k + a3*(k**3) + a4*(k**4) + a5*(k**5)) w = 1.0 - w if x < 0 return w end def BlackScholes(callPutFlag, s, x, t, r, v) d1 = (Math.log(s/x)+(r+v*v/2.0)*t)/(v*Math.sqrt(t)) d2 = d1-v*Math.sqrt(t) if callPutFlag == 'c' s*cnd(d1)-x*Math.exp(-r*t)*cnd(d2) else x*Math.exp(-r*t)*cnd(-d2)-s*cnd(-d1) end end |
Black-Scholes in VB.NET By Marco Sturlese,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
'// The Black and Scholes (1973) Stock option formula Public Function BlackScholes(ByVal CallPutFlag As String, ByVal S As Double, ByVal X _ As Double, ByVal T As Double, ByVal r As Double, ByVal v As Double) As Double Dim d1 As Double, d2 As Double d1 = (Math.Log(S / X) + (r + v ^ 2 / 2) * T) / (v * Math.Sqrt(T)) d2 = d1 - v * Math.Sqrt(T) If CallPutFlag = "c" Then BlackScholes = S * CND(d1) - X * Math.Exp(-r * T) * CND(d2) ElseIf CallPutFlag = "p" Then BlackScholes = X * Math.Exp(-r * T) * CND(-d2) - S * CND(-d1) End If End Function '// The cumulative normal distribution function Public Function CND(ByVal X As Double) As Double Dim L As Double, K As Double Const a1 = 0.31938153 : Const a2 = -0.356563782 : Const a3 = 1.781477937 Const a4 = -1.821255978 : Const a5 = 1.330274429 L = Math.Abs(X) K = 1 / (1 + 0.2316419 * L) CND = 1 - 1 / Math.Sqrt(2 * Math.PI) * Math.Exp(-L ^ 2 / 2) * (a1 * K + a2 * K ^ 2 + a3 * K ^ 3 + a4 * K ^ 4 + a5 * K ^ 5) If X < 0 Then CND = 1 - CND End If End Function |
Black-Scholes in F# By Michael de la Maza
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#light // This code was transliterated from the OCAML code // located here http://www.espenhaug.com/black_scholes.html let pow x n = exp (n * log(x) ) let cnd x = let a1 = 0.31938153 let a2 = -0.356563782 let a3 = 1.781477937 let a4 = -1.821255978 let a5 = 1.330274429 let pi = 4.0 * atan 1.0 let l = abs(x) let k = 1.0 / (1.0 + 0.2316419 * l) let w = ref (1.0-1.0/sqrt(2.0*pi)*exp(-l*l/2.0)*(a1*k+a2*k*k+a3*(pow k 3.0)+a4*(pow k 4.0)+a5*(pow k 5.0))) if (x < 0.0) then w := 1.0 - !w !w // call_put_flag: 'c' if call option; otherwise put option // s: stock price // x: strike price of option // t: time to expiration in years // r: risk free interest rate // v: volatility let black_scholes call_put_flag s x t r v = let d1=(log(s / x) + (r+v*v/2.0)*t)/(v*sqrt(t)) let d2=d1-v*sqrt(t) let res = ref 0.0 if (call_put_flag = 'c') then res := s*cnd(d1)-x*exp(-r*t)*cnd(d2) else res := x*exp(-r*t)*cnd(-d2)-s*cnd(-d1) !res Example usage:: > black_scholes 'c' 60.0 65.0 0.25 0.08 0.3;; val it : float = 2.133371862 > black_scholes 'p' 60.0 65.0 0.25 0.08 0.3;; val it : float = 5.846285627 |
Black-Scholes in Objective-C/iPhone By Paul J. Sholtz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
HEADER FILE/DECLARATION: -(double)BlackSholes:(char)CallPutFlag: (double)S: (double)X: (double)T: (double)r: (double)v; -(double)CND:(double)X; IMPLEMENTATION: #ifndef Pi #define Pi 3.141592653589793238462643 #endif -(double)BlackSholes:(char)CallPutFlag: (double)S: (double)X: (double)T: (double)r: (double)v { double d1, d2; d1=(log(S/X)+(r+v*v/2)*T)/(v*sqrt(T)); d2=d1-v*sqrt(T); if(CallPutFlag == 'c') return S *[self CND:d1]-X * exp(-r*T)*[self CND:d2]; if(CallPutFlag == 'p') return X * exp(-r * T) * [self CND:-d2] - S * [self CND:-d1]; // error flag; [NSException raise:@"Call/Put Error" format:@"Option must be either a call or put"]; } -(double)CND:(double)X { double L, K, w ; double const a1 = 0.31938153, a2 = -0.356563782, a3 = 1.781477937; double const a4 = -1.821255978, a5 = 1.330274429; L = fabs(X); K = 1.0 / (1.0 + 0.2316419 * L); w = 1.0 - 1.0 / sqrt(2 * Pi) * exp(-L *L / 2) * (a1 * K + a2 * K *K + a3 * pow(K,3) + a4 * pow(K,4) + a5 * pow(K,5)); if (X < 0 ){ w= 1.0 - w; } return w; } INVOCATION (LOGGING ANSWER TO CONSOLE): (models a call option, equity price 50, strike price 45, time to expiry 1/3 of a year, 8% risk free rate and 30% volatility) char flag = 'p'; double S = 50.0; double X = 45.0; double T = 1./3.; double r = 0.08; double v = 0.3; double option_value = [self BlackSholes:flag :S :X :T :r :v]; NSLog(@"option value: %f",option_value); |