TAIFEX.pm

filter: Statementstmt 100% 24 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 100% 32 / 32 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« TAIFEX/Contract.pm ↑ Summary
1 package Finance::TW::TAIFEX;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use List::MoreUtils qw(firstidx);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Any::Moose 'X::Types::DateTime';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 require MouseX::NativeTraits if Any::Moose->mouse_is_preferred;
11 use HTTP::Request::Common qw(POST);
12 use LWP::Simple 'getstore';
13 use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
14  
15 use Finance::TW::TAIFEX::Product;
16 use Finance::TW::TAIFEX::Contract;
17  
18 use 5.008_001;
19 our $VERSION = '0.39';
20  
21 has context_date => ( is => "rw", isa => "DateTime",
22 default => sub { DateTime->now(time_zone => 'Asia/Taipei') },
23 coerce => 1);
24  
25 has calendar => (is => "ro", isa => "HashRef", default => sub { {} });
26  
27 has products => (
28 traits => ['Hash'],
29 is => "ro",
30 isa => "HashRef[Finance::TW::TAIFEX::Product]",
31 handles => {
32 has_product => 'exists',
33 product => 'get',
34 },
35 lazy_build => 1,
36 );
37  
38  
39 sub _build_products {
40 my $self = shift;
41 return {
42 (map { $_ => Finance::TW::TAIFEX::Product->new_with_traits(
43 traits => ['Settlement::ThirdWednesday'],
44 exchange => $self,
45 name => $_,
46 ) } qw(TX MTX TE TF T5F MSF CPF XIF GTF
47 TXO TEO TFO MSO XIO GTO)),
48 };
49  
50 # (map { $_ => Finance::TW::TAIFEX::Product->new_with_traits(
51 # traits => ['Settlement::ThirdToLastDayOfMonth'],
52 # exchange => $self,
53 # name => $_,
54 # ) } qw(TGF TGO)),
55 #
56 # (GBF => Finance::TW::TAIFEX::Product->new_with_traits(
57 # traits => ['Settlement::SecondWednesday'],
58 # exchange => $self,
59 # name => 'GBF',
60 # )),
61  
62 }
63  
64 =head1 NAME
65  
66 Finance::TW::TAIFEX - Helper functions for Taiwan Futures Exchange
67  
68 =head1 SYNOPSIS
69  
70 use Finance::TW::TAIFEX;
71  
72 my $taifex = Finance::TW::TAIFEX->new();
73  
74 $taifex->is_trading_day(); # is today a trading day?
75  
76 my $date = DateTime->now;
77 $taifex->daily_futures_uri($date);
78 $taifex->daily_options_uri($date);
79  
80 $taifex->contract('TX', '201001')->settlement_date;
81 $taifex->product('TX')->near_term;
82 $taifex->product('TX')->next_term;
83  
84 =head1 DESCRIPTION
85  
86 Finance::TW::TAIFEX provides useful helper functions for the Taiwan
87 Future Exchanges.
88  
89 =head1 METHODS
90  
91 =head2 product NAME
92  
93 Returns the L<Finance::TW::TAIFEX::Product> object represented by NAME.
94  
95 Currently supported product names:
96  
97 =over
98  
99 =item Futures
100  
101 TX MTX TE TF T5F MSF CPF XIF GTF
102  
103 =item Options
104  
105 TXO TEO TFO MSO XIO GTO
106  
107 =back
108  
109 =head2 has_product NAME
110  
111 Checks if the given product exists.
112  
113 =cut
114  
115 my $Strp = DateTime::Format::Strptime->new( pattern => '%F', time_zone => 'Asia/Taipei');
116  
117 sub BUILDARGS {
118 my $class = shift;
119 return { @_ } unless $#_ == 0;
120 return { } unless $_[0];
121 return { context_date => ref $_[0] ? $_[0] : $Strp->parse_datetime($_[0]) }
122 }
123  
124 =head2 contract NAME YEAR MONTH
125  
126 Returns the L<Finance::TW::TAIFEX::Contract> of the given product expires on YEAR/MONTH.
127  
128 =cut
129  
130 sub contract {
131 my ($self, $name, $year, $month) = @_;
132 unless ($month) {
133 my $spec = $year;
134 ($year, $month) = $spec =~ m/^(\d{4})(\d{2})$/
135 or die "$spec doesn't look like a contract month";
136 }
137  
138 die "unknown product $name"
139 unless $self->has_product($name);
140  
141 return Finance::TW::TAIFEX::Contract->new( product => $self->product($name),
142 year => $year,
143 month => $month );
144 }
145  
146 sub _read_cal {
147 my ($self, $file) = @_;
148 open my $fh, '<', $file or die "$file: $!" ;
149 return [map { chomp; $_ } <$fh>];
150 }
151  
152 =head2 calendar_for YEAR
153  
154 Returns the trading calendar for YEAR.
155  
156 =cut
157  
158 sub calendar_for {
159 my ($self, $year) = @_;
160 $year ||= $self->context_date->year;
161  
162 return $self->calendar->{$year}
163 if $self->calendar->{$year};
164  
165 my $dist_dir = File::Spec->rel2abs("../../../share", File::Basename::dirname($INC{"Finance/TW/TAIFEX.pm"}));
166 $dist_dir = try { dist_dir('Finance-TW-TAIFEX') || 'share' } unless -e $dist_dir;
167 $self->calendar->{$year} = $self->_read_cal("$dist_dir/calendar/$year.txt");
168 }
169  
170 =head2 is_trading_day [DATE]
171  
172 Checks if the given DATE is a known trading day. Default DATE is the date in the current context.
173  
174 =cut
175  
176 sub is_trading_day {
177 my ($self, $date) = @_;
178 $date ||= $self->context_date;
179  
180 $self->_nth_trading_day($self->calendar_for($date->year), $date->ymd) != -1;
181 }
182  
183 =head2 next_trading_day [DATE]
184  
185 Returns the next known trading day in string after the given DATE.
186  
187 =cut
188  
189 sub next_trading_day {
190 my ($self, $date) = @_;
191 $date ||= $self->context_date;
192  
193 my $cal = $self->calendar_for($date->year);
194 my $d = $date->ymd;
195 my $nth = firstidx { $_ gt $d } @{$cal};
196  
197 if ($nth < 0) {
198 return $self->calendar_for($date->year + 1)->[0];
199 }
200  
201 return $cal->[$nth];
202 }
203  
204 =head2 previous_trading_day [DATE]
205  
206 Returns the previous known trading day in string after the given DATE.
207  
208 =cut
209  
210 sub previous_trading_day {
211 my ($self, $date, $offset) = @_;
212 $date ||= $self->context_date;
213 $offset ||= -1;
214  
215 my $cal = $self->calendar_for($date->year);
216 my $nth = $self->_nth_trading_day($cal, $date->ymd);
217 die "$date not a known trading day"
218 if $nth < 0;
219  
220 if ($nth + $offset < 0) {
221 return $self->calendar_for($date->year - 1)->[-1];
222 }
223  
224 return $cal->[$nth + $offset];
225 }
226  
227 sub _nth_trading_day {
228 my ($self, $cal, $date) = @_;
229 firstidx { $_ eq $date } @{$cal}
230 }
231  
232 =head2 daily_futures_uri DATE
233  
234 Returns the URI of the official TAIFEX futures trading records for DATE.
235  
236 =cut
237  
238 sub daily_futures_uri {
239 my ($self, $date) = @_;
240 $date ||= $self->context_date;
241 return "http://www.taifex.com.tw/DailyDownload/Daily_@{[ $date->ymd('_') ]}.zip";
242 }
243  
244 =head2 interday_futures_request($product, [$DATE])
245  
246 Returns a HTTP::Request object that fetches futures monthly interday
247 csv file for $product of $DATE.
248  
249 =cut
250  
251 sub interday_futures_request {
252 my ($self, $product, $date) = @_;
253 $date ||= $self->context_date;
254 my $from = $date->clone->truncate( to => 'month' );
255 my $to = $from->clone->add( months => 1 )->subtract( days => 1 );
256 return POST 'http://www.taifex.com.tw/chinese/3/3_1_2dl.asp',
257 [ goday => '',
258 DATA_DATE => $from->ymd('/'),
259 DATA_DATE1 => $to->ymd('/'),
260 DATA_DATE_Y => $from->year,
261 DATA_DATE_M => $from->month,
262 DATA_DATE_D => $from->day,
263 DATA_DATE_Y1 => $to->year,
264 DATA_DATE_M1 => $to->month,
265 DATA_DATE_D1 => $to->day,
266 commodity_id2t => '',
267 COMMODITY_ID => $product ];
268 }
269  
270 =head2 interday_options_request($product, [$DATE])
271  
272 Returns a HTTP::Request object that fetches options monthly interday
273 csv file for $product of $DATE.
274  
275 =cut
276  
277 sub interday_options_request {
278 my ($self, $product, $date) = @_;
279 $date ||= $self->context_date;
280 my $from = $date->clone->truncate( to => 'month' );
281 my $to = $from->clone->add( months => 1 )->subtract( days => 1 );
282 return POST 'http://www.taifex.com.tw/chinese/3/3_2_3_b.asp',
283 [ goday => '',
284 DATA_DATE => $from->ymd('/'),
285 DATA_DATE1 => $to->ymd('/'),
286 DATA_DATE_Y => $from->year,
287 DATA_DATE_M => $from->month,
288 DATA_DATE_D => $from->day,
289 DATA_DATE_Y1 => $to->year,
290 DATA_DATE_M1 => $to->month,
291 DATA_DATE_D1 => $to->day,
292 COMMODITY_ID => $product.'%' ];
293 }
294  
295  
296 =head2 daily_options_uri DATE
297  
298 Returns the URI of the official TAIFEX options trading records for DATE.
299  
300 =cut
301  
302 sub daily_options_uri {
303 my ($self, $date) = @_;
304 $date ||= $self->context_date;
305 return "http://www.taifex.com.tw/OptionsDailyDownload/OptionsDaily_@{[ $date->ymd('_') ]}.zip";
306 }
307  
308 =head2 ensure_rpt DIR, TYPE, PREFIX
309  
310 =cut
311  
312 sub ensure_rpt {
313 my ($self, $rpt_dir, $type, $prefix) = @_;
314 my $date = $self->context_date;
315 my $rpt_f = "$rpt_dir/".$date->ymd('-').".rpt";
316  
317 unless (-s $rpt_f) {
318 my $rpt = $prefix.$date->ymd('_' ).".rpt";
319 my $f = $self->can('daily_'.$type.'_uri') or die "unknown type: $type";
320 my $url = $self->$f();
321 my $tmp = "/tmp/taifex-$type-".$date->ymd('-').".zip";
322 unless (-s $tmp) {
323 my $rc = getstore($url => $tmp);
324  
325 die("failed to fetch $url: $rc")
326 if HTTP::Status::is_error($rc);
327 }
328 my $zip = Archive::Zip->new();
329 unless ( $zip->read( $tmp ) == AZ_OK ) {
330 unlink( $tmp );
331 die("Unable to read zip file $tmp");
332 }
333 unless ( $zip->extractMember($rpt, $rpt_f) == AZ_OK ) {
334 die("Unable to extract $rpt from $tmp");
335 }
336 }
337 }
338  
339 =head1 CAVEATS
340  
341 The URI returned by C<daily_futures_uri> and C<daily_options_uri> are only valid for the last 30 trading days per the policy of TAIFEX.
342  
343 =head1 AUTHOR
344  
345 Chia-liang Kao E<lt>clkao@clkao.orgE<gt>
346  
347 =head1 LICENSE
348  
349 This library is free software; you can redistribute it and/or modify
350 it under the same terms as Perl itself.
351  
352 =head1 SEE ALSO
353  
354 L<http://www.taifex.com.tw/>
355  
356 =cut
357  
358 __PACKAGE__->meta->make_immutable;
359 no Any::Moose;
360 1;
361  
« TAIFEX/Contract.pm ↑ Summary