File Coverage

blib/lib/Finance/Instrument/Futures.pm
Criterion Covered Total %
statement 53 56 94.6
branch 8 12 66.6
condition 4 10 40.0
subroutine 14 15 93.3
pod 0 1 0.0
total 79 94 84.0


line stmt bran cond sub pod time code
1             package Finance::Instrument::Futures;
2              
3 5     5   753838 use strict;
  5         13  
  5         193  
4 5     5   146 use 5.008_001;
  5         15  
  5         246  
5             our $VERSION = '0.01';
6              
7 5     5   2965 use Moose;
  5         1438670  
  5         41  
8 5     5   39101 use methods;
  5         126856  
  5         30  
9 5     5   9907 use Finance::Instrument::FuturesContract;
  5         21  
  5         257  
10 5     5   53 use List::MoreUtils qw(firstidx);
  5         9  
  5         1444  
11             extends 'Finance::Instrument';
12              
13             has multiplier => (is => "rw", isa => "Num");
14             has currency => (is => "rw", isa => "Str");
15             has underlying => (is => "rw", isa => "Maybe[Finance::Instrument]");
16             has last_trading_day_close => (is => "rw", isa => "Int");
17             has month_cycle => (is => "rw", isa => "Str");
18             has active_months => (is => "rw", isa => "Int");
19              
20             has contract_calendar => (is => "rw", isa => "HashRef", default => sub { {} });
21              
22             my $month_code = [qw(F G H J K M N Q U V X Z)];
23              
24             sub encode {
25 0     0 0 0 my $self = shift;
26 0   0     0 my $year_digit = shift || 2;
27 0         0 return $month_code->[$self->month-1].substr($self->year, -1 * $year_digit, $year_digit);
28             }
29              
30 5     5   2095 method near_term_contract($datetime) {
  2     2   20907  
  2         5  
  2         5  
31 2 50       19 my $date = ref($datetime) ? $datetime->ymd : $datetime;
32 2         42 my @contracts = reverse sort keys %{$self->contract_calendar};
  2         125  
33 2         11 for my $c_i (0..$#contracts-1) {
34 3         10 my ($curr, $prev) = map { $self->contract_calendar->{$contracts[$_]} } ($c_i, $c_i + 1);
  6         277  
35 3 100 66     38 if ($date le $curr->{last_trading_day} && $date gt $prev->{last_trading_day}) {
36 2         20 return $self->contract($contracts[$c_i]);
37             }
38             }
39             }
40              
41 5     5   2802 method contract($year, $month) {
  3     3   8  
  3         16  
  3         6  
42 3 100       27 unless ($month) {
43 2 50       32 ($year, $month) = $year =~ m/(\d\d\d\d)(\d\d)/
44             or Carp::croak "invalid expiry format";
45             }
46 3         17 my $expiry = sprintf("%04d%02d", $year, $month);
47 3   50     156 my $curr = $self->contract_calendar->{$expiry} || {};
48 3   33     120 my $fc = $self->domain->get($self->code.'_'.$expiry)
49             || Finance::Instrument::FuturesContract->new( futures => $self,
50             expiry_year => int($year),
51             expiry_month => int($month),
52             domain => $self->domain,
53             %$curr,
54             );
55             }
56              
57 5     5   2992 method previous_contract($year, $month) {
  1     1   4  
  1         2  
  1         3  
58 1         49 my $month_idx = index($self->month_cycle, $month_code->[$month-1]);
59 1 50       6 die "unknown month $month in month_cycle" if $month_idx < 0;
60 1 50       6 if ($month_idx == 0) {
61 1         2 --$year;
62             }
63 1         45 my $code = substr($self->month_cycle, $month_idx - 1, 1);
64              
65 1     12   13 $self->contract($year, 1 + firstidx { $_ eq $code } @$month_code);
  12         21  
66             }
67              
68             __PACKAGE__->meta->make_immutable;
69 5     5   1182 no Moose;
  5         10  
  5         41  
70              
71             1;
72             __END__
73              
74             =encoding utf-8
75              
76             =for stopwords
77              
78             =head1 NAME
79              
80             Finance::Instrument::Futures -
81              
82             =head1 SYNOPSIS
83              
84             use Finance::Instrument;
85              
86             =head1 DESCRIPTION
87              
88             Finance::Instrument is
89              
90             =head1 AUTHOR
91              
92             Chia-liang Kao E<lt>clkao@clkao.orgE<gt>
93              
94             =head1 LICENSE
95              
96             This library is free software; you can redistribute it and/or modify
97             it under the same terms as Perl itself.
98              
99             =head1 SEE ALSO
100              
101             =cut