| 1 | package Finance::Calendar; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | Finance::Calendar - represents the trading calendar. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | use Finance::Calendar; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | use Date::Utility; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | my $calendar = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | holidays => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | "25-Dec-2013" => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | "Christmas Day" => [qw(FOREX METAL)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | "1-Jan-2014" => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | "New Year's Day" => [qw( FOREX METAL)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | "1-Apr-2013" => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | "Easter Monday" => [qw( USD)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | early_closes => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | '24-Dec-2009' => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | '16:30' => ['HKSE'], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | '22-Dec-2016' => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | '18:00' => ['FOREX', 'METAL'], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | late_opens => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | '24-Dec-2010' => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | '14:30' => ['HKSE'], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | my $calendar = Finance::Calendar->new(calendar => $calendar); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | my $now = Date::Utility->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # Does London Stocks Exchange trade on $now | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | $calendar->trades_on(Finance::Exchange->create_exchange('LSE'), $now); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # Is it a country holiday for the United States on $now | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | $calendar->is_holiday_for('USD', $now); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # Returns the opening time of Australian Stocks Exchange on $now | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | $calendar->opening_on(Finance::Exchange->create_exchange('ASX'), $now); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # Returns the closing time of Forex on $now | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | $calendar->closing_on(Finance::Exchange->create_exchange('FOREX'), $now); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | =head1 DESCRIPTION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | This class is responsible for providing trading times or holidays related information of a given financial stock exchange on a specific date. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | use Moose; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | our $VERSION = '0.05'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | use List::Util qw(min max first); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | use Date::Utility; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | use Memoize; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | use Finance::Exchange; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | use Carp qw(croak); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | =head1 ATTRIBUTES - Object Construction | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | =head2 calendar | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | A hash reference that has information on: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | - exchange and country holidays | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | - late opens | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | - early closes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | has calendar => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | is => 'ro', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | required => 1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | has _cache => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | is => 'ro', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | default => sub { {} }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | sub _get_cache { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 542 | ▶ | my ($self, $method_name, $exchange, @dates) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_cache: called CC=2 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 542 | ▶ | return undef unless exists $self->_cache->{$method_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 534 | my $key = join "_", ($exchange->symbol, (map { $self->trading_date_for($exchange, $_)->epoch } @dates)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 534 | return $self->_cache->{$method_name}{$key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | sub _set_cache { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 211 | ▶ | my ($self, $value, $method_name, $exchange, @dates) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_cache: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 211 | my $key = join "_", ($exchange->symbol, (map { $self->trading_date_for($exchange, $_)->epoch } @dates)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 211 | $self->_cache->{$method_name}{$key} = $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 211 | return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | =head1 METHODS - TRADING DAYS RELATED | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | =head2 trades_on | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | ->trades_on($exchange_object, $date_object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | Returns true if trading is done on the day of a given Date::Utility. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | sub trades_on { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 320 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trades_on: called CC=4 SCAR=13.9
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 320 | ▶ | if (my $cache = $self->_get_cache('trades_on', $exchange, $when)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 208 | return $cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 112 | my $really_when = $self->trading_date_for($exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 112 | ▶ | my $result = (@{$exchange->trading_days_list}[$really_when->day_of_week] && !$self->is_holiday_for($exchange->symbol, $really_when)) ? 1 : 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$exchange->trading_days_list;}[$really_when->day_of_week] and not $self->is_holiday_for($exchange->symbol, $really_when)
MC/DC100%2/2
@{$exchange->trading_days_list;}[$really_when->day_of_week] and not $self->is_holiday_for($exchange->symbol, $really_when)
@{$exchange->trading_days_list;}[$really_when->day_of_week]
not $self->is_holiday_for($exchange->symbol, $really_when)
Truth table100%3/3
A and not(B)
A= @{$exchange->trading_days_list;}[$really_when->day_of_week]B= not $self->is_holiday_for($exchange->symbol, $really_when)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 112 | $self->_set_cache($result, 'trades_on', $exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 112 | return $result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | =head2 trade_date_before | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | ->trade_date_before($exchange_object, $date_object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | Returns a Date::Utility object for the previous trading day of an exchange for the given date. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | sub trade_date_before { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 5 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trade_date_before: called CC=5 SCAR=16.2
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 5 | my $begin = $self->trading_date_for($exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 5 | ▶ | if (my $cache = $self->_get_cache('trade_date_before', $exchange, $begin)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | return $cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 5 | my $date_behind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 5 | my $counter = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # look back at most 10 days. The previous trading day could have span over a weekend with multiple consecutive holidays. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # Previously it was 7 days, but need to increase a little bit since there is a case | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # where the holidays was more than 7. That was during end of ramadhan at Saudi Arabia Exchange. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 5 | ▶ | while (not $date_behind and $counter < 10) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $date_behind and $counter < 10
MC/DC50%1/2
not $date_behind and $counter < 10
not $date_behind
$counter < 10
Truth table66%2/3
not(A) and B
A= not $date_behindB= $counter < 10
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 8 | my $possible = $begin->minus_time_interval($counter . 'd'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 8 | ▶ | $date_behind = $possible if $self->trades_on($exchange, $possible); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 8 | $counter++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 5 | $self->_set_cache($date_behind, 'trade_date_before', $exchange, $begin); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 5 | return $date_behind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | =head2 trade_date_after | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | ->trade_date_after($exchange_object, $date_object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | Returns a Date::Utility object of the next trading day of an exchange for a given date. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | sub trade_date_after { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 19 | ▶ | my ($self, $exchange, $date) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trade_date_after: called CC=5 SCAR=16.1
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 19 | my $date_next; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 19 | my $counter = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 19 | my $begin = $self->trading_date_for($exchange, $date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 19 | ▶ | if (my $cache = $self->_get_cache('trade_date_after', $exchange, $begin)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 2 | return $cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # look forward at most 11 days. The next trading day could have span over a weekend with multiple consecutive holidays. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # We chosed 11 due to the fact that the longest trading holidays we have got so far was 10 days(TSE). | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 17 | ▶ | while (not $date_next and $counter <= 11) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $date_next and $counter <= 11
MC/DC50%1/2
not $date_next and $counter <= 11
not $date_next
$counter <= 11
Truth table66%2/3
not(A) and B
A= not $date_nextB= $counter <= 11
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 30 | my $possible = $begin->plus_time_interval($counter . 'd'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 30 | ▶ | $date_next = $possible if $self->trades_on($exchange, $possible); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 30 | $counter++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 17 | $self->_set_cache($date_next, 'trade_date_after', $exchange, $begin); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 17 | return $date_next; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | =head2 trading_date_for | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | ->trading_date_for($exchange_object, $date_object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | The date on which trading is considered to be taking place even if it is not the same as the GMT date. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | Note that this does not handle trading dates are offset forward beyond the next day (24h). It will need additional work if these are found to exist. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | Returns a Date object representing midnight GMT of the trading date. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | sub trading_date_for { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 1037 | ▶ | my ($self, $exchange, $date) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trading_date_for: called CC=4 SCAR=13.9
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # if there's no pre-midnight open, then returns the same day. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 1037 | ▶ | return $date->truncate_to_day unless ($exchange->trading_date_can_differ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 35 | my $next_day = $date->plus_time_interval('1d')->truncate_to_day; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | my $open_ti = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 35 | $exchange->market_times->{$self->_times_dst_key($exchange, $next_day)}->{daily_open}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 35 | ▶ | return $next_day if ($open_ti and $next_day->epoch + $open_ti->seconds <= $date->epoch); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$open_ti and $next_day->epoch + $open_ti->seconds <= $date->epoch
MC/DC50%1/2
$open_ti and $next_day->epoch + $open_ti->seconds <= $date->epoch
$open_ti
$next_day->epoch + $open_ti->seconds <= $date->epoch
Truth table66%2/3
A and B
A= $open_tiB= $next_day->epoch + $open_ti->seconds <= $date->epoch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 34 | return $date->truncate_to_day; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | =head2 calendar_days_to_trade_date_after | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | ->calendar_days_to_trade_date_after($exchange_object, $date_object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | Returns the number of calendar days between a given Date::Utility | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | and the next day on which trading is open. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | sub calendar_days_to_trade_date_after { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 6 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
calendar_days_to_trade_date_after: called CC=2 SCAR=7.2
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 6 | ▶ | if (my $cache = $self->_get_cache('calendar_days_to_trade_date_after', $exchange, $when)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | return $cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 6 | my $number_of_days = $self->trade_date_after($exchange, $when)->days_between($when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 6 | $self->_set_cache($number_of_days, 'calendar_days_to_trade_date_after', $exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 6 | return $number_of_days; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | =head2 trading_days_between | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | ->trading_days_between($exchange_object, Date::Utility->new('4-May-10'),Date::Utility->new('5-May-10')); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | Returns the number of trading days _between_ two given dates. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | sub trading_days_between { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 4 | ▶ | my ($self, $exchange, $begin, $end) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trading_days_between: called CC=2 SCAR=7.1
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 4 | ▶ | if (my $cache = $self->_get_cache('trading_days_between', $exchange, $begin, $end)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | return $cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | # Count up how many are trading days. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 4 | my $number_of_days = scalar grep { $self->trades_on($exchange, $_) } @{$self->_days_between($begin, $end)}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 4 | $self->_set_cache($number_of_days, 'trading_days_between', $exchange, $begin, $end); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 4 | return $number_of_days; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | =head2 holiday_days_between | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | ->holiday_days_between($exchange_object, Date::Utility->new('4-May-10'),Date::Utility->new('5-May-10')); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | Returns the number of holidays _between_ two given dates. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | sub holiday_days_between { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 2 | ▶ | my ($self, $exchange, $begin, $end) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
holiday_days_between: called CC=2 SCAR=7.1
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 2 | ▶ | if (my $cache = $self->_get_cache('holiday_days_between', $exchange, $begin, $end)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | return $cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | # Count up how many are trading days. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 2 | my $number_of_days = scalar grep { $self->is_holiday_for($exchange->symbol, $_) } @{$self->_days_between($begin, $end)}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 2 | $self->_set_cache($number_of_days, 'holiday_days_between', $exchange, $begin, $end); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 2 | return $number_of_days; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | =head1 METHODS - TRADING TIMES RELATED. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | =head2 is_open | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | ->is_open($exchange_object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | Returns true is exchange is open now, false otherwise. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | sub is_open { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 1 | ▶ | my ($self, $exchange) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_open: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 1 | return $self->is_open_at($exchange, Date::Utility->new); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | =head2 is_open_at | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | ->is_open_at($exchange_object, $epoch); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | Return true is exchange is open at the given epoch, false otherwise. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | sub is_open_at { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 17 | ▶ | my ($self, $exchange, $date) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_open_at: called CC=5 SCAR=16.1
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 17 | my $opening = $self->opening_on($exchange, $date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 17 | ▶ | return undef if (not $opening or $self->_is_in_trading_break($exchange, $date)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $opening or $self->_is_in_trading_break($exchange, $date)
MC/DC100%2/2
not $opening or $self->_is_in_trading_break($exchange, $date)
not $opening
$self->_is_in_trading_break($exchange, $date)
Truth table100%3/3
not(A) or B
A= not $openingB= $self->_is_in_trading_break($exchange, $date)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 14 | ▶ | return 1 if (not $date->is_before($opening) and not $date->is_after($self->closing_on($exchange, $date))); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$date->is_before($opening) or $date->is_after($self->closing_on($exchange, $date))
MC/DC100%2/2
$date->is_before($opening) or $date->is_after($self->closing_on($exchange, $date))
$date->is_before($opening)
$date->is_after($self->closing_on($exchange, $date))
Truth table100%3/3
A or B
A= $date->is_before($opening)B= $date->is_after($self->closing_on($exchange, $date))
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # if everything falls through, assume it is not open | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 11 | return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | =head2 seconds_since_open_at | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | ->seconds_since_open_at($exchange_object, $epoch); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | Returns the number of seconds since the exchange opened from the given epoch. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | sub seconds_since_open_at { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 5 | ▶ | my ($self, $exchange, $date) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
seconds_since_open_at: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 5 | return $self->_market_opens($exchange, $date)->{'opened'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | =head2 seconds_since_close_at | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | ->seconds_since_close_at($exchange_object, $epoch); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | Returns the number of seconds since the exchange closed from the given epoch. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | sub seconds_since_close_at { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 5 | ▶ | my ($self, $exchange, $date) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
seconds_since_close_at: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 5 | return $self->_market_opens($exchange, $date)->{'closed'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | =head2 opening_on | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | ->opening_on($exchange_object, Date::Utility->new('25-Dec-10')); # returns undef (given Xmas is a holiday) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | Returns the opening time (Date::Utility) of the exchange for a given Date::Utility, undefined otherwise. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | sub opening_on { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 108 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opening_on: called CC=3 SCAR=11.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 108 | ▶ | if (my $cache = $self->_get_cache('opening_on', $exchange, $when)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 68 | return $cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 40 | ▶ | my $opening_on = $self->opens_late_on($exchange, $when) // $self->get_exchange_open_times($exchange, $when, 'daily_open'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->opens_late_on($exchange, $when) // $self->get_exchange_open_times($exchange, $when, "daily_open")
MC/DC100%2/2
$self->opens_late_on($exchange, $when) // $self->get_exchange_open_times($exchange, $when, "daily_open")
$self->opens_late_on($exchange, $when)
$self->get_exchange_open_times($exchange, $when, "daily_open")
Truth table100%3/3
A // B
A= $self->opens_late_on($exchange, $when)B= $self->get_exchange_open_times($exchange, $when, "daily_open")
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 40 | $self->_set_cache($opening_on, 'opening_on', $exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 40 | return $opening_on; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | =head2 closing_on | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | ->closing_on($exchange_object, Date::Utility->new('25-Dec-10')); # returns undef (given Xmas is a holiday) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | Returns the closing time (Date::Utility) of the exchange for a given Date::Utility, undefined otherwise. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | sub closing_on { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 78 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
closing_on: called CC=3 SCAR=11.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 78 | ▶ | if (my $cache = $self->_get_cache('closing_on', $exchange, $when)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 53 | return $cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 25 | ▶ | my $closing_on = $self->closes_early_on($exchange, $when) // $self->get_exchange_open_times($exchange, $when, 'daily_close'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->closes_early_on($exchange, $when) // $self->get_exchange_open_times($exchange, $when, "daily_close")
MC/DC100%2/2
$self->closes_early_on($exchange, $when) // $self->get_exchange_open_times($exchange, $when, "daily_close")
$self->closes_early_on($exchange, $when)
$self->get_exchange_open_times($exchange, $when, "daily_close")
Truth table100%3/3
A // B
A= $self->closes_early_on($exchange, $when)B= $self->get_exchange_open_times($exchange, $when, "daily_close")
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 25 | $self->_set_cache($closing_on, 'closing_on', $exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 25 | return $closing_on; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | =head2 trading_breaks | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | ->trading_breaks($exchange_object, $date_object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | Defines the breaktime for this exchange. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | sub trading_breaks { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 81 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trading_breaks: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 81 | return $self->get_exchange_open_times($exchange, $when, 'trading_breaks'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | =head2 regularly_adjusts_trading_hours_on | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | Returns a hashref of special-case changes that may apply on specific | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | trading days. Currently, this applies on Fridays only: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | =over 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | =item * for forex or metals | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | =back | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | Example: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | $calendar->regularly_adjusts_trading_hours_on('FOREX', time); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | sub regularly_adjusts_trading_hours_on { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 57 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
regularly_adjusts_trading_hours_on: called CC=4 SCAR=13.9
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # Only applies on Fridays | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 57 | ▶ | return undef if $when->day_of_week != 5; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 19 | my $changes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 19 | my $rule = 'Fridays'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 19 | ▶ | if ($exchange->symbol eq 'FOREX' or $exchange->symbol eq 'METAL') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$exchange->symbol eq "FOREX" or $exchange->symbol eq "METAL"
MC/DC100%2/2
$exchange->symbol eq "FOREX" or $exchange->symbol eq "METAL"
$exchange->symbol eq "FOREX"
$exchange->symbol eq "METAL"
Truth table100%3/3
A or B
A= $exchange->symbol eq "FOREX"B= $exchange->symbol eq "METAL"
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 4 | $changes = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 'daily_close' => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | to => '20h55m', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | rule => $rule, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | }}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 19 | return $changes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | =head2 closes_early_on | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | ->closes_early_on($exchange_object, $date_object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | Returns the closing time as a L<Date::Utility> instance if the exchange closes early on the given date, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | or C<undef>. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | sub closes_early_on { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 31 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
closes_early_on: called CC=5 SCAR=16.1
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 31 | ▶ | return undef unless $self->trades_on($exchange, $when); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 27 | my $closes_early; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 27 | my $listed = $self->_get_partial_trading_for($exchange, 'early_closes', $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 27 | ▶ | if ($listed) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 3 | $closes_early = $when->truncate_to_day->plus_time_interval($listed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | } elsif (my $scheduled_changes = $self->regularly_adjusts_trading_hours_on($exchange, $when)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | $closes_early = $when->truncate_to_day->plus_time_interval($scheduled_changes->{daily_close}->{to}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 2 | ▶ | if ($scheduled_changes->{daily_close}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 27 | return $closes_early; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | =head2 opens_late_on | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | ->opens_late_on($exchange_object, $date_object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | Returns true if the exchange opens late on the given date. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | sub opens_late_on { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 44 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opens_late_on: called CC=5 SCAR=16.8
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 44 | ▶ | return undef unless $self->trades_on($exchange, $when); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 31 | my $opens_late; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 31 | my $listed = $self->_get_partial_trading_for($exchange, 'late_opens', $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 31 | ▶ | if ($listed) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 2 | $opens_late = $when->truncate_to_day->plus_time_interval($listed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | } elsif (my $scheduled_changes = $self->regularly_adjusts_trading_hours_on($exchange, $when)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | $opens_late = $when->truncate_to_day->plus_time_interval($scheduled_changes->{daily_open}->{to}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | ▶ | if ($scheduled_changes->{daily_open}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | 31 | return $opens_late; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | =head2 seconds_of_trading_between_epochs | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | ->seconds_of_trading_between_epochs($exchange_object, $epoch1, $epoch2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | Get total number of seconds of trading time between two epochs accounting for breaks. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 2 | my $full_day = 86400; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | sub seconds_of_trading_between_epochs { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 22 | ▶ | my ($self, $exchange, $start, $end) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
seconds_of_trading_between_epochs: called CC=6 SCAR=17.9
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 22 | my ($start_epoch, $end_epoch) = ($start->epoch, $end->epoch); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 22 | my $result = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | # step 1: calculate non-cached incomplete start-day and end_dates | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | 22 | my $day_start = $start_epoch - ($start_epoch % $full_day); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 22 | my $day_end = $end_epoch - ($end_epoch % $full_day); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 22 | ▶ | if (($day_start != $start_epoch) && ($start_epoch < $end_epoch)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$day_start != $start_epoch and $start_epoch < $end_epoch
MC/DC50%1/2
$day_start != $start_epoch and $start_epoch < $end_epoch
$day_start != $start_epoch
$start_epoch < $end_epoch
Truth table66%2/3
A and B
A= $day_start != $start_epochB= $start_epoch < $end_epoch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | 17 | $result += $self->_computed_trading_seconds($exchange, $start_epoch, min($day_start + 86399, $end_epoch)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 17 | $start_epoch = $day_start + $full_day; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | 22 | ▶ | if (($day_end != $end_epoch) && ($start_epoch < $end_epoch)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$day_end != $end_epoch and $start_epoch < $end_epoch
MC/DC100%2/2
$day_end != $end_epoch and $start_epoch < $end_epoch
$day_end != $end_epoch
$start_epoch < $end_epoch
Truth table100%3/3
A and B
A= $day_end != $end_epochB= $start_epoch < $end_epoch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 10 | $result += $self->_computed_trading_seconds($exchange, max($start_epoch, $day_end), $end_epoch); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 10 | $end_epoch = $day_end; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | # step 2: calculate intermediated values (which are guaranteed to be day-boundary) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | # with cache-aware way | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 22 | ▶ | if ($start_epoch < $end_epoch) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | 7 | $result += $self->_seconds_of_trading_between_epochs_days_boundary($exchange, $start_epoch, $end_epoch); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 22 | return $result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | =head2 regular_trading_day_after | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | ->regular_trading_day_after($exchange_object, $date_object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | Returns a Date::Utility object on a trading day where the exchange does not close early or open late after the given date. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | sub regular_trading_day_after { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 2 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
regular_trading_day_after: called CC=6 SCAR=20.2
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | 2 | ▶ | return undef if $self->closing_on($exchange, $when); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | 2 | my $counter = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 2 | my $regular_trading_day = $self->trade_date_after($exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | 2 | ▶ | while ($counter <= 10) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | 2 | my $possible = $regular_trading_day->plus_time_interval($counter . 'd'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | 2 | ▶ | if ( not $self->closes_early_on($exchange, $possible) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not $self->closes_early_on($exchange, $possible) || $self->opens_late_on($exchange, $possible) and $self->trades_on($exchange, $possible)
$self->closes_early_on($exchange, $possible) || $self->opens_late_on($exchange, $possible)
MC/DC0%0/3
not $self->closes_early_on($exchange, $possible) || $self->opens_late_on($exchange, $possible) and $self->trades_on($exchange, $possible)
$self->closes_early_on($exchange, $possible)
$self->opens_late_on($exchange, $possible)
$self->trades_on($exchange, $possible)
Truth table25%1/4
not(A || B) and C
A= $self->closes_early_on($exchange, $possible)B= $self->opens_late_on($exchange, $possible)C= $self->trades_on($exchange, $possible)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | and not $self->opens_late_on($exchange, $possible) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | and $self->trades_on($exchange, $possible)) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | 2 | $regular_trading_day = $possible; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | 2 | last; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 0 | $counter++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 2 | return $regular_trading_day; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | =head2 trading_period | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | ->trading_period('HKSE', Date::Utility->new); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | Returns an array reference of hash references of open and close time of the given exchange and epoch | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | sub trading_period { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 2 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trading_period: called CC=5 SCAR=16.1
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 2 | ▶ | return [] if not $self->trades_on($exchange, $when); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 2 | my $open = $self->opening_on($exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 2 | my $close = $self->closing_on($exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | 2 | my $breaks = $self->trading_breaks($exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | 2 | my @times = ($open); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 2 | ▶ | if (defined $breaks) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 1 | push @times, @{$_} for @{$breaks}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 2 | push @times, $close; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | 2 | my @periods; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 2 | ▶ | for (my $i = 0; $i < $#times; $i += 2) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 3 | push @periods, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | open => $times[$i]->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | close => $times[$i + 1]->epoch | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | 2 | return \@periods; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | =head2 is_holiday_for | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | Check if it is a holiday for a specific exchange or a country on a specific day | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | ->is_holiday_for('ASX', '2013-01-01'); # Australian exchange holiday | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | ->is_holiday_for('USD', Date::Utility->new); # United States country holiday | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | Returns the description of the holiday if it is a holiday. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | sub is_holiday_for { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 82 | ▶ | my ($self, $symbol, $date) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_holiday_for: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 82 | return $self->_get_holidays_for($symbol, $date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | =head2 is_in_dst_at | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | ->is_in_dst_at($exchange_object, $date_object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | Is this exchange trading on daylight savings times for the given epoch? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | sub is_in_dst_at { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 163 | ▶ | my ($self, $exchange, $epoch) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_in_dst_at: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 163 | return Date::Utility->new($epoch)->is_dst_in_zone($exchange->trading_timezone); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | ### PRIVATE ### | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | sub _get_holidays_for { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 82 | ▶ | my ($self, $symbol, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_holidays_for: called CC=4 SCAR=13.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 82 | my $date = $when->truncate_to_day->epoch; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | 82 | my $calendar = $self->calendar->{holidays}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 82 | my $holiday = $calendar->{$date}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 82 | ▶ | return undef unless $holiday; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | 24 | foreach my $holiday_desc (keys %$holiday) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 24 | ▶ | return $holiday_desc if (first { $symbol eq $_ } @{$holiday->{$holiday_desc}}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | 2 | return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | sub _is_in_trading_break { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | 16 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_in_trading_break: called CC=5 SCAR=16.1
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | 16 | $when = Date::Utility->new($when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | 16 | my $in_trading_break = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | 16 | ▶ | if (my $breaks = $self->trading_breaks($exchange, $when)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | 5 | foreach my $break_interval (@{$breaks}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | 5 | ▶ | if ($when->epoch >= $break_interval->[0]->epoch and $when->epoch <= $break_interval->[1]->epoch) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$when->epoch >= $break_interval->[0]->epoch and $when->epoch <= $break_interval->[1]->epoch
MC/DC100%2/2
$when->epoch >= $break_interval->[0]->epoch and $when->epoch <= $break_interval->[1]->epoch
$when->epoch >= $break_interval->[0]->epoch
$when->epoch <= $break_interval->[1]->epoch
Truth table100%3/3
A and B
A= $when->epoch >= $break_interval->[0]->epochB= $when->epoch <= $break_interval->[1]->epoch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | 2 | $in_trading_break++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | 2 | last; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 16 | return $in_trading_break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | =head2 get_exchange_open_times | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | Query an exchange for valid opening times. Expects 3 parameters: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | =over 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | =item * C<$exchange> - a L<Finance::Exchange> instance | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | =item * C<$date> - a L<Date::Utility> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | =item * C<$which> - which market information to request, see below | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | =back | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | The possible values for C<$which> include: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | =over 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | =item * C<daily_open> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | =item * C<daily_close> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | =item * C<trading_breaks> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | =back | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | Returns either C<undef>, a single L<Date::Utility>, or an arrayref of L<Date::Utility> instances. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | sub get_exchange_open_times { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | 143 | ▶ | my ($self, $exchange, $date, $which) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_exchange_open_times: called CC=9 SCAR=22.2
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | 143 | ▶ | my $when = (ref $date) ? $date : Date::Utility->new($date); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | 143 | my $that_midnight = $self->trading_date_for($exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | 143 | my $requested_time; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | 143 | ▶ | if ($self->trades_on($exchange, $that_midnight)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | 126 | my $dst_key = $self->_times_dst_key($exchange, $that_midnight); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | 126 | my $ti = $exchange->market_times->{$dst_key}->{$which}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | 126 | my $extended_lunch_hour; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | 126 | ▶ | if ($which eq 'trading_breaks') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 81 | my $extended_trading_breaks = $exchange->market_times->{$dst_key}->{day_of_week_extended_trading_breaks}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 81 | ▶ | $extended_lunch_hour = ($extended_trading_breaks and $when->day_of_week == $extended_trading_breaks) ? 1 : 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$extended_trading_breaks and $when->day_of_week == $extended_trading_breaks
MC/DC0%0/2
$extended_trading_breaks and $when->day_of_week == $extended_trading_breaks
$extended_trading_breaks
$when->day_of_week == $extended_trading_breaks
Truth table33%1/3
A and B
A= $extended_trading_breaksB= $when->day_of_week == $extended_trading_breaks
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 126 | ▶ | if ($ti) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | 106 | ▶ | if (ref $ti eq 'ARRAY') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | 61 | ▶ | my $trading_breaks = $extended_lunch_hour ? @$ti[1] : @$ti[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | 61 | my $start_of_break = $that_midnight->plus_time_interval($trading_breaks->[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | 61 | my $end_of_break = $that_midnight->plus_time_interval($trading_breaks->[1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | 61 | push @{$requested_time}, [$start_of_break, $end_of_break]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | } else { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | 45 | $requested_time = $that_midnight->plus_time_interval($ti); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 143 | return $requested_time; # returns null on no trading days. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | sub _times_dst_key { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | 161 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_times_dst_key: called CC=3 SCAR=11.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | 161 | ▶ | my $epoch = (ref $when) ? $when->epoch : $when; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | 161 | ▶ | return 'dst' if $self->is_in_dst_at($exchange, $epoch); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | 115 | return 'standard'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | # get partial trading data for a given exchange | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | sub _get_partial_trading_for { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | 58 | ▶ | my ($self, $exchange, $type, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_partial_trading_for: called CC=4 SCAR=14.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | 58 | my $cached = $self->calendar->{$type}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | 58 | my $date = $when->truncate_to_day->epoch; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | 58 | my $partial_defined = $cached->{$date}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | 58 | ▶ | return undef unless $partial_defined; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | 5 | foreach my $close_time (keys %{$cached->{$date}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | 5 | my $symbols = $cached->{$date}{$close_time}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | 5 | ▶ | return $close_time if (first { $exchange->symbol eq $_ } @$symbols); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | 0 | return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | sub _days_between { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 6 | ▶ | my ($self, $begin, $end) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_days_between: called CC=2 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 6 | my @days_between = (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | # Don't include start and end days. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | 6 | my $current = Date::Utility->new($begin)->truncate_to_day->plus_time_interval('1d'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | 6 | $end = Date::Utility->new($end)->truncate_to_day->minus_time_interval('1d'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | # Generate all days between. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | 6 | ▶ | while (not $current->is_after($end)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | 17 | push @days_between, $current; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | 17 | $current = $current->plus_time_interval('1d'); # Next day, please! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | 6 | return \@days_between; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | 2 | Memoize::memoize('_days_between', NORMALIZER => '_normalize_on_just_dates'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | =head2 next_open_at | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | ->next_open_at($exchange_object, Date::Utility->new('2023-02-16 15:30:00')); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | Returns Date::Utility object of the next opening date and time. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | Returns undef if exchange is open for the requested date. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | sub next_open_at { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | 7 | ▶ | my ($self, $exchange, $date) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next_open_at: called CC=10 SCAR=24.2
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | 7 | ▶ | return undef if $self->is_open_at($exchange, $date); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | 7 | my $market_opens = $self->_market_opens($exchange, $date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | # exchange is closed for the trading day | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | 7 | ▶ | unless (defined $market_opens->{open}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | 4 | my $next_trading = $self->trade_date_after($exchange, $date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | 4 | return $self->opening_on($exchange, $next_trading); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | # exchange is closed for trading breaks, will open again | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | 3 | ▶ | unless ($market_opens->{open}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | 3 | my $trading_breaks = $self->trading_breaks($exchange, $date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 3 | foreach my $break ($trading_breaks->@*) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | 2 | my ($close, $open) = $break->@*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | # Between trading brakes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | 2 | ▶ | if ($date->is_after($close) and $date->is_before($open)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$date->is_after($close) and $date->is_before($open)
MC/DC50%1/2
$date->is_after($close) and $date->is_before($open)
$date->is_after($close)
$date->is_before($open)
Truth table66%2/3
A and B
A= $date->is_after($close)B= $date->is_before($open)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | 1 | return $open; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | ▶ | } elsif ($date->is_before($close) and $date->is_after($date->truncate_to_day)) { # Between midnight and first opening | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$date->is_before($close) and $date->is_after($date->truncate_to_day)
MC/DC0%0/2
$date->is_before($close) and $date->is_after($date->truncate_to_day)
$date->is_before($close)
$date->is_after($date->truncate_to_day)
Truth table33%1/3
A and B
A= $date->is_before($close)B= $date->is_after($date->truncate_to_day)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | 1 | return $self->opening_on($exchange, $date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | # When there is no trading break but opens on same day | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | 1 | ▶ | if (!@$trading_breaks) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | 1 | my $opening_late = $self->opening_on($exchange, $date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | 1 | return $opening_late; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | # we shouldn't reach here but, return undef instead of a wrong time here. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | 0 | return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | ## PRIVATE _market_opens | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | # PARAMETERS : | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | # - time : the time as a timestamp | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | # RETURNS : A reference to a hash with the following keys: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | # - open : is set to 1 if the market is currently open, 0 if market is closed | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | # but will open, 'undef' if market is closed and will not open again | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | # today. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | # - closed : undefined if market has not been open yet, otherwise contains the | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | # seconds for how long the market was closed. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | # - opens : undefined if market is currently open and does not open anymore today, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | # otherwise the market will open in 'opens' seconds. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | # - closes : undefined if open is undef, otherwise market will close in 'closes' seconds. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | # - opened : undefined if market is closed, contains the seconds the market has | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | # been open. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | ######## | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | sub _market_opens { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | 17 | ▶ | my ($self, $exchange, $when) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_market_opens: called CC=26 SCAR=42.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | 17 | my $date = $when; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | # Figure out which "trading day" we are on | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | # even if it differs from the GMT calendar day. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | 17 | my $next_day = $date->plus_time_interval('1d')->truncate_to_day; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | 17 | my $next_open = $self->opening_on($exchange, $next_day); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | 17 | ▶ | $date = $next_day if ($next_open and not $date->is_before($next_open)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$next_open and not $date->is_before($next_open)
MC/DC0%0/2
$next_open and not $date->is_before($next_open)
$next_open
not $date->is_before($next_open)
Truth table66%2/3
A and not(B)
A= $next_openB= not $date->is_before($next_open)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | 17 | my $open = $self->opening_on($exchange, $date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | 17 | my $close = $self->closing_on($exchange, $date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | 17 | ▶ | if (not $open) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | # date is not a trading day: will not and has not been open today | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | 2 | my $next_open = $self->opening_on($exchange, $self->trade_date_after($exchange, $when)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | return { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | 2 | open => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | opens => $next_open->epoch - $when->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | opened => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | closes => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | closed => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | 15 | my $breaks = $self->trading_breaks($exchange, $when); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | # not trading breaks | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | 15 | ▶ | if (not $breaks) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | # Past closing time: opens next trading day, and has been open today | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | 7 | ▶ | if ($close and not $when->is_before($close)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
$close and not $when->is_before($close)
MC/DC50%1/2
$close and not $when->is_before($close)
$close
not $when->is_before($close)
Truth table66%2/3
A and not(B)
A= $closeB= not $when->is_before($close)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | return { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | 4 | open => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | opens => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | opened => $when->epoch - $open->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | closes => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | closed => $when->epoch - $close->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | } elsif ($when->is_before($open)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | return { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | 3 | open => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | opens => $open->epoch - $when->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | opened => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | closes => $close->epoch - $when->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | closed => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | ▶ | } elsif ($when->is_same_as($open) or ($when->is_after($open) and $when->is_before($close)) or $when->is_same_same($close)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/9
$when->is_same_as($open) or $when->is_after($open) and $when->is_before($close) or $when->is_same_same($close)
$when->is_same_as($open) or $when->is_after($open) and $when->is_before($close)
$when->is_after($open) and $when->is_before($close)
MC/DC0%0/4
$when->is_same_as($open) or $when->is_after($open) and $when->is_before($close) or $when->is_same_same($close)
$when->is_same_as($open)
$when->is_after($open)
$when->is_before($close)
$when->is_same_same($close)
Truth table0%0/6
A or B and C or D
A= $when->is_same_as($open)B= $when->is_after($open)C= $when->is_before($close)D= $when->is_same_same($close)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | return { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | 0 | open => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | opens => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | opened => $when->epoch - $open->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | closes => $close->epoch - $when->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | closed => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | } else { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | 8 | my @breaks = @$breaks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | # Past closing time: opens next trading day, and has been open today | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | 8 | ▶ | if ($close and not $when->is_before($close)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$close and not $when->is_before($close)
MC/DC50%1/2
$close and not $when->is_before($close)
$close
not $when->is_before($close)
Truth table66%2/3
A and not(B)
A= $closeB= not $when->is_before($close)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | return { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | 3 | open => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | opens => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | opened => $when->epoch - $breaks[-1][1]->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | closes => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | closed => $when->epoch - $close->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | } elsif ($when->is_before($open)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | return { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | 1 | open => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | opens => $open->epoch - $when->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | opened => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | closes => $breaks[0][0]->epoch - $when->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | closed => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | } else { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | 4 | my $current_open = $open; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | 4 | ▶ | for (my $i = 0; $i <= $#breaks; $i++) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | 4 | my $int_open = $breaks[$i][0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | 4 | my $int_close = $breaks[$i][1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | 4 | ▶ | my $next_open = exists $breaks[$i + 1] ? $breaks[$i + 1][0] : $close; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | 4 | ▶ | if ($when->is_before($int_open) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition50%3/6
$when->is_before($int_open) and $when->is_same_as($current_open) || $when->is_after($current_open)
$when->is_same_as($current_open) or $when->is_after($current_open)
MC/DC33%1/3
$when->is_before($int_open) and $when->is_same_as($current_open) || $when->is_after($current_open)
$when->is_before($int_open)
$when->is_same_as($current_open)
$when->is_after($current_open)
Truth table50%2/4
A and B or C
A= $when->is_before($int_open)B= $when->is_same_as($current_open)C= $when->is_after($current_open)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | and ($when->is_same_as($current_open) or $when->is_after($current_open))) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | return { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | 1 | open => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | opens => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | opened => $when->epoch - $current_open->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | closes => $int_open->epoch - $when->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | closed => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | ▶ | } elsif ($when->is_same_as($int_open) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%3/9
$when->is_same_as($int_open) or $when->is_after($int_open) and $when->is_before($int_close) or $when->is_same_as($int_close)
$when->is_same_as($int_open) or $when->is_after($int_open) and $when->is_before($int_close)
$when->is_after($int_open) and $when->is_before($int_close)
MC/DC0%0/4
$when->is_same_as($int_open) or $when->is_after($int_open) and $when->is_before($int_close) or $when->is_same_as($int_close)
$when->is_same_as($int_open)
$when->is_after($int_open)
$when->is_before($int_close)
$when->is_same_as($int_close)
Truth table16%1/6
A or B and C or D
A= $when->is_same_as($int_open)B= $when->is_after($int_open)C= $when->is_before($int_close)D= $when->is_same_as($int_close)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | or ($when->is_after($int_open) and $when->is_before($int_close)) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | or $when->is_same_as($int_close)) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | return { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | 3 | open => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | opens => $int_close->epoch - $when->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | opened => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | closes => $close->epoch - $when->epoch, # we want to know seconds to official close | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | closed => $when->epoch - $int_open->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | ▶ | } elsif ($when->is_after($int_close) and $when->is_before($next_open)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$when->is_after($int_close) and $when->is_before($next_open)
MC/DC0%0/2
$when->is_after($int_close) and $when->is_before($next_open)
$when->is_after($int_close)
$when->is_before($next_open)
Truth table0%0/3
A and B
A= $when->is_after($int_close)B= $when->is_before($next_open)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | return { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | 0 | open => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | opens => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | opened => $when->epoch - $int_close->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | closes => $next_open->epoch - $when->epoch, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | closed => undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | 0 | return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | ## PRIVATE method _seconds_of_trading_between_epochs_days_boundary | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | # there is a strict assumption, that start and end epoch are day boundaries | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | 2 | my %cached_seconds_for_interval; # key ${epoch1}-${epoch2}, value: seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | sub _seconds_of_trading_between_epochs_days_boundary { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | 23 | ▶ | my ($self, $exchange, $start_epoch, $end_epoch) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_seconds_of_trading_between_epochs_days_boundary: called CC=3 SCAR=11.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | 23 | my $cache_key = join('-', $exchange->symbol, $start_epoch, $end_epoch); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | 23 | ▶ | my $result = $cached_seconds_for_interval{$cache_key} //= do { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$cached_seconds_for_interval{$cache_key} //= do {
MC/DC50%1/2
$cached_seconds_for_interval{$cache_key} //= do {
my $head = $self->_computed_trading_seconds($exchange, $start_epoch, $start_epoch + 86399);
if ($end_epoch - $start_epoch > $full_day - 1) {
my $tail = $self->_seconds_of_trading_between_epochs_days_boundary($exchange, $start_epoch + $full_day, $end_epoch);
$head + $tail;
}
}
$cached_seconds_for_interval{$cache_key}
do {
my $head = $self->_computed_trading_seconds($exchange, $start_epoch, $start_epoch + 86399);
if ($end_epoch - $start_epoch > $full_day - 1) {
my $tail = $self->_seconds_of_trading_between_epochs_days_boundary($exchange, $start_epoch + $full_day, $end_epoch);
$head + $tail;
}
}
Truth table66%2/3
A //= B
A= $cached_seconds_for_interval{$cache_key}B= do {
my $head = $self->_computed_trading_seconds($exchange, $start_epoch, $start_epoch + 86399);
if ($end_epoch - $start_epoch > $full_day - 1) {
my $tail = $self->_seconds_of_trading_between_epochs_days_boundary($exchange, $start_epoch + $full_day, $end_epoch);
$head + $tail;
}
}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | 22 | my $head = $self->_computed_trading_seconds($exchange, $start_epoch, $start_epoch + 86399); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | 22 | ▶ | if ($end_epoch - $start_epoch > $full_day - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | 16 | my $tail = $self->_seconds_of_trading_between_epochs_days_boundary($exchange, $start_epoch + $full_day, $end_epoch); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | 16 | $head + $tail; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | 23 | return $result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | ## PRIVATE method _computed_trading_seconds | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | # This one ACTUALLY does the heavy lifting of determining the number of trading seconds in an intraday period. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | sub _computed_trading_seconds { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | 49 | ▶ | my ($self, $exchange, $start, $end) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_computed_trading_seconds: called CC=6 SCAR=18.2
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | 49 | my $total_trading_time = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | 49 | my $when = Date::Utility->new($start); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | 49 | ▶ | if ($self->trades_on($exchange, $when)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | # Do the full computation. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | 42 | my $opening_epoch = $self->opening_on($exchange, $when)->epoch; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | 42 | my $closing_epoch = $self->closing_on($exchange, $when)->epoch; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | # Total trading time left in interval. This is always between 0 to $period_secs_basis. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | # This will automatically take care of early close because market close will just be the early close time. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | 42 | my $total_trading_time_including_lunchbreaks = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | max(min($closing_epoch, $end), $opening_epoch) - min(max($opening_epoch, $start), $closing_epoch); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | 42 | my $total_lunch_break_time = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | # Now take care of lunch breaks. But handle early close properly. It could be that | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | # the early close already wipes out the need to handle lunch breaks. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | # Handle early close. For example on 24 Dec 2009, HKSE opens at 2:00, and stops | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | # for lunch at 4:30 and never reopens. In that case the value of $self->closing_on($thisday) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | # is 4:30, and lunch time between 4:30 to 6:00 is no longer relevant. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | 42 | ▶ | if (my $breaks = $self->trading_breaks($exchange, $when)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | 42 | for my $break_interval (@{$breaks}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | 42 | my $interval_open = $break_interval->[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | 42 | my $interval_close = $break_interval->[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | 42 | my $close_am = min($interval_open->epoch, $closing_epoch); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | 42 | my $open_pm = min($interval_close->epoch, $closing_epoch); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | 42 | $total_lunch_break_time = max(min($open_pm, $end), $close_am) - min(max($close_am, $start), $open_pm); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | 42 | ▶ | if ($total_lunch_break_time < 0) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | 0 | die 'Total lunch break time between ' . $start . '] and [' . $end . '] for exchange[' . $self->exchange->symbol . '] is negative'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | 42 | $total_trading_time = $total_trading_time_including_lunchbreaks - $total_lunch_break_time; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | 42 | ▶ | if ($total_trading_time < 0) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | 0 | croak 'Total trading time (minus lunch) between ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | . $start | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | . '] and [' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | . $end | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | . '] for exchange[' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | . $self->exchange->symbol | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | . '] is negative.'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | 49 | return $total_trading_time; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | ## PRIVATE static methods | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | # Many of these functions don't change their results if asked for the | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | # same dates many times. Let's exploit that for time over space | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | # This actually comes up in our pricing where we have to do many interpolations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | # over the same ranges on different values. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | # This attaches to the static method on the class for the lifetime of this instance. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | # Since we only want the cache for our specific symbol, we need to include an identifier. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | sub _normalize_on_just_dates { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | 6 | ▶ | my ($self, @dates) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_on_just_dates: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | 6 | return join '|', (map { Date::Utility->new($_)->days_since_epoch } @dates); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1049 | 2 | ▶ | no Moose; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1050 | 2 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | 2 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||