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