Lookback.pm

filter: Statementstmt 100% 63 / 63 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 100% 88 / 88 CC 3 SCAR 2.4
CC
3
Cov
100%
l_max
2.0
l_min
2.0
BEGIN
1.0
CRAP 1.3
↑ Summary Lookback/Greeks/Delta.pm »
1 package Math::Business::Lookback;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Math::CDF qw(pnorm);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Math::Business::Lookback::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: The Black-Scholes formula for Lookback options.
12  
13 3 our $VERSION = '0.01';
14  
15 =head1 NAME
16  
17 Math::Business::Lookback
18  
19 =head1 SYNOPSIS
20  
21 use Math::Business::Lookback;
22  
23 # price of a Lookback Fixed Call option
24  
25 my $price_lbfixedcall_option = Math::Business::Lookback::lbfixedcall(
26 1.35, # stock price
27 1.36, # barrier
28 (7/365), # time
29 0.002, # payout currency interest rate (0.05 = 5%)
30 0.001, # quanto drift adjustment (0.05 = 5%)
31 0.11, # volatility (0.3 = 30%)
32 1.39, # maximum spot
33 undef # minimum spot
34 );
35  
36 =head1 DESCRIPTION
37  
38 Prices lookback options using the GBM model, all closed formulas.
39  
40 =head1 SUBROUTINES
41  
42 =head2 lbfloatcall
43  
44 USAGE
45 my $price = lbfloatcall($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min)
46  
47 DESCRIPTION
48 Price of a Lookback Float Call
49  
50 =cut
51  
52 3 ▶sub lbfloatcall {
Subroutine lbfloatcall: called CC=1 SCAR=0.0
Pod documented
53 3 my ($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min) = @_;
54  
55 3 $S_max = undef;
56 3 my $d1 = d1_function($S, $S_min, $t, $r_q, $mu, $sigma);
57 3 my $d2 = $d1 - ($sigma * sqrt($t));
58  
59 3 my $value = exp(-$r_q * $t) * ($S * exp($mu * $t) * pnorm($d1) - $S_min * pnorm($d2) + l_min($S, $S_min, $t, $r_q, $mu, $sigma));
60  
61 3 return $value;
62 }
63  
64 =head2 lbfloatput
65  
66 USAGE
67 my $price = lbfloatcall($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min)
68  
69 DESCRIPTION
70 Price of a Lookback Float Put
71  
72 =cut
73  
74 3 ▶sub lbfloatput { # Floating Strike Put
Subroutine lbfloatput: called CC=1 SCAR=0.0
Pod documented
75 3 my ($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min) = @_;
76  
77 3 $S_min = undef;
78 3 my $d1 = d1_function($S, $S_max, $t, $r_q, $mu, $sigma);
79 3 my $d2 = $d1 - ($sigma * sqrt($t));
80  
81 3 my $value = exp(-$r_q * $t) * ($S_max * pnorm(-$d2) - $S * exp($mu * $t) * pnorm(-$d1) + l_max($S, $S_max, $t, $r_q, $mu, $sigma));
82  
83 3 return $value;
84 }
85  
86 =head2 lbfixedcall
87  
88 USAGE
89 my $price = lbfixedcall($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min)
90  
91 DESCRIPTION
92 Price of a Lookback Fixed Call
93  
94 =cut
95  
96 2 ▶sub lbfixedcall {
Subroutine lbfixedcall: called CC=1 SCAR=0.0
Pod documented
97 2 my ($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min) = @_;
98  
99 2 $S_min = undef;
100 2 my $K_max = max($S_max, $K);
101 2 my $d1 = d1_function($S, $K_max, $t, $r_q, $mu, $sigma);
102 2 my $d2 = $d1 - ($sigma * sqrt($t));
103  
104 2 my $value =
105 exp(-$r_q * $t) * (max($S_max - $K, 0.0) + $S * exp($mu * $t) * pnorm($d1) - $K_max * pnorm($d2) + l_max($S, $K_max, $t, $r_q, $mu, $sigma));
106  
107 2 return $value;
108 }
109  
110 =head2 lbfixedput
111  
112 USAGE
113 my $price = lbfixedput($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min)
114  
115 DESCRIPTION
116 Price of a Lookback Fixed Put
117  
118 =cut
119  
120 2 ▶sub lbfixedput {
Subroutine lbfixedput: called CC=1 SCAR=0.0
Pod documented
121 2 my ($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min) = @_;
122  
123 2 $S_max = undef;
124 2 my $K_min = min($S_min, $K);
125 2 my $d1 = d1_function($S, $K_min, $t, $r_q, $mu, $sigma);
126 2 my $d2 = $d1 - ($sigma * sqrt($t));
127  
128 2 my $value = exp(-$r_q * $t) *
129 (max($K - $S_min, 0.0) + $K_min * pnorm(-$d2) - $S * exp($mu * $t) * pnorm(-$d1) + l_min($S, $K_min, $t, $r_q, $mu, $sigma));
130  
131 2 return $value;
132 }
133  
134 =head2 lbhighlow
135  
136 USAGE
137 my $price = lbhighlow($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min)
138  
139 DESCRIPTION
140 Price of a Lookback High Low
141  
142 =cut
143  
144 1 ▶sub lbhighlow {
Subroutine lbhighlow: called CC=1 SCAR=0.0
Pod documented
145 1 my ($S, $K, $t, $r_q, $mu, $sigma, $S_max, $S_min) = @_;
146  
147 1 my $value = lbfloatcall($S, $S_min, $t, $r_q, $mu, $sigma, $S_max, $S_min) + lbfloatput($S, $S_max, $t, $r_q, $mu, $sigma, $S_max, $S_min);
148  
149 1 return $value;
150 }
151  
152 =head2 d1_function
153  
154 returns the d1 term common to many BlackScholes formulae.
155  
156 =cut
157  
158 20 ▶sub d1_function {
Subroutine d1_function: called CC=1 SCAR=0.0
Pod documented
159 20 my ($S, $K, $t, $r_q, $mu, $sigma) = @_;
160  
161 20 my $value = (log($S / $K) + ($mu + $sigma * $sigma * 0.5) * $t) / ($sigma * sqrt($t));
162  
163 20 return $value;
164 }
165  
166 =head2 l_max
167  
168 This is a common function use to calculate the lookbacks options price. See [1] for details.
169  
170 =cut
171  
172 5 ▶sub l_max {
Subroutine l_max: called CC=2 SCAR=6.9
Pod documented
173 5 my ($S, $K, $t, $r_q, $mu, $sigma) = @_;
174  
175 5 my $d1 = d1_function($S, $K, $t, $r_q, $mu, $sigma);
176 5 my $value;
177  
178 5 ▶ if ($mu) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mu) { } 3 2 5
179 3 $value =
180 $S *
181 ($sigma**2) /
182 (2.0 * $mu) *
183 (-($S / $K)**(-2.0 * $mu / ($sigma**2)) * pnorm($d1 - 2.0 * $mu / $sigma * sqrt($t)) + exp($mu * $t) * pnorm($d1));
184 } else {
185 2 $value = $S * ($sigma * sqrt($t)) * (Math::Business::Lookback::Common::dnorm($d1) + $d1 * pnorm($d1));
186 }
187  
188 5 return $value;
189 }
190  
191 =head2 l_min
192  
193 This is a common function use to calculate the lookbacks options price. See [1] for details.
194  
195 =cut
196  
197 5 ▶sub l_min {
Subroutine l_min: called CC=2 SCAR=6.9
Pod documented
198 5 my ($S, $K, $t, $r_q, $mu, $sigma) = @_;
199  
200 5 my $d1 = d1_function($S, $K, $t, $r_q, $mu, $sigma);
201 5 my $value;
202  
203 5 ▶ if ($mu) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mu) { } 3 2 5
204 3 $value =
205 $S *
206 ($sigma**2) /
207 (2.0 * $mu) *
208 (($S / $K)**(-2.0 * $mu / ($sigma**2)) * pnorm(-$d1 + 2.0 * $mu / $sigma * sqrt($t)) - exp($mu * $t) * pnorm(-$d1));
209 } else {
210 2 $value = $S * ($sigma * sqrt($t)) * (Math::Business::Lookback::Common::dnorm($d1) + $d1 * (pnorm($d1) - 1));
211 }
212  
213 5 return $value;
214 }
215  
216 =head1 DEPENDENCIES
217  
218 * Math::CDF
219  
220 =head1 SOURCE CODE
221  
222 https://github.com/binary-com/perl-Math-Business-Lookback
223  
224 =head1 REFERENCES
225  
226 [1] Espen Gaarder Haug, PhD
227 The Complete Guide to Option Pricing Formulas p141-p144
228  
229 =head1 AUTHOR
230  
231 binary.com, C<< <shahrizal at binary.com> >>
232  
233 =head1 BUGS
234  
235 Please report any bugs or feature requests to
236  
237 C<bug-math-business-lookbacks at rt.cpan.org>, or through the web
238  
239 interface at
240  
241 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Business-Lookback>.
242  
243 I will be notified, and then you'll automatically be notified of progress on
244  
245 your bug as I make changes.
246  
247 =head1 SUPPORT
248  
249 You can find documentation for this module with the perldoc command.
250  
251 perldoc Math::Business::Lookback
252  
253 You can also look for information at:
254  
255 =over 4
256  
257 =item * RT: CPAN's request tracker (report bugs here)
258  
259 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-Business-Lookback>
260  
261 =item * AnnoCPAN: Annotated CPAN documentation
262  
263 L<http://annocpan.org/dist/Math-Business-Lookback>
264  
265 =item * CPAN Ratings
266  
267 L<http://cpanratings.perl.org/d/Math-Business-Lookback>
268  
269 =item * Search CPAN
270  
271 L<http://search.cpan.org/dist/Math-Business-Lookback/>
272  
273 =back
274  
275 =cut
276  
277 3 1;
↑ Summary Lookback/Greeks/Delta.pm »