Finance-TW-TAIFEX
    
    
  
  
  
view release on metacpan or search on metacpan
Revision history for Perl extension Finance::TW::TAIFEX
0.39 Sun Dec 30 15:53:15 CST 2012
        - 2013 calendar.
0.38 Fri Aug  3 11:53:01 CST 2012
        - update calender for 2012 typhoon.
0.37 Thu Feb  2 14:51:28 CST 2012
        - fix two missing Saturdays in 2012 calendar.
0.36 Wed Jan 18 19:02:51 CST 2012
        - helper scripts for eod data maintenance.
0.35 Tue Dec 27 18:20:00 CST 2011
        - calendar for 2012.
        - tx helper scripts.
        - make next/previous trading day api work across years.
0.34 Sun May  1 17:33:59 CST 2011
        - correct 2011 calendar.
0.33 Sun Feb  6 21:25:37 CST 2011
        - Better any-moose deps handling.
        - correct 2011 lunar new year calendar.
0.32 Sat Dec 25 20:10:39 CST 2010
        - Use Any::Moose.
        - Calendar for 2011.
        - Calendar for 2000-2008.
0.31 Mon Oct 11 09:28:46 CST 2010
        - Use exchange timezone by default.
        - Fix dependencies.
0.30 Sat Oct  9 17:03:03 CST 2010
        - Use moose native attribute traits.
        - Calendar updates.
0.29  Sat Jan 30 12:03:52 CST 2010
        - Correct calendar for Feb 2010.
0.28  Fri Jan  1 17:47:05 2010
        - original version
inc/Test/Exception.pm
lib/Finance/TW/TAIFEX.pm
lib/Finance/TW/TAIFEX/Contract.pm
lib/Finance/TW/TAIFEX/Product.pm
lib/Finance/TW/TAIFEX/Settlement/SecondWednesday.pm
lib/Finance/TW/TAIFEX/Settlement/ThirdWednesday.pm
Makefile.PL
MANIFEST			This list of files
META.yml
README
share/calendar/2000.txt
share/calendar/2001.txt
share/calendar/2002.txt
share/calendar/2003.txt
share/calendar/2004.txt
share/calendar/2005.txt
share/calendar/2006.txt
share/calendar/2007.txt
share/calendar/2008.txt
share/calendar/2009.txt
share/calendar/2010.txt
share/calendar/2011.txt
share/calendar/2012.txt
share/calendar/2013.txt
t/00_compile.t
t/01-basic.t
t/02-tx.t
t/03-year.t
xt/perlcritic.t
xt/pod.t
xt/podspell.t
xt/synopsis.t
    Options
        TXO TEO TFO MSO XIO GTO
  has_product NAME
    Checks if the given product exists.
  contract NAME YEAR MONTH
    Returns the Finance::TW::TAIFEX::Contract of the given product expires
    on YEAR/MONTH.
  calendar_for YEAR
    Returns the trading calendar for YEAR.
  is_trading_day [DATE]
    Checks if the given DATE is a known trading day. Default DATE is the
    date in the current context.
  next_trading_day [DATE]
    Returns the next known trading day in string after the given DATE.
  previous_trading_day [DATE]
    Returns the previous known trading day in string after the given DATE.
lib/Finance/TW/TAIFEX.pm view on Meta::CPAN
use Finance::TW::TAIFEX::Product;
use Finance::TW::TAIFEX::Contract;
use 5.008_001;
our $VERSION = '0.39';
has context_date => ( is => "rw", isa => "DateTime",
                      default => sub { DateTime->now(time_zone => 'Asia/Taipei') },
                      coerce => 1);
has calendar => (is => "ro", isa => "HashRef", default => sub { {} });
has products => (
    traits => ['Hash'],
    is => "ro",
    isa => "HashRef[Finance::TW::TAIFEX::Product]",
    handles  => {
        has_product => 'exists',
        product     => 'get',
    },
    lazy_build => 1,
lib/Finance/TW/TAIFEX.pm view on Meta::CPAN
                                               year => $year,
                                               month => $month );
}
sub _read_cal {
    my ($self, $file) = @_;
    open my $fh, '<', $file or die "$file: $!" ;
    return [map { chomp; $_ } <$fh>];
}
=head2 calendar_for YEAR
Returns the trading calendar for YEAR.
=cut
sub calendar_for {
    my ($self, $year) = @_;
    $year ||= $self->context_date->year;
    return $self->calendar->{$year}
        if $self->calendar->{$year};
    my $dist_dir = File::Spec->rel2abs("../../../share", File::Basename::dirname($INC{"Finance/TW/TAIFEX.pm"}));
    $dist_dir = try { dist_dir('Finance-TW-TAIFEX') || 'share' } unless -e $dist_dir;
    $self->calendar->{$year} = $self->_read_cal("$dist_dir/calendar/$year.txt");
}
=head2 is_trading_day [DATE]
Checks if the given DATE is a known trading day.  Default DATE is the date in the current context.
=cut
sub is_trading_day {
    my ($self, $date) = @_;
    $date ||= $self->context_date;
    $self->_nth_trading_day($self->calendar_for($date->year), $date->ymd) != -1;
}
=head2 next_trading_day [DATE]
Returns the next known trading day in string after the given DATE.
=cut
sub next_trading_day {
    my ($self, $date) = @_;
    $date ||= $self->context_date;
    my $cal = $self->calendar_for($date->year);
    my $d = $date->ymd;
    my $nth = firstidx { $_ gt $d } @{$cal};
    if ($nth < 0) {
        return $self->calendar_for($date->year + 1)->[0];
    }
    return $cal->[$nth];
}
=head2 previous_trading_day [DATE]
Returns the previous known trading day in string after the given DATE.
=cut
sub previous_trading_day {
    my ($self, $date, $offset) = @_;
    $date ||= $self->context_date;
    $offset ||= -1;
    my $cal = $self->calendar_for($date->year);
    my $nth = $self->_nth_trading_day($cal, $date->ymd);
    die "$date not a known trading day"
        if $nth < 0;
    if ($nth + $offset < 0) {
        return $self->calendar_for($date->year - 1)->[-1];
    }
    return $cal->[$nth + $offset];
}
sub _nth_trading_day {
    my ($self, $cal, $date) = @_;
    firstidx { $_ eq $date } @{$cal}
}
( run in 0.454 second using v1.01-cache-2.11-cpan-5dc5da66d9d )