| 1 | package Quant::Framework::Utils::Types; | ||
| 2 | |||
| 3 | 2 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 2 | ▶ | use MooseX::Types::Moose qw(Int Num Str); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Moose::Util::TypeConstraints; | ||
| 8 | |||
| 9 | subtype 'qf_date_object', as 'Date::Utility'; | ||
| 10 | coerce 'qf_date_object', from 'Str', via { Date::Utility->new($_) }; | ||
| 11 | |||
| 12 | =head2 qf_timestamp | ||
| 13 | |||
| 14 | A valid ISO8601 timestamp, restricted specifically to the YYYY-MM-DDTHH:MI:SS format. Optionally, "Z", "UTC", or "GMT" can be appended to the end. No other time zones are supported. | ||
| 15 | |||
| 16 | qf_timestamp can be coerced from C<Date::Utility> | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | subtype 'qf_timestamp', as Str, where { | ||
| 21 | if (/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|GMT|UTC)?$/) { | ||
| 22 | my $date = try { | ||
| 23 | DateTime->new( | ||
| 24 | year => $1, | ||
| 25 | month => $2, | ||
| 26 | day => $3, | ||
| 27 | hour => $4, | ||
| 28 | minute => $5, | ||
| 29 | second => $6, | ||
| 30 | time_zone => 'GMT' | ||
| 31 | ); | ||
| 32 | }; | ||
| 33 | return $date ? 1 : 0; | ||
| 34 | } else { | ||
| 35 | return 0; | ||
| 36 | } | ||
| 37 | }, message { | ||
| 38 | "Invalid timestamp $_, please use YYYY-MM-DDTHH:MM:SSZ format"; | ||
| 39 | }; | ||
| 40 | |||
| 41 | coerce 'qf_timestamp', from 'qf_date_object', via { $_->datetime_iso8601 }; | ||
| 42 | |||
| 43 | my @interest_rate_types = qw(implied market); | ||
| 44 | subtype 'qf_interest_rate_type', as Str, where { | ||
| 45 | my $regex = '(' . join('|', @interest_rate_types) . ')'; | ||
| 46 | /^$regex$/ | ||
| 47 | }, message { | ||
| 48 | "Invalid interest_rate type $_. Must be one of: " . join(', ', @interest_rate_types) | ||
| 49 | }; | ||
| 50 | |||
| 51 | =head2 qf_cutoff_code | ||
| 52 | |||
| 53 | A volatility surface cutoff time convention | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | subtype 'qf_cutoff_code', as Str, where { | ||
| 58 | /^(?:Bangkok|Beijing|Bucharest|Budapest|Colombia|Frankfurt|Hanoi|Istanbul|Jakarta|Kuala Lumpur|London|Manila|Mexico|Moscow|Mumbai|New York|PTAX \(Ask\)|Santiago|Sao Paulo|Seoul|Singapore|Taipei|Taiwan|Tel Aviv|Tokyo|UTC|GMT|Warsaw|Wellington) \d{1,2}:\d{2}$/; | ||
| 59 | }, message { | ||
| 60 | 'Invalid cutoff_code [' . $_ . ']'; | ||
| 61 | }; | ||
| 62 | |||
| 63 | =head2 qf_surface_type | ||
| 64 | |||
| 65 | Volatility surface types. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | my @surface_types = qw( delta flat moneyness); | ||
| 70 | subtype 'qf_surface_type', as Str, where { | ||
| 71 | my $regex = '(' . join('|', @surface_types) . ')'; | ||
| 72 | /^$regex$/; | ||
| 73 | }, message { | ||
| 74 | "Invalid surface type $_. Must be one of: " . join(', ', @surface_types); | ||
| 75 | }; | ||
| 76 | |||
| 77 | subtype 'qf_cutoff_helper', as 'Quant::Framework::VolSurface::Cutoff'; | ||
| 78 | coerce 'qf_cutoff_helper', from 'Str', via { Quant::Framework::VolSurface::Cutoff->new($_) }; | ||
| 79 | |||
| 80 | 1; | ||