| 1 | package Quant::Framework::PartialTrading; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use List::MoreUtils qw(uniq); | ||
| 5 | use List::Util qw(first); | ||
| 6 | use Date::Utility; | ||
| 7 | use Carp qw(croak); | ||
| 8 | |||
| 9 | use Data::Chronicle::Reader; | ||
| 10 | use Data::Chronicle::Writer; | ||
| 11 | |||
| 12 | =head2 type | ||
| 13 | |||
| 14 | Partial trading means times when an exchange is opened later than usual (late_open) or closed earlier than usual (early_close). | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | has type => ( | ||
| 19 | is => 'ro', | ||
| 20 | required => 1, | ||
| 21 | ); | ||
| 22 | |||
| 23 | has [qw(calendar recorded_date)] => ( | ||
| 24 | is => 'ro', | ||
| 25 | ); | ||
| 26 | |||
| 27 | has chronicle_reader => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => 'Data::Chronicle::Reader', | ||
| 30 | ); | ||
| 31 | |||
| 32 | has chronicle_writer => ( | ||
| 33 | is => 'ro', | ||
| 34 | isa => 'Data::Chronicle::Writer', | ||
| 35 | ); | ||
| 36 | |||
| 37 | =head2 save | ||
| 38 | |||
| 39 | Save given calendar into Chronicle for partial-trading (either early-close or late-open). | ||
| 40 | This calendar will specify partial-trading dates for a number of symbols at the same time. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub save { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | croak "calendar and recorded_date are required when pass in either." if $self->calendar xor $self->recorded_date; | ||
| 48 | die "Invalid partial-trading type" if $self->type ne 'early_closes' and $self->type ne 'late_opens'; | ||
| 49 | |||
| 50 | my $cached_data = $self->chronicle_reader->get('partial_trading', $self->type); | ||
| 51 | my $recorded_epoch = $self->recorded_date->truncate_to_day->epoch; | ||
| 52 | my %relevant_dates = | ||
| 53 | map { $_ => $cached_data->{$_} } | ||
| 54 | grep { $_ >= $recorded_epoch } keys %$cached_data; | ||
| 55 | my %calendar = map { Date::Utility->new($_)->truncate_to_day->epoch => $self->calendar->{$_} } keys %{$self->calendar}; | ||
| 56 | |||
| 57 | foreach my $epoch (keys %calendar) { | ||
| 58 | unless ($relevant_dates{$epoch}) { | ||
| 59 | $relevant_dates{$epoch} = $calendar{$epoch}; | ||
| 60 | next; | ||
| 61 | } | ||
| 62 | foreach my $close_time (keys %{$calendar{$epoch}}) { | ||
| 63 | my @symbols_to_save = uniq(@{$relevant_dates{$epoch}{$close_time}}, @{$calendar{$epoch}{$close_time}}); | ||
| 64 | $relevant_dates{$epoch}{$close_time} = \@symbols_to_save; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | return $self->chronicle_writer->set('partial_trading', $self->type, \%relevant_dates, $self->recorded_date); | ||
| 69 | } | ||
| 70 | |||
| 71 | =head2 get_partial_trading_for | ||
| 72 | |||
| 73 | Returns a hash-ref for partial-tradings of the given type for a specific symbol. | ||
| 74 | You can also query historical data using this function. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub get_partial_trading_for { | ||
| 79 | my ($self, $symbol, $for_date) = @_; | ||
| 80 | |||
| 81 | my $cached = | ||
| 82 | $for_date | ||
| 83 | ? $self->chronicle_reader->get_for('partial_trading', $self->type, $for_date) | ||
| 84 | : $self->chronicle_reader->get('partial_trading', $self->type); | ||
| 85 | |||
| 86 | my %partial_tradings; | ||
| 87 | foreach my $epoch (keys %$cached) { | ||
| 88 | foreach my $close_time (keys %{$cached->{$epoch}}) { | ||
| 89 | my $symbols = $cached->{$epoch}{$close_time}; | ||
| 90 | $partial_tradings{$epoch} = $close_time | ||
| 91 | if (first { $symbol eq $_ } @$symbols); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | return \%partial_tradings; | ||
| 96 | } | ||
| 97 | |||
| 98 | __PACKAGE__->meta->make_immutable; | ||
| 99 | 1; |