| 1 | package Quant::Framework::Holiday; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use Carp qw(croak); | ||
| 5 | use Date::Utility; | ||
| 6 | use List::Util qw(first); | ||
| 7 | use List::MoreUtils qw(uniq); | ||
| 8 | |||
| 9 | use Data::Chronicle::Reader; | ||
| 10 | use Data::Chronicle::Writer; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Quant::Framework::Holiday - A module to save/load market holidays | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | This module saves/loads holidays to/from Chronicle. | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | around BUILDARGS => sub { | ||
| 23 | my $orig = shift; | ||
| 24 | my $class = shift; | ||
| 25 | my %params = ref $_[0] ? %{$_[0]} : @_; | ||
| 26 | |||
| 27 | if ($params{calendar} xor $params{recorded_date}) { | ||
| 28 | croak "calendar and recorded_date are required when pass in either."; | ||
| 29 | } | ||
| 30 | |||
| 31 | return $class->$orig(@_); | ||
| 32 | }; | ||
| 33 | |||
| 34 | has chronicle_reader => ( | ||
| 35 | is => 'ro', | ||
| 36 | isa => 'Data::Chronicle::Reader', | ||
| 37 | ); | ||
| 38 | |||
| 39 | has chronicle_writer => ( | ||
| 40 | is => 'ro', | ||
| 41 | isa => 'Data::Chronicle::Writer', | ||
| 42 | ); | ||
| 43 | |||
| 44 | has [qw(calendar recorded_date)] => ( | ||
| 45 | is => 'ro', | ||
| 46 | ); | ||
| 47 | |||
| 48 | =head2 save | ||
| 49 | |||
| 50 | Updates the current holiday calendar with the new inserts. | ||
| 51 | It trims the calendar by removing holiday before the recorded_date. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub save { | ||
| 56 | my $self = shift; | ||
| 57 | |||
| 58 | my $cached_holidays = $self->chronicle_reader->get('holidays', 'holidays'); | ||
| 59 | my $recorded_date = $self->recorded_date->truncate_to_day->epoch; | ||
| 60 | delete @{$cached_holidays}{grep { $_ < $recorded_date } keys %$cached_holidays}; | ||
| 61 | my $calendar = $self->calendar; | ||
| 62 | |||
| 63 | foreach my $new_holiday (keys %$calendar) { | ||
| 64 | my $epoch = Date::Utility->new($new_holiday)->truncate_to_day->epoch; | ||
| 65 | unless ($cached_holidays->{$epoch}) { | ||
| 66 | $cached_holidays->{$epoch} = $calendar->{$new_holiday}; | ||
| 67 | next; | ||
| 68 | } | ||
| 69 | foreach my $new_holiday_desc (keys %{$calendar->{$new_holiday}}) { | ||
| 70 | my $new_symbols = $calendar->{$new_holiday}{$new_holiday_desc}; | ||
| 71 | my $symbols_to_save = [uniq(@{$cached_holidays->{$epoch}{$new_holiday_desc}}, @$new_symbols)]; | ||
| 72 | $cached_holidays->{$epoch}{$new_holiday_desc} = $symbols_to_save; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | return $self->chronicle_writer->set('holidays', 'holidays', \%{$cached_holidays}, $self->recorded_date); | ||
| 77 | } | ||
| 78 | |||
| 79 | =head2 get_holidays_for | ||
| 80 | |||
| 81 | This method looks for holidays of the given symbol (at the optional given time) using | ||
| 82 | chronicle_reader object passed to it. Note that this method needs a chronicle reader | ||
| 83 | because it is not part of the Holiday class. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub get_holidays_for { | ||
| 88 | my ($reader, $symbol, $for_date) = @_; | ||
| 89 | |||
| 90 | my $calendar = | ||
| 91 | ($for_date) | ||
| 92 | ? $reader->get_for('holidays', 'holidays', $for_date) | ||
| 93 | : $reader->get('holidays', 'holidays'); | ||
| 94 | my %holidays; | ||
| 95 | foreach my $date (keys %$calendar) { | ||
| 96 | foreach my $holiday_desc (keys %{$calendar->{$date}}) { | ||
| 97 | $holidays{$date} = $holiday_desc if (first { $symbol eq $_ } @{$calendar->{$date}{$holiday_desc}}); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | return \%holidays; | ||
| 102 | } | ||
| 103 | |||
| 104 | no Moose; | ||
| 105 | __PACKAGE__->meta->make_immutable; | ||
| 106 | 1; |