| 1 | package Finance::Options::Calc; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use constant PI => 4 * atan2(1,1); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use vars qw(@EXPORT @ISA $VERSION $s $k $r $vol $t $d1 $d2 $nd1); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 8 | 1 | $VERSION = 0.90; | |||||||||||||||||||||||||||
| 9 | 1 | @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | C<Finance::Options::Calc> - Option analysis based on different option pricing models. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | use Finance::Options::Calc; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | print b_s_call(90, 80, 20, 30, 4.5); | ||||||||||||||||||||||||||||
| 20 | print b_s_put (90, 80, 20, 30, 4.5); | ||||||||||||||||||||||||||||
| 21 | print call_delta(90, 80, 20, 30, 4.5); | ||||||||||||||||||||||||||||
| 22 | print put_delta(90, 80, 20, 30, 4.5); | ||||||||||||||||||||||||||||
| 23 | print call_theta(90, 80, 20, 30, 4.5); | ||||||||||||||||||||||||||||
| 24 | print put_theta(90, 80, 20, 30, 4.5); | ||||||||||||||||||||||||||||
| 25 | print gamma(90, 80, 20, 30, 4.5); | ||||||||||||||||||||||||||||
| 26 | print vega(90, 80, 20, 30, 4.5); | ||||||||||||||||||||||||||||
| 27 | print call_rho(90, 80, 20, 30, 4.5); | ||||||||||||||||||||||||||||
| 28 | print put_rho(90, 80, 20, 30, 4.5); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | b_s_call() subroutines returns theorical value of the call option based on | ||||||||||||||||||||||||||||
| 34 | Black_Scholes model. The arguments are current stock price, | ||||||||||||||||||||||||||||
| 35 | strike price, time to expiration (calender days, note this module | ||||||||||||||||||||||||||||
| 36 | does NOT use business days), volatility(%), annual interest rate(%) in order. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | b_s_put() subroutines returns theorical value of the put option based on | ||||||||||||||||||||||||||||
| 39 | Black_Scholes model. The arguments are current stock price, | ||||||||||||||||||||||||||||
| 40 | strike price, time to expiration (calender days, note this module | ||||||||||||||||||||||||||||
| 41 | does NOT use business days), volatility(%), annual interest rate(%) in order. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | call_delta() returns call delta. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | put_delta() returns put delta. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Other methods are similar. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 TODO | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | more calculation models will be included. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Chicheng Zhang | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | chichengzhang@hotmail.com | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | @EXPORT = qw(b_s_call b_s_put call_delta put_delta vega | |||||||||||||||||||||||||||
| 62 | call_rho put_rho call_theta put_theta gamma); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 10 | ▶ | sub _variables { | ||||||||||||||||||||||||||
|
Subroutine
_variables: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 10 | ▶ | croak "Not enough arguments.\n" unless $#_ == 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | ## s -- current price | ||||||||||||||||||||||||||||
| 69 | ## k -- strike price | ||||||||||||||||||||||||||||
| 70 | ## t -- time remains | ||||||||||||||||||||||||||||
| 71 | ## vol -- volatility | ||||||||||||||||||||||||||||
| 72 | ## r -- interest rate | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 10 | ($s, $k, $t, $vol, $r) = @_; | |||||||||||||||||||||||||||
| 75 | 10 | $r /= 100; | |||||||||||||||||||||||||||
| 76 | 10 | $vol /= 100; | |||||||||||||||||||||||||||
| 77 | 10 | $t /= 365; | |||||||||||||||||||||||||||
| 78 | 10 | $d1 = (log($s / $k) + ( $r + $vol * $vol / 2 ) * $t) / ($vol * (sqrt $t)); | |||||||||||||||||||||||||||
| 79 | 10 | $d2 = $d1 - $vol * (sqrt $t); | |||||||||||||||||||||||||||
| 80 | 10 | $nd1 = exp( - $d1 * $d1 / 2 ) / sqrt( 2 * PI ); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | ▶ | sub call_delta { | ||||||||||||||||||||||||||
|
Subroutine
call_delta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 1 | _variables(@_); | |||||||||||||||||||||||||||
| 85 | 1 | return sprintf "%5.5f", _norm($d1); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | ▶ | sub put_delta { | ||||||||||||||||||||||||||
|
Subroutine
put_delta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 1 | _variables(@_); | |||||||||||||||||||||||||||
| 90 | 1 | return sprintf "%5.5f", _norm($d1) - 1; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub call_theta { | ||||||||||||||||||||||||||
|
Subroutine
call_theta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 1 | _variables(@_); | |||||||||||||||||||||||||||
| 95 | 1 | my $theta_c = - $s * $nd1 * $vol / (2 * sqrt($t)) - $r * $k * exp( - $r * $t ) * _norm($d2); | |||||||||||||||||||||||||||
| 96 | 1 | return sprintf "%5.5f", $theta_c / 365; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | ▶ | sub put_theta { | ||||||||||||||||||||||||||
|
Subroutine
put_theta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | 1 | _variables(@_); | |||||||||||||||||||||||||||
| 101 | 1 | my $theta_p = - $s * $nd1 * $vol / (2 * sqrt($t)) + $r * $k * exp( - $r * $t ) * _norm(-$d2); | |||||||||||||||||||||||||||
| 102 | 1 | return sprintf "%5.5f", $theta_p / 365; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | ▶ | sub call_rho { | ||||||||||||||||||||||||||
|
Subroutine
call_rho: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 1 | _variables(@_); | |||||||||||||||||||||||||||
| 107 | 1 | my $rho = $k * $t * exp( - $r * $t ) * _norm($d2); | |||||||||||||||||||||||||||
| 108 | 1 | return sprintf "%5.5f", $rho / 100; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | sub put_rho { | ||||||||||||||||||||||||||
|
Subroutine
put_rho: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | 1 | _variables(@_); | |||||||||||||||||||||||||||
| 113 | 1 | my $rho = - $k * $t * exp( - $r * $t ) * _norm(-$d2); | |||||||||||||||||||||||||||
| 114 | 1 | return sprintf "%5.5f", $rho / 100; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | ▶ | sub vega { | ||||||||||||||||||||||||||
|
Subroutine
vega: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 1 | _variables(@_); | |||||||||||||||||||||||||||
| 119 | 1 | my $vega = $s * sqrt($t) * $nd1; | |||||||||||||||||||||||||||
| 120 | 1 | return sprintf "%5.5f", $vega / 100; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | sub gamma { | ||||||||||||||||||||||||||
|
Subroutine
gamma: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 1 | _variables(@_); | |||||||||||||||||||||||||||
| 125 | 1 | my $gamma= $nd1 / ( $s * $vol * sqrt($t) ); | |||||||||||||||||||||||||||
| 126 | 1 | return sprintf "%5.5f", $gamma; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | ▶ | sub b_s_call { | ||||||||||||||||||||||||||
|
Subroutine
b_s_call: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 1 | _variables(@_); | |||||||||||||||||||||||||||
| 131 | 1 | my $c = $s * _norm($d1) - $k * (exp (-$r*$t)) * _norm($d2); | |||||||||||||||||||||||||||
| 132 | 1 | return sprintf "%5.5f", $c; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | ▶ | sub b_s_put { | ||||||||||||||||||||||||||
|
Subroutine
b_s_put: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | 1 | _variables(@_); | |||||||||||||||||||||||||||
| 137 | 1 | my $p = $k * (exp (-$r*$t)) * _norm(-$d2) - $s * _norm(-$d1); | |||||||||||||||||||||||||||
| 138 | 1 | return sprintf "%5.5f", $p; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 10 | ▶ | sub _norm { | ||||||||||||||||||||||||||
|
Subroutine
_norm: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 10 | my $d = shift; | |||||||||||||||||||||||||||
| 144 | 10 | my $step = 0.01; | |||||||||||||||||||||||||||
| 145 | 10 | my $sum = 0; | |||||||||||||||||||||||||||
| 146 | 10 | my $x = -5 + $step / 2; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 10 | ▶ | while ( ($x < $d) && ($x < 4) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$x < $d and $x < 4
MC/DC50%1/2
$x < $d and $x < 4
$x < $d
$x < 4
Truth table66%2/3
A and B
A= $x < $dB= $x < 4
| |||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||
| 150 | 5350 | $sum += exp(- $x * $x / 2) * $step; | |||||||||||||||||||||||||||
| 151 | 5350 | $x += $step; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 10 | return $sum / sqrt(2 * PI); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | 1; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||