| 1 | package Finance::TW::TSEQuote; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use 5.10.1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | our $VERSION = '0.28'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use LWP::Simple (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Encode 'from_to'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use App::Cache; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | sub resolve { | ||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=8 SCAR=30.7
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 14 | 1 | ▶ | $self = bless {}, __PACKAGE__ unless ref($self) eq __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 1 | my $name = shift; | |||||||||||||||||||||||||||
| 16 | 1 | ▶ | $self->{cache} ||= App::Cache->new({ ttl => 7 * 24 * 60 * 60 }); # a week | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"cache"} ||= "App::Cache"->new({"ttl", 604800})
MC/DC0%0/2
$self->{"cache"} ||= "App::Cache"->new({"ttl", 604800})
$self->{"cache"}
"App::Cache"->new({"ttl", 604800})
Truth table33%1/3
A ||= B
A= $self->{"cache"}B= "App::Cache"->new({"ttl", 604800})
| |||||||||||||||||||||||||||||
| 17 | 1 | my $cache = $self->{cache}; | |||||||||||||||||||||||||||
| 18 | 1 | my $key = md5_hex($name); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | unless ($cache->get($key)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 1 | my $url = "http://brk.twse.com.tw:8000/isin/C_public.jsp?strMode=2"; | |||||||||||||||||||||||||||
| 22 | 1 | my $content = $cache->get_url($url); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | from_to($content, 'big5', 'utf-8'); | |||||||||||||||||||||||||||
| 25 | 1 | ▶ | use HTML::TableExtract; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 0 | my $te = HTML::TableExtract->new( | |||||||||||||||||||||||||||
| 27 | headers => [qw(證券代號及名稱 上市日)]); | ||||||||||||||||||||||||||||
| 28 | 0 | $te->parse($content); | |||||||||||||||||||||||||||
| 29 | 0 | foreach my $ts ($te->tables) { | |||||||||||||||||||||||||||
| 30 | 0 | foreach my $row ($ts->rows) { | |||||||||||||||||||||||||||
| 31 | 0 | my ($symbol, $company) | |||||||||||||||||||||||||||
| 32 | = $row->[0] =~ m|(\S+)\s+\xe3\x80\x80(.*?)$|o; | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | next unless $symbol; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | my $board_date = $row->[1]; | |||||||||||||||||||||||||||
| 35 | 0 | $cache->set(md5_hex($company), | |||||||||||||||||||||||||||
| 36 | { id => $symbol, date => $board_date }); | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | my $id = $cache->get($key)->{id}; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | die "can't resolve symbol: $name" unless $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | @{$self}{qw/id/} = ($id); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | return $id; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=16.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 1 | my ($class, $target) = @_; | |||||||||||||||||||||||||||
| 52 | 1 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | $self->resolve($target) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | unless $target =~ /^\d+$/; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $self->{id} ||= $target; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"id"} ||= $target
MC/DC0%0/2
$self->{"id"} ||= $target
$self->{"id"}
$target
Truth table0%0/3
A ||= B
A= $self->{"id"}B= $target
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | return $self; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | no utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | no encoding; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | my $stockno = ref $self ? $self->{id} : shift; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | my $content | |||||||||||||||||||||||||||
| 69 | = LWP::Simple::get("http://mis.twse.com.tw/data/$stockno.csv"); | ||||||||||||||||||||||||||||
| 70 | 0 | from_to($content, 'big5', 'utf-8'); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | my $result; | |||||||||||||||||||||||||||
| 73 | 0 | $content =~ s/["\n\r]//g; | |||||||||||||||||||||||||||
| 74 | 0 | my @info = split /,/, $content; | |||||||||||||||||||||||||||
| 75 | 0 | my $cmap = [ undef, 'UpDown', 'time', 'UpPrice', | |||||||||||||||||||||||||||
| 76 | 'DownPrice', 'OpenPrice', 'HighPrice', 'LowPrice', | ||||||||||||||||||||||||||||
| 77 | 'MatchPrice', 'MatchQty', 'DQty' ]; | ||||||||||||||||||||||||||||
| 78 | 0 | $result->{ $cmap->[$_] } = $info[$_] foreach (0 .. 10); | |||||||||||||||||||||||||||
| 79 | 0 | $result->{name} = $info[32]; | |||||||||||||||||||||||||||
| 80 | 0 | $result->{name} =~ s/\s//g; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | $self->{name} ||= $result->{name} if ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"name"} ||= $result->{"name"}
MC/DC0%0/2
$self->{"name"} ||= $result->{"name"}
$self->{"name"}
$result->{"name"}
Truth table0%0/3
A ||= B
A= $self->{"name"}B= $result->{"name"}
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($result->{MatchPrice} == $result->{UpPrice}) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 84 | 0 | $result->{UpDownMark} = '♁'; | |||||||||||||||||||||||||||
| 85 | } elsif ($result->{MatchPrice} == $result->{DownPrice}) { | ||||||||||||||||||||||||||||
| 86 | 0 | $result->{UpDownMark} = '?'; | |||||||||||||||||||||||||||
| 87 | } elsif ($result->{UpDown} > 0) { | ||||||||||||||||||||||||||||
| 88 | 0 | $result->{UpDownMark} = '+'; | |||||||||||||||||||||||||||
| 89 | } elsif ($result->{UpDown} < 0) { | ||||||||||||||||||||||||||||
| 90 | 0 | $result->{UpDownMark} = '-'; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | $result->{Bid}{Buy}[$_]{ $info[ 11 + $_ * 2 ] } = $info[ 12 + $_ * 2 ] | ||||||||||||||||||||||||||||
| 94 | 0 | foreach (0 .. 4); | |||||||||||||||||||||||||||
| 95 | $result->{Bid}{Sell}[$_]{ $info[ 21 + $_ * 2 ] } = $info[ 22 + $_ * 2 ] | ||||||||||||||||||||||||||||
| 96 | 0 | foreach (0 .. 4); | |||||||||||||||||||||||||||
| 97 | 0 | $result->{BuyPrice} = $info[11]; | |||||||||||||||||||||||||||
| 98 | 0 | $result->{SellPrice} = $info[21]; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | $self->{quote} = $result if ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | return $result; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub fetchMarketFile { | ||||||||||||||||||||||||||
|
Subroutine
fetchMarketFile: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 107 | 0 | my ($stock, $year, $month) = @_; | |||||||||||||||||||||||||||
| 108 | 0 | my @fields = (); | |||||||||||||||||||||||||||
| 109 | 0 | my ($i, $url, $file, $arg, $outfile); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $month = "0" . $month if $month < 10; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | $url | |||||||||||||||||||||||||||
| 113 | = "http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/genpage/Report" | ||||||||||||||||||||||||||||
| 114 | . $year | ||||||||||||||||||||||||||||
| 115 | . $month . "/"; | ||||||||||||||||||||||||||||
| 116 | 0 | $file = $year . $month . "_F3_1_8_" . $stock . ".php?STK_NO=" . $stock; | |||||||||||||||||||||||||||
| 117 | 0 | $arg = "&myear=" . $year . "&mmon=" . $month; | |||||||||||||||||||||||||||
| 118 | 0 | my $content = LWP::Simple::get("$url$file$arg"); | |||||||||||||||||||||||||||
| 119 | 0 | my $result; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ($content) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($content =~ /<tr bgcolor='#F7F0E8'>(.+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $content = $1; | |||||||||||||||||||||||||||
| 124 | 0 | $content =~ s/<table(.)*?>/ /g; | |||||||||||||||||||||||||||
| 125 | 0 | $content =~ s/<tr(.)*?>/ /g; | |||||||||||||||||||||||||||
| 126 | 0 | $content =~ s/<td(.)*?>/ /g; | |||||||||||||||||||||||||||
| 127 | 0 | $content =~ s/<\/tr(.)*?>/ /g; | |||||||||||||||||||||||||||
| 128 | 0 | $content =~ s/<\/td(.)*?>/ /g; | |||||||||||||||||||||||||||
| 129 | 0 | $content =~ s/<div(.)*?>/ /g; | |||||||||||||||||||||||||||
| 130 | 0 | $content =~ s/<\/div(.)*?>/ /g; | |||||||||||||||||||||||||||
| 131 | 0 | $content =~ s/ / /g; | |||||||||||||||||||||||||||
| 132 | 0 | $content =~ s/.*µ§¼Æ\s*//; | |||||||||||||||||||||||||||
| 133 | 0 | $content =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 134 | 0 | $content =~ s/,//g; | |||||||||||||||||||||||||||
| 135 | 0 | @fields = split / /, $content; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | for ($i = 18; $i <= $#fields; $i += 9) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | my $date = $fields[ $i - 3 ]; | |||||||||||||||||||||||||||
| 139 | 0 | my ($yy, $mm, $dd) = split /\//, $date; | |||||||||||||||||||||||||||
| 140 | 0 | ▶ | $fields[ $i - 3 ] = (1911 + $yy) . "-" . $mm . "-" . $dd | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | if $mm; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | $result | |||||||||||||||||||||||||||
| 144 | .= $fields[$i] . "\t" | ||||||||||||||||||||||||||||
| 145 | . $fields[ $i + 1 ] . "\t" | ||||||||||||||||||||||||||||
| 146 | . $fields[ $i + 2 ] . "\t" | ||||||||||||||||||||||||||||
| 147 | . $fields[ $i + 3 ] . "\t" | ||||||||||||||||||||||||||||
| 148 | . $fields[ $i + 5 ] . "\t" | ||||||||||||||||||||||||||||
| 149 | . $fields[ $i - 3 ] . "\n"; | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 0 | return $result; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 1 | 1; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head1 NAME | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Finance::TW::TSEQuote - Check stock quotes from Taiwan Security Exchange | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | use Finance::TW::TSEQuote; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | my $quote = Finance::TW::TSEQuote->new('2002'); | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | while (1) { print $quote->get->{MatchPrice}.$/; sleep 30 } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | This module provides interface to stock information available from | ||||||||||||||||||||||||||||
| 174 | Taiwan Security Exchange. You could resolve company name to stock | ||||||||||||||||||||||||||||
| 175 | symbol, as well as getting the real time quote. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =over 4 | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =item new | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Create a stock quote object. Resolve the name to symbol | ||||||||||||||||||||||||||||
| 184 | if the argument is not a symbol. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =item resolve | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Resolve the company name to stock symbol. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =item fetchMarketFile | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | Fetch the Een-Of-Day stock information for specific company | ||||||||||||||||||||||||||||
| 193 | by year and month. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =item get | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Get the real time stock information. | ||||||||||||||||||||||||||||
| 198 | Return a hash containing stock information. The keys are: | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =over 4 | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =item Bid | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | a hash of array of best 5 matching Sell and Buy bids | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =item DQty | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | current volume | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =item MatchQty | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | daily volume | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =item MatchPrice | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | current price | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =item OpenPrice | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | opening price | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =item HighPrice | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | daily high | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =item LowPrice | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | daily low | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =back | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =back | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | Chia-liang Kao E<lt>clkao@clkao.orgE<gt> | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Cheng-Lung Sung | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | Copyright 2003-2012 by Chia-liang Kao E<lt>clkao@clkao.orgE<gt>. | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 245 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =cut | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||