Black Scholes Option Call Pricing Perl Script



Black Scholes Option Call Pricing Script

This perl script uses the Black Scholes formula for pricing a call option.

perl script based on Kevin Bracker's youtube worked example

Youtube: Black-Scholes Option Pricing Model -- Intro and Call Example

formula used is as per the youtube video

Disclaimer: script for illustration purposes only

Youtube video on Black Scholes

Reference files for video:

Bracker-StNormTable

Bracker-BSOPM

Bracker-BSOPMSpread

black scholes call option valuation

Sample Output

black_scholes_calc.pl OUTPUT:



stock_price is : 62.00

exercise_price is : 60.00

time_to_expire_days is : 40

volatility_percent is : 32

risk_fee_rate_percent is : 4



log_stock_exercise_price_ratio is: 0.032789822822991
time_to_expire_years is: 0.10958904109589
volatility_decimal is: 0.32



half_volatility_squared 0.0512

volatility_multiplied_by_root_time_to_expire_years 0.105933553741103

formula_bottom is: 0.105933553741103

formula_top_right is: 0.0099945205479452

formula_top is: 0.0427843433709362



d1 is: 0.403879053047717

d2 is: 0.297945499306614

Nd1 is 0.65685

Nd2 is 0.61713


e_value is 0.995626032139075



Calculated option call price is: $3.86

Due to rounding, value will vary from manual worked example


Calculated Delta is: 0.65685

Calculated Gamma is: 0.05598

Calculated Theta is: -12.49301

Calculated Vega is: 7.54683

Calculated Rho is: 4.04009


End of run black_scholes_calc.pl


Reference text file black_scholes.txt

Contents of reference file...
type black_scholes.txt

stock_price:62.00
exercise_price:60.00
time_to_expire_days:40
volatility_percent:32
risk_fee_rate_percent:4


Perl Script

#!/usr/bin/perl

# black_scholes_calc.pl script...
# 3rd March 2016

# Script made deliberately wordy to show workings.

#perl script based on Kevin Bracker's youtube worked example
#Youtube: Black-Scholes Option Pricing Model -- Intro and Call Example
#formula used is as per the youtube video
#Disclaimer: script for illustration purposes only

#File contents of reference text file black_scholes.txt:
#stock_price:62.00
#exercise_price:60.00
#time_to_expire_days:40
#volatility_percent:40
#risk_fee_rate_percent:4

use Statistics::Distributions;

print "\nblack_scholes_calc.pl OUTPUT:\n\n";

#open reference file and read...
open(DATA_IN,"<black_scholes.txt") || die "Couldn't open file properly";


while(<DATA_IN>){

	#remove newline
	chomp $_;
	print "\n";

	$al_string = $_;

	if($al_string =~ /(stock_price):(.*)/)
	{
	$stock_price = $2;
   	print "\nstock_price is : $stock_price";
	}

	if($al_string =~ /(exercise_price):(.*)/)
	{
	$exercise_price = $2;
	print "\nexercise_price is : $exercise_price";
	}

	if($al_string =~ /(time_to_expire_days):(.*)/)
	{
	$time_to_expire_days = $2;
	print "\ntime_to_expire_days is : $time_to_expire_days";
	}

	if($al_string =~ /(volatility_percent):(.*)/)
	{
   	$volatility_percent = $2;
   	print "\nvolatility_percent is : $volatility_percent";
	}

	if($al_string =~ /(risk_fee_rate_percent):(.*)/)
	{
   	$risk_fee_rate_percent = $2;
   	print "\nrisk_fee_rate_percent is : $risk_fee_rate_percent";
	}

}#...while


print "\n\n";
$log_stock_exercise_price_ratio = log($stock_price/$exercise_price);
print "\n\nlog_stock_exercise_price_ratio is: $log_stock_exercise_price_ratio";


$time_to_expire_years = $time_to_expire_days / 365;
print "\ntime_to_expire_years is: $time_to_expire_years";

$volatility_decimal = $volatility_percent / 100;
print "\nvolatility_decimal is: $volatility_decimal";

$risk_fee_rate_decimal = $risk_fee_rate_percent / 100;

print "\n\n";
$half_volatility_squared = 0.5 * $volatility_decimal * $volatility_decimal;
print "\n\nhalf_volatility_squared $half_volatility_squared";

$volatility_multiplied_by_root_time_to_expire_years = $volatility_decimal * sqrt($time_to_expire_years);
print "\n\nvolatility_multiplied_by_root_time_to_expire_years $volatility_multiplied_by_root_time_to_expire_years";

$formula_bottom = $volatility_multiplied_by_root_time_to_expire_years;
print "\n\nformula_bottom is: $formula_bottom";

$formula_top_right = ($risk_fee_rate_decimal + $half_volatility_squared) * $time_to_expire_years;
print "\n\nformula_top_right is: $formula_top_right";

$formula_top = $log_stock_exercise_price_ratio + $formula_top_right;
print "\n\nformula_top is: $formula_top\n\n";


$d1 = $formula_top / $formula_bottom;
print "\n\nd1 is: $d1";

$d2 = $d1 -$volatility_multiplied_by_root_time_to_expire_years;
print "\n\nd2 is: $d2";


# for cumulative standard normal distribution
# the equivalent of excel function NORMDIST(a,b,c,d)
# code used from a perl forum...

my ($mean, $stdev) = (0, 1); 

#finding the N value for d1...
my $z = ( $d1 - $mean)/$stdev; 
my $Nd1 = 1 - Statistics::Distributions::uprob($z);

print "\n\nNd1 is $Nd1";


#finding the N value for d2...
my $z = ( $d2 - $mean)/$stdev; 
my $Nd2 = 1 - Statistics::Distributions::uprob($z); 

print "\n\nNd2 is $Nd2\n";


$e_value = exp( -$risk_fee_rate_decimal * $time_to_expire_years);

print "\n\ne_value is $e_value";

$call_price = ($stock_price * $Nd1) - ($exercise_price * $Nd2 * $e_value);

print "\n\n\n\n";

printf("Calculated option call price is: \$%.2f", $call_price); 
print "\n\n";

print "Due to rounding, value will vary from manual worked example\n";

$delta = $Nd1;

print "\n\n";
printf("Calculated Delta is: %.5f", $delta);

$pi = 4 * atan2 1, 1;

$top_gamma = (1/(sqrt(2*$pi))) * exp(-1*(($d1 * $d1)/2)) ;

$bottom_gamma = $stock_price * $volatility_decimal * sqrt($time_to_expire_years);

$gamma = $top_gamma / $bottom_gamma;

print "\n\n";
printf("Calculated Gamma is: %.5f", $gamma);

$theta=(-1*(($stock_price)*((1/(sqrt(2*$pi)))*exp(-1*((($d1 * $d1)/2))))*$volatility_decimal)/(2*sqrt($time_to_expire_years))) 
- $risk_fee_rate_decimal*$exercise_price*exp(-$risk_fee_rate_decimal*$time_to_expire_years)*$Nd2;

print "\n\n";
printf("Calculated Theta is: %.5f", $theta);
print "\n\n";

$vega=$stock_price*sqrt($time_to_expire_years)*((1/(sqrt(2*$pi)))*exp(-1*((($d1 * $d1)/2))));

printf("Calculated Vega is: %.5f", $vega);
print "\n\n";


$Rho=$exercise_price*$time_to_expire_years*(exp(-1*$risk_fee_rate_decimal*$time_to_expire_years))*$Nd2;

printf("Calculated Rho is: %.5f", $Rho);
print "\n\n";


close DATA_IN;
print "\nEnd of run black_scholes_calc.pl\n\n";

Contact Information

If there are any changes suggested for this page please contact me.

Alastair Cook email:
cookalastair @ btinternet.com

Last update to this page - 3rd March 2016