| 1 | package Quant::Framework::VolSurface::Delta; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Quant::Framework::VolSurface::Delta | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | Represents a volatility surface, built from market implied volatilities. | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | my $surface = Quant::Framework::VolSurface::Delta->new({underlying_config => $underlying_config}); | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | use Moose; | ||
| 18 | |||
| 19 | extends 'Quant::Framework::VolSurface'; | ||
| 20 | |||
| 21 | use Date::Utility; | ||
| 22 | use VolSurface::Utils qw( get_delta_for_strike get_strike_for_moneyness ); | ||
| 23 | use List::MoreUtils qw(none); | ||
| 24 | use Math::Function::Interpolator; | ||
| 25 | use Storable qw( dclone ); | ||
| 26 | use Try::Tiny; | ||
| 27 | |||
| 28 | =head2 for_date | ||
| 29 | |||
| 30 | The date for which we wish data | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | has for_date => ( | ||
| 35 | is => 'ro', | ||
| 36 | isa => 'Maybe[Date::Utility]', | ||
| 37 | default => undef, | ||
| 38 | ); | ||
| 39 | |||
| 40 | sub _document_content { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | my %structure = ( | ||
| 44 | surfaces => $self->surfaces_to_save, | ||
| 45 | date => $self->recorded_date->datetime_iso8601, | ||
| 46 | master_cutoff => $self->cutoff->code, | ||
| 47 | symbol => $self->symbol, | ||
| 48 | type => $self->type, | ||
| 49 | ); | ||
| 50 | |||
| 51 | return \%structure; | ||
| 52 | } | ||
| 53 | |||
| 54 | has document => ( | ||
| 55 | is => 'rw', | ||
| 56 | lazy_build => 1, | ||
| 57 | ); | ||
| 58 | |||
| 59 | sub _build_document { | ||
| 60 | my $self = shift; | ||
| 61 | |||
| 62 | my $document = $self->chronicle_reader->get('volatility_surfaces', $self->symbol); | ||
| 63 | |||
| 64 | if ($self->for_date and $self->for_date->epoch < Date::Utility->new($document->{date})->epoch) { | ||
| 65 | $document = $self->chronicle_reader->get_for('volatility_surfaces', $self->symbol, $self->for_date->epoch); | ||
| 66 | |||
| 67 | # This works around a problem with Volatility surfaces and negative dates to expiry. | ||
| 68 | # We have to use the oldest available surface.. and we don't really know when it | ||
| 69 | # was relative to where we are now.. so just say it's from the requested day. | ||
| 70 | # We do not allow saving of historical surfaces, so this should be fine. | ||
| 71 | $document //= {}; | ||
| 72 | } | ||
| 73 | |||
| 74 | return $document; | ||
| 75 | } | ||
| 76 | |||
| 77 | =head2 save | ||
| 78 | |||
| 79 | Saves current surface using given chronicle writer. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub save { | ||
| 84 | my $self = shift; | ||
| 85 | |||
| 86 | #if chronicle does not have this document, first create it because in document_content we will need it | ||
| 87 | if (not defined $self->chronicle_reader->get('volatility_surfaces', $self->symbol)) { | ||
| 88 | #Due to some strange coding of retrieval for recorded_date, there MUST be an existing document (even empty) | ||
| 89 | #before one can save a document. As a result, upon the very first storage of an instance of the document, we need to create an empty one. | ||
| 90 | $self->chronicle_writer->set('volatility_surfaces', $self->symbol, {}); | ||
| 91 | } | ||
| 92 | |||
| 93 | return $self->chronicle_writer->set('volatility_surfaces', $self->symbol, $self->_document_content, $self->recorded_date); | ||
| 94 | } | ||
| 95 | |||
| 96 | =head1 ATTRIBUTES | ||
| 97 | |||
| 98 | =head2 type | ||
| 99 | |||
| 100 | Return the surface type | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | has '+type' => ( | ||
| 105 | default => 'delta', | ||
| 106 | ); | ||
| 107 | |||
| 108 | =head2 deltas | ||
| 109 | |||
| 110 | Get the available deltas for which we have vols. | ||
| 111 | |||
| 112 | Returns an ArrayRef, is required and read-only. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | has deltas => ( | ||
| 117 | is => 'ro', | ||
| 118 | isa => 'ArrayRef', | ||
| 119 | lazy_build => 1, | ||
| 120 | ); | ||
| 121 | |||
| 122 | sub _build_deltas { | ||
| 123 | my $self = shift; | ||
| 124 | return $self->smile_points; | ||
| 125 | } | ||
| 126 | |||
| 127 | has atm_spread_point => ( | ||
| 128 | is => 'ro', | ||
| 129 | isa => 'Num', | ||
| 130 | default => '50', | ||
| 131 | ); | ||
| 132 | |||
| 133 | =head2 get_volatility | ||
| 134 | |||
| 135 | Given a maturity of some form and a barrier of some form, gives you a vol | ||
| 136 | from the surface. | ||
| 137 | |||
| 138 | The barrier can be specified either as a strike (strike => $bet->barrier) or a | ||
| 139 | delta (delta => 25). | ||
| 140 | |||
| 141 | The maturity can be given either as a number of days (days => 7), an expiry date | ||
| 142 | (expiry_date => Date::Utility->new) or a tenor (tenor => 'ON'). | ||
| 143 | |||
| 144 | When given an expiry_date, get_volatility assumes that you want an integer number | ||
| 145 | of days, and calculates that based on the number of vol rollovers between the | ||
| 146 | recorded_date and the given expiry. So if the rollover is at GMT2200 (NY1700) and | ||
| 147 | recorded_date is 2012-02-01 10:00:00, a given expiry of 2012-02-02 10:00:00 would | ||
| 148 | give you the ON vol, but an expiry of 2012-02-02 23:59:59 would give a 2-day vol. | ||
| 149 | |||
| 150 | USAGE: | ||
| 151 | |||
| 152 | my $vol = $s->get_volatility({delta => 25, days => 7}); | ||
| 153 | my $vol = $s->get_volatility({strike => $bet->barrier, tenor => '1M'}); | ||
| 154 | my $vol = $s->get_volatility({delta => 50, expiry_date => Date::Utility->new}); | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub get_volatility { | ||
| 159 | my ($self, $args) = @_; | ||
| 160 | |||
| 161 | # args validity checks | ||
| 162 | die("Must pass exactly one of delta, strike or moneyness to get_volatility.") | ||
| 163 | if (scalar(grep { defined $args->{$_} } qw(delta strike moneyness)) != 1); | ||
| 164 | die("Must pass exactly one of days, tenor or expirty_date to get_volatility.") | ||
| 165 | if (scalar(grep { defined $args->{$_} } qw(days tenor expiry_date)) != 1); | ||
| 166 | |||
| 167 | if (not $args->{days}) { | ||
| 168 | $args->{days} = $self->_convert_expiry_to_day($args); | ||
| 169 | } | ||
| 170 | |||
| 171 | my $sought_point = | ||
| 172 | (defined $args->{delta}) ? $args->{delta} | ||
| 173 | : (defined $args->{strike}) ? $self->_convert_strike_to_delta($args) | ||
| 174 | : $self->_convert_moneyness_to_delta($args); | ||
| 175 | |||
| 176 | return $self->SUPER::get_volatility({ | ||
| 177 | sought_point => $sought_point, | ||
| 178 | days => $args->{days}, | ||
| 179 | }); | ||
| 180 | } | ||
| 181 | |||
| 182 | =head2 interpolate | ||
| 183 | |||
| 184 | Quadratic interpolation to interpolate across smile | ||
| 185 | ->interpolate({smile => $smile, sought_point => $sought_point}); | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub interpolate { | ||
| 190 | my ($self, $args) = @_; | ||
| 191 | |||
| 192 | return Math::Function::Interpolator->new(points => $args->{smile})->quadratic($args->{sought_point}); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub _convert_moneyness_to_delta { | ||
| 196 | my ($self, $args) = @_; | ||
| 197 | |||
| 198 | $args->{strike} = get_strike_for_moneyness({ | ||
| 199 | moneyness => $args->{moneyness}, | ||
| 200 | spot => $self->underlying_config->spot | ||
| 201 | }); | ||
| 202 | |||
| 203 | delete $args->{moneyness}; | ||
| 204 | my $delta = $self->_convert_strike_to_delta($args); | ||
| 205 | |||
| 206 | return $delta; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub _convert_strike_to_delta { | ||
| 210 | my ($self, $args) = @_; | ||
| 211 | |||
| 212 | my $conversion_args = $self->_ensure_conversion_args($args); | ||
| 213 | |||
| 214 | return 100 * get_delta_for_strike($conversion_args); | ||
| 215 | } | ||
| 216 | |||
| 217 | sub _ensure_conversion_args { | ||
| 218 | my ($self, $args) = @_; | ||
| 219 | |||
| 220 | my %new_args = %{$args}; | ||
| 221 | my $underlying_config = $self->underlying_config; | ||
| 222 | |||
| 223 | $new_args{t} ||= $new_args{days} / 365; | ||
| 224 | $new_args{spot} ||= $underlying_config->spot; | ||
| 225 | $new_args{premium_adjusted} ||= $underlying_config->{market_convention}->{delta_premium_adjusted}; | ||
| 226 | $new_args{r_rate} ||= $self->builder->build_interest_rate->interest_rate_for($new_args{t}); | ||
| 227 | $new_args{q_rate} ||= $self->builder->build_dividend->dividend_rate_for($new_args{t}); | ||
| 228 | |||
| 229 | $new_args{atm_vol} ||= $self->get_volatility({ | ||
| 230 | days => $new_args{days}, | ||
| 231 | delta => 50, | ||
| 232 | }); | ||
| 233 | |||
| 234 | return \%new_args; | ||
| 235 | } | ||
| 236 | |||
| 237 | =head2 generate_surface_for_cutoff | ||
| 238 | |||
| 239 | Transforms the surface to a given cutoff. Cutoff can be given either | ||
| 240 | as a qf_cutoff_code string, or a Quant::Framework::VolSurface::Cutoff instance. | ||
| 241 | |||
| 242 | Returns the cut surface data-structure (not a B::M::VS instance). | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub generate_surface_for_cutoff { | ||
| 247 | my ($self, $cutoff) = @_; | ||
| 248 | |||
| 249 | # Everything with a trailing 1 is from what we are transforming from. | ||
| 250 | # Everything else is what we're transforming to. | ||
| 251 | |||
| 252 | my $surface1 = $self; | ||
| 253 | $cutoff = Quant::Framework::VolSurface::Cutoff->new($cutoff) if (not ref $cutoff); | ||
| 254 | my $surface_hashref = {}; | ||
| 255 | my $underlying_config = $surface1->underlying_config; | ||
| 256 | |||
| 257 | foreach my $maturity (@{$surface1->term_by_day}) { | ||
| 258 | my $t1 = $surface1->cutoff->seconds_to_cutoff_time({ | ||
| 259 | from => $surface1->recorded_date, | ||
| 260 | maturity => $maturity, | ||
| 261 | calendar => $self->builder->build_trading_calendar, | ||
| 262 | }); | ||
| 263 | my $t = $cutoff->seconds_to_cutoff_time({ | ||
| 264 | from => $surface1->recorded_date, | ||
| 265 | maturity => $maturity, | ||
| 266 | calendar => $self->builder->build_trading_calendar, | ||
| 267 | }); | ||
| 268 | |||
| 269 | foreach my $delta (@{$surface1->deltas}) { | ||
| 270 | my $v1 = $surface1->get_volatility({ | ||
| 271 | days => $maturity, | ||
| 272 | delta => $delta, | ||
| 273 | }); | ||
| 274 | |||
| 275 | my $v = $v1 * sqrt($t / $t1); | ||
| 276 | $surface_hashref->{$maturity}->{smile}->{$delta} = $v; | ||
| 277 | |||
| 278 | if (defined $surface1->surface->{$maturity}->{vol_spread}->{$delta}) { | ||
| 279 | $surface_hashref->{$maturity}->{vol_spread}->{$delta} = $surface1->surface->{$maturity}->{vol_spread}->{$delta}; | ||
| 280 | } | ||
| 281 | |||
| 282 | } | ||
| 283 | |||
| 284 | if (my $tenor = $surface1->surface->{$maturity}->{tenor}) { | ||
| 285 | $surface_hashref->{$maturity}->{tenor} = $tenor; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | return $surface_hashref; | ||
| 290 | } | ||
| 291 | |||
| 292 | has _default_cutoff_list => ( | ||
| 293 | is => 'ro', | ||
| 294 | isa => 'ArrayRef', | ||
| 295 | default => sub { ['UTC 21:00', 'UTC 23:59'] }, | ||
| 296 | ); | ||
| 297 | |||
| 298 | =head2 surfaces_to_save | ||
| 299 | |||
| 300 | The surfaces that will be saved on Chronicle | ||
| 301 | |||
| 302 | =cut | ||
| 303 | |||
| 304 | has surfaces_to_save => ( | ||
| 305 | is => 'ro', | ||
| 306 | isa => 'HashRef', | ||
| 307 | lazy_build => 1, | ||
| 308 | ); | ||
| 309 | |||
| 310 | sub _build_surfaces_to_save { | ||
| 311 | my $self = shift; | ||
| 312 | |||
| 313 | my $master_surface = $self->surface; | ||
| 314 | my %surfaces; | ||
| 315 | $surfaces{$self->cutoff->code} = $master_surface; | ||
| 316 | |||
| 317 | $surfaces{$_} = $self->generate_surface_for_cutoff($_) foreach @{$self->_default_cutoff_list}; | ||
| 318 | |||
| 319 | return \%surfaces; | ||
| 320 | } | ||
| 321 | |||
| 322 | sub _build_surface { | ||
| 323 | my $self = shift; | ||
| 324 | |||
| 325 | my $doc = $self->document; | ||
| 326 | my $cutoff = $self->cutoff->code; | ||
| 327 | |||
| 328 | return $doc->{surfaces}->{$cutoff} if $doc->{surfaces}->{$cutoff}; | ||
| 329 | |||
| 330 | my $master_cutoff = $doc->{master_cutoff}; | ||
| 331 | if (not $doc->{surfaces}->{$master_cutoff}) { | ||
| 332 | die('master surface is missing for ' . $self->symbol . ' on ' . $self->recorded_date->datetime_iso8601); | ||
| 333 | } | ||
| 334 | |||
| 335 | my $master_surface = __PACKAGE__->new( | ||
| 336 | symbol => $self->symbol, | ||
| 337 | cutoff => $master_cutoff, | ||
| 338 | for_date => $self->for_date, | ||
| 339 | chronicle_reader => $self->chronicle_reader, | ||
| 340 | chronicle_writer => $self->chronicle_writer, | ||
| 341 | underlying_config => $self->underlying_config, | ||
| 342 | ); | ||
| 343 | my $surface = $master_surface->generate_surface_for_cutoff($cutoff); | ||
| 344 | $self->_stores_surface($cutoff, $surface); | ||
| 345 | |||
| 346 | return $surface; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub _stores_surface { | ||
| 350 | my ($self, $cutoff, $surface_hashref) = @_; | ||
| 351 | |||
| 352 | try { | ||
| 353 | my $doc = $self->document; | ||
| 354 | $doc->{surfaces} ||= {}; | ||
| 355 | $doc->{surfaces}->{$cutoff} = $surface_hashref; | ||
| 356 | } | ||
| 357 | catch { | ||
| 358 | warn('Could not save ' . $cutoff . ' cutoff for ' . $self->symbol); | ||
| 359 | }; | ||
| 360 | |||
| 361 | return; | ||
| 362 | } | ||
| 363 | |||
| 364 | sub _extrapolate_smile_down { | ||
| 365 | my ($self, $days) = @_; | ||
| 366 | |||
| 367 | my $first_market_point = $self->original_term_for_smile->[0]; | ||
| 368 | return $self->surface->{$first_market_point}->{smile} if $self->_market_name eq 'indices'; | ||
| 369 | my $market = $self->get_market_rr_bf($first_market_point); | ||
| 370 | my %initial_rr = %{$self->_get_initial_rr($market)}; | ||
| 371 | |||
| 372 | # we won't be using the indices case unless we revert back to delta surfaces | ||
| 373 | my %rr_bf = ( | ||
| 374 | ATM => $market->{ATM}, | ||
| 375 | BF_25 => $market->{BF_25}, | ||
| 376 | ); | ||
| 377 | $rr_bf{BF_10} = $market->{BF_10} if (exists $market->{BF_10}); | ||
| 378 | |||
| 379 | # Only RR is interpolated at this point. | ||
| 380 | # Data structure is here in case that changes, plus it's easier to understand. | ||
| 381 | foreach my $which (keys %initial_rr) { | ||
| 382 | my $interp = Math::Function::Interpolator->new( | ||
| 383 | points => { | ||
| 384 | $first_market_point => $market->{$which}, | ||
| 385 | 0 => $initial_rr{$which}, | ||
| 386 | }); | ||
| 387 | $rr_bf{$which} = $interp->linear($days); | ||
| 388 | } | ||
| 389 | |||
| 390 | my $extrapolated_smile->{smile} = { | ||
| 391 | 25 => $rr_bf{RR_25} / 2 + $rr_bf{BF_25} + $rr_bf{ATM}, | ||
| 392 | 50 => $rr_bf{ATM}, | ||
| 393 | 75 => $rr_bf{BF_25} - $rr_bf{RR_25} / 2 + $rr_bf{ATM}, | ||
| 394 | }; | ||
| 395 | |||
| 396 | if (exists $market->{RR_10}) { | ||
| 397 | $extrapolated_smile->{smile}->{10} = $rr_bf{RR_10} / 2 + $rr_bf{BF_10} + $rr_bf{ATM}; | ||
| 398 | $extrapolated_smile->{smile}->{90} = $rr_bf{RR_10} / 2 + $rr_bf{BF_10} + $rr_bf{ATM}; | ||
| 399 | } | ||
| 400 | |||
| 401 | return $extrapolated_smile->{smile}; | ||
| 402 | } | ||
| 403 | |||
| 404 | =head2 clone | ||
| 405 | |||
| 406 | USAGE: | ||
| 407 | |||
| 408 | my $clone = $s->clone({ | ||
| 409 | surface => $my_new_surface, | ||
| 410 | cutoff => $my_new_cutoff, | ||
| 411 | }); | ||
| 412 | |||
| 413 | Returns a new cloned instance. | ||
| 414 | You can pass overrides to override an attribute value as it is on the original surface. | ||
| 415 | |||
| 416 | =cut | ||
| 417 | |||
| 418 | sub clone { | ||
| 419 | my ($self, $args) = @_; | ||
| 420 | |||
| 421 | my $clone_args; | ||
| 422 | $clone_args = dclone($args) if $args; | ||
| 423 | |||
| 424 | $clone_args->{underlying_config} = $self->underlying_config if (not exists $clone_args->{underlying_config}); | ||
| 425 | $clone_args->{cutoff} = $self->cutoff if (not exists $clone_args->{cutoff}); | ||
| 426 | |||
| 427 | if (not exists $clone_args->{surface}) { | ||
| 428 | my $orig_surface = dclone($self->surface); | ||
| 429 | my %surface_to_clone = map { $_ => $orig_surface->{$_} } @{$self->original_term_for_smile}; | ||
| 430 | $clone_args->{surface} = \%surface_to_clone; | ||
| 431 | } | ||
| 432 | |||
| 433 | $clone_args->{recorded_date} = $self->recorded_date if (not exists $clone_args->{recorded_date}); | ||
| 434 | $clone_args->{print_precision} = $self->print_precision if (not exists $clone_args->{print_precision}); | ||
| 435 | $clone_args->{original_term} = dclone($self->original_term) if (not exists $clone_args->{original_term}); | ||
| 436 | $clone_args->{chronicle_reader} = $self->chronicle_reader; | ||
| 437 | $clone_args->{chronicle_writer} = $self->chronicle_writer; | ||
| 438 | |||
| 439 | return $self->meta->name->new($clone_args); | ||
| 440 | } | ||
| 441 | |||
| 442 | =head2 cutoff | ||
| 443 | |||
| 444 | cutoff is New York 10:00 when we save new volatility delta surfaces. (Surfaces that we get from Bloomberg) | ||
| 445 | |||
| 446 | cutoff is UTC 23:59 or UTC 21:00 for contract pricing. | ||
| 447 | |||
| 448 | =cut | ||
| 449 | |||
| 450 | has cutoff => ( | ||
| 451 | is => 'ro', | ||
| 452 | isa => 'qf_cutoff_helper', | ||
| 453 | lazy_build => 1, | ||
| 454 | coerce => 1, | ||
| 455 | ); | ||
| 456 | |||
| 457 | sub _build_cutoff { | ||
| 458 | my $self = shift; | ||
| 459 | |||
| 460 | my $date = $self->for_date ? $self->for_date : Date::Utility->new; | ||
| 461 | my $cutoff_string = | ||
| 462 | $self->_new_surface ? 'New York 10:00' : 'UTC ' . $self->builder->build_trading_calendar->standard_closing_on($date)->time_hhmm; | ||
| 463 | |||
| 464 | return Quant::Framework::VolSurface::Cutoff->new($cutoff_string); | ||
| 465 | } | ||
| 466 | |||
| 467 | no Moose; | ||
| 468 | __PACKAGE__->meta->make_immutable; | ||
| 469 | |||
| 470 | 1; |