| 1 | package Finance::Exchange; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 3 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 3 | ▶ | use Time::Duration::Concise::Localize; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use Clone qw(clone); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use File::ShareDir; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use YAML::XS qw(LoadFile); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 3 | our $VERSION = '0.01'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | =head1 NAME | ||||||||||||||
| 13 | |||||||||||||||
| 14 | Finance::Exchange - represents a financial stock exchange object. | ||||||||||||||
| 15 | |||||||||||||||
| 16 | =head1 VERSION | ||||||||||||||
| 17 | |||||||||||||||
| 18 | version 0.01 | ||||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||
| 21 | |||||||||||||||
| 22 | use Finance::Exchange; | ||||||||||||||
| 23 | |||||||||||||||
| 24 | my $exchange_symbol = 'LSE'; # London Stocks Exchange | ||||||||||||||
| 25 | my $exchange = Finance::Exchange->create_exchange($exchange_symbol); | ||||||||||||||
| 26 | |||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||
| 28 | |||||||||||||||
| 29 | This is a generic representation of a financial stock exchange. | ||||||||||||||
| 30 | |||||||||||||||
| 31 | =head2 USAGE | ||||||||||||||
| 32 | |||||||||||||||
| 33 | my $exchange = Finance::Exchange->create_exchange('LSE'); | ||||||||||||||
| 34 | is $exchange->symbol, 'LSE'; | ||||||||||||||
| 35 | is $exchange->display_name, 'London Stock Exchange'; | ||||||||||||||
| 36 | is $exchange->trading_days, 'weekdays'; | ||||||||||||||
| 37 | is $exchange->trading_timezone, 'Europe/London'; | ||||||||||||||
| 38 | # The list of days starts on Sunday and is a set of flags indicating whether | ||||||||||||||
| 39 | # we trade on that day or not | ||||||||||||||
| 40 | is $exchange->trading_days_list, [ 0, 1, 1, 1, 1, 1, 0 ]; | ||||||||||||||
| 41 | is $exchange->market_times, { ... }; | ||||||||||||||
| 42 | is $exchange->delay_amount, 15, 'LSE minimum delay is 15 minutes'; | ||||||||||||||
| 43 | is $exchange->currency, 'GBP', 'LSE is traded in pound sterling'; | ||||||||||||||
| 44 | is $exchange->trading_date_can_differ, 0, 'only applies to AU/NZ'; | ||||||||||||||
| 45 | ... | ||||||||||||||
| 46 | |||||||||||||||
| 47 | =cut | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 3 | my ($cached_objects, $exchange_config, $trading_day_aliases); | |||||||||||||
| 50 | |||||||||||||||
| 51 | BEGIN { | ||||||||||||||
| 52 | 3 | ▶ | $exchange_config = YAML::XS::LoadFile(File::ShareDir::dist_file('Finance-Exchange', 'exchange.yml')); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 53 | 3 | $trading_day_aliases = YAML::XS::LoadFile(File::ShareDir::dist_file('Finance-Exchange', 'exchanges_trading_days_aliases.yml')); | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | =head2 create_exchange | ||||||||||||||
| 57 | |||||||||||||||
| 58 | Exchange object constructor. | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =cut | ||||||||||||||
| 61 | |||||||||||||||
| 62 | sub create_exchange { | ||||||||||||||
| 63 | 223 | ▶ | my ($class, $symbol) = @_; | ||||||||||||
|
Subroutine
create_exchange: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 64 | |||||||||||||||
| 65 | 223 | ▶ | die 'symbol is required' unless $symbol; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | |||||||||||||||
| 67 | 223 | ▶ | if (my $cached = $cached_objects->{$symbol}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 148 | return $cached; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 75 | my $params_ref = clone($exchange_config->{$symbol}); | |||||||||||||
| 72 | 75 | ▶ | die 'Config for exchange[' . $symbol . '] not specified in exchange.yml' unless $params_ref; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | |||||||||||||||
| 74 | 74 | $params_ref->{_market_times} = delete $params_ref->{market_times}; | |||||||||||||
| 75 | 74 | my $new = $class->new($params_ref); | |||||||||||||
| 76 | 74 | $cached_objects->{$symbol} = $new; | |||||||||||||
| 77 | |||||||||||||||
| 78 | 74 | return $new; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =head1 ATTRIBUTES | ||||||||||||||
| 82 | |||||||||||||||
| 83 | =head2 display_name | ||||||||||||||
| 84 | |||||||||||||||
| 85 | Exchange display name, e.g. London Stock Exchange. | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =head2 symbol | ||||||||||||||
| 88 | |||||||||||||||
| 89 | Exchange symbol, e.g. LSE to represent London Stocks Exchange. | ||||||||||||||
| 90 | |||||||||||||||
| 91 | =head2 trading_days | ||||||||||||||
| 92 | |||||||||||||||
| 93 | An exchange's trading day category. | ||||||||||||||
| 94 | |||||||||||||||
| 95 | For example, an exchange that trades from Monday to Friday is given a trading days category of 'weekdays'. | ||||||||||||||
| 96 | |||||||||||||||
| 97 | The list is enumerated in the exchanges_trading_days_aliases.yml file. | ||||||||||||||
| 98 | |||||||||||||||
| 99 | =head2 trading_timezone | ||||||||||||||
| 100 | |||||||||||||||
| 101 | The timezone in which the exchange conducts business. | ||||||||||||||
| 102 | |||||||||||||||
| 103 | This should be a string which will allow the standard DateTime module to find the proper information. | ||||||||||||||
| 104 | |||||||||||||||
| 105 | =cut | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 3 | has [ | |||||||||||||
| 108 | qw( display_name symbol trading_days trading_timezone | ||||||||||||||
| 109 | ) | ||||||||||||||
| 110 | ] => ( | ||||||||||||||
| 111 | is => 'ro', | ||||||||||||||
| 112 | required => 1, | ||||||||||||||
| 113 | ); | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 3 | has _market_times => ( | |||||||||||||
| 116 | is => 'ro', | ||||||||||||||
| 117 | required => 1, | ||||||||||||||
| 118 | ); | ||||||||||||||
| 119 | |||||||||||||||
| 120 | =head2 trading_days_list | ||||||||||||||
| 121 | |||||||||||||||
| 122 | List the trading day index which is defined in exchanges_trading_days_aliases.yml. | ||||||||||||||
| 123 | |||||||||||||||
| 124 | An example of a 'weekdays' trading days list is as follow: | ||||||||||||||
| 125 | - 0 # Sun | ||||||||||||||
| 126 | - 1 # Mon | ||||||||||||||
| 127 | - 1 # Tues | ||||||||||||||
| 128 | - 1 # Wed | ||||||||||||||
| 129 | - 1 # Thurs | ||||||||||||||
| 130 | - 1 # Fri | ||||||||||||||
| 131 | - 0 # Sat | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =cut | ||||||||||||||
| 134 | |||||||||||||||
| 135 | 3 | has trading_days_list => ( | |||||||||||||
| 136 | is => 'ro', | ||||||||||||||
| 137 | lazy_build => 1, | ||||||||||||||
| 138 | ); | ||||||||||||||
| 139 | |||||||||||||||
| 140 | sub _build_trading_days_list { | ||||||||||||||
| 141 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_trading_days_list: called CC=1 SCAR=0.0
| |||||||||||||||
| 142 | |||||||||||||||
| 143 | 1 | return $trading_day_aliases->{$self->trading_days}; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =head2 market_times | ||||||||||||||
| 147 | |||||||||||||||
| 148 | A hash reference of human-readable exchange trading times in Greenwich Mean Time (GMT). | ||||||||||||||
| 149 | |||||||||||||||
| 150 | The trading times are broken into three categories: | ||||||||||||||
| 151 | |||||||||||||||
| 152 | 1. standard - which represents the trading times in non Day Light Saving (DST) period. | ||||||||||||||
| 153 | 2. dst - which represents the trading time in DST period. | ||||||||||||||
| 154 | 3. partial_trading - which represents the trading breaks (e.g. lunch break) in a trading day | ||||||||||||||
| 155 | |||||||||||||||
| 156 | =cut | ||||||||||||||
| 157 | |||||||||||||||
| 158 | 3 | has market_times => ( | |||||||||||||
| 159 | is => 'ro', | ||||||||||||||
| 160 | lazy_build => 1, | ||||||||||||||
| 161 | ); | ||||||||||||||
| 162 | |||||||||||||||
| 163 | sub _build_market_times { | ||||||||||||||
| 164 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_market_times: called CC=6 SCAR=18.0
| |||||||||||||||
| 165 | |||||||||||||||
| 166 | 3 | my $mt = $self->_market_times; | |||||||||||||
| 167 | 3 | my $market_times; | |||||||||||||
| 168 | |||||||||||||||
| 169 | 3 | foreach my $key (keys %$mt) { | |||||||||||||
| 170 | 9 | foreach my $trading_segment (keys %{$mt->{$key}}) { | |||||||||||||
| 171 | 28 | ▶ | if ($trading_segment eq 'day_of_week_extended_trading_breaks') { next; } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 172 | elsif ($trading_segment ne 'trading_breaks') { | ||||||||||||||
| 173 | $market_times->{$key}->{$trading_segment} = Time::Duration::Concise::Localize->new( | ||||||||||||||
| 174 | 26 | interval => $mt->{$key}->{$trading_segment}, | |||||||||||||
| 175 | ); | ||||||||||||||
| 176 | } else { | ||||||||||||||
| 177 | 2 | my $break_intervals = $mt->{$key}->{$trading_segment}; | |||||||||||||
| 178 | 2 | my @converted; | |||||||||||||
| 179 | 2 | foreach my $int (@$break_intervals) { | |||||||||||||
| 180 | 2 | my $open_int = Time::Duration::Concise::Localize->new( | |||||||||||||
| 181 | interval => $int->[0], | ||||||||||||||
| 182 | ); | ||||||||||||||
| 183 | 2 | my $close_int = Time::Duration::Concise::Localize->new( | |||||||||||||
| 184 | interval => $int->[1], | ||||||||||||||
| 185 | ); | ||||||||||||||
| 186 | 2 | push @converted, [$open_int, $close_int]; | |||||||||||||
| 187 | } | ||||||||||||||
| 188 | 2 | $market_times->{$key}->{$trading_segment} = \@converted; | |||||||||||||
| 189 | } | ||||||||||||||
| 190 | } | ||||||||||||||
| 191 | } | ||||||||||||||
| 192 | |||||||||||||||
| 193 | 3 | return $market_times; | |||||||||||||
| 194 | } | ||||||||||||||
| 195 | |||||||||||||||
| 196 | =head2 delay_amount | ||||||||||||||
| 197 | |||||||||||||||
| 198 | The acceptable delay amount of feed on this exchange, in minutes. Default is 60 minutes. | ||||||||||||||
| 199 | |||||||||||||||
| 200 | =cut | ||||||||||||||
| 201 | |||||||||||||||
| 202 | 3 | has delay_amount => ( | |||||||||||||
| 203 | is => 'ro', | ||||||||||||||
| 204 | isa => 'Num', | ||||||||||||||
| 205 | default => 60, | ||||||||||||||
| 206 | ); | ||||||||||||||
| 207 | |||||||||||||||
| 208 | =head2 currency | ||||||||||||||
| 209 | |||||||||||||||
| 210 | The currency in which the exchange is traded in. | ||||||||||||||
| 211 | |||||||||||||||
| 212 | =cut | ||||||||||||||
| 213 | |||||||||||||||
| 214 | 3 | has currency => ( | |||||||||||||
| 215 | is => 'ro', | ||||||||||||||
| 216 | ); | ||||||||||||||
| 217 | |||||||||||||||
| 218 | =head2 trading_date_can_differ | ||||||||||||||
| 219 | |||||||||||||||
| 220 | A boolean flag to indicate if an exchange would open on the previous GMT date due to DST. | ||||||||||||||
| 221 | |||||||||||||||
| 222 | =cut | ||||||||||||||
| 223 | |||||||||||||||
| 224 | 3 | has trading_date_can_differ => ( | |||||||||||||
| 225 | is => 'ro', | ||||||||||||||
| 226 | lazy_build => 1, | ||||||||||||||
| 227 | ); | ||||||||||||||
| 228 | |||||||||||||||
| 229 | sub _build_trading_date_can_differ { | ||||||||||||||
| 230 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_trading_date_can_differ: called CC=2 SCAR=7.0
| |||||||||||||||
| 231 | |||||||||||||||
| 232 | my @premidnight_opens = | ||||||||||||||
| 233 | 2 | grep { $_->seconds < 0 } | |||||||||||||
| 234 | 2 | map { $self->market_times->{$_}->{daily_open} } | |||||||||||||
| 235 | 3 | grep { exists $self->market_times->{$_}->{daily_open} } | |||||||||||||
| 236 | 1 | keys %{$self->market_times}; | |||||||||||||
| 237 | 1 | ▶ | return (scalar @premidnight_opens) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 238 | } | ||||||||||||||
| 239 | |||||||||||||||
| 240 | 3 | ▶ | no Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 241 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 242 | |||||||||||||||
| 243 | 3 | 1; | |||||||||||||