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