| 1 | package Finance::QuoteDB; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use Exporter (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use vars qw/@EXPORT @EXPORT_OK @EXPORT_TAGS $VERSION/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Finance::Quote; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | # Bug correction in Finance::QuoteHist | ||||||||||||||||||||
| 11 | # reported on RT #64365 | ||||||||||||||||||||
| 12 | # This block can safely be removed once Finance::QuoteHist is corrected | ||||||||||||||||||||
| 13 | BEGIN { | ||||||||||||||||||||
| 14 | 2 | ▶ | $Date::Manip::Backend = 'DM5'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | } | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 2 | ▶ | use Finance::QuoteHist; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 2 | ▶ | use HTML::TableExtract; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | require Finance::QuoteDB::Geniustrader; | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 2 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 NAME | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | Finance::QuoteDB - User database tools based on Finance::Quote | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | @EXPORT = (); | ||||||||||||||||||||
| 32 | @EXPORT_OK = qw /createdb updatedb addstock/ ; | ||||||||||||||||||||
| 33 | @EXPORT_TAGS = ( all => [@EXPORT_OK] ); | ||||||||||||||||||||
| 34 | $VERSION = '0.15'; | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head1 SYNOPSIS | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | Please take a look at script/fqdb which is the command-line frontend | ||||||||||||||||||||
| 39 | to Finance::QuoteDB. | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head1 METHODS | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | =head2 new | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | new({dsn=>$dsn}) | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | sub new { | ||||||||||||||||||||
| 50 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 51 | 0 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | my $this = {} ; | |||||||||||||||||||
| 54 | 0 | bless $this, $class; | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | my $config = shift ; | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | foreach (keys %$config) { | |||||||||||||||||||
| 59 | 0 | $this->{$_} = $$config{$_}; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 0 | $this->{logger} = Log::Log4perl::get_logger(); | |||||||||||||||||||
| 62 | 0 | ▶ | if ($ENV{"FQDBDEBUG"}) { # enable debug logging if FQDBDEBUG is set | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 0 | $this->{logger}->level($DEBUG) | |||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||
| 65 | 0 | $this->{logger}->level($INFO) | |||||||||||||||||||
| 66 | } ; | ||||||||||||||||||||
| 67 | 0 | ▶ | if (my $dsn = $this->{dsn}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 0 | INFO ("CREATED FQDB object based on $dsn\n"); | |||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||
| 70 | 0 | ERROR ("No dsn specified\n") ; | |||||||||||||||||||
| 71 | 0 | die; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | return $this; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head2 createdb | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | createdb() | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | sub createdb { | ||||||||||||||||||||
| 84 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
createdb: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | my $dsn = $self->{dsn}; | |||||||||||||||||||
| 87 | 0 | my $dsnuser = $self->{dsnuser}; | |||||||||||||||||||
| 88 | 0 | my $dsnpasswd = $self->{dsnpasswd}; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | INFO ("COMMAND: Create database $dsn with user $dsnuser\n"); | |||||||||||||||||||
| 91 | 0 | my $schema = Finance::QuoteDB::Schema->connect_and_deploy($dsn,$dsnuser,$dsnpasswd); # creates the database | |||||||||||||||||||
| 92 | 0 | return $schema; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =head2 updatedb | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | updatedb() | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | sub updatedb { | ||||||||||||||||||||
| 102 | 0 | ▶ | my $self = shift ; | ||||||||||||||||||
|
Subroutine
updatedb: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | my $dsn = $self->{dsn}; | |||||||||||||||||||
| 105 | 0 | INFO ("COMMAND: Update database $dsn\n"); | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | my $schema = $self->schema(); | |||||||||||||||||||
| 108 | 0 | my @stocks = $schema -> resultset('Symbol')-> | |||||||||||||||||||
| 109 | search(undef, { order_by => "fqmarket,fqsymbol", | ||||||||||||||||||||
| 110 | columns => [qw / symbolID fqmarket fqsymbol /] }); | ||||||||||||||||||||
| 111 | 0 | my %symbolIDs ; | |||||||||||||||||||
| 112 | my %fqsymbols ; | ||||||||||||||||||||
| 113 | 0 | foreach my $stock (@stocks) { | |||||||||||||||||||
| 114 | 0 | my $fqmarket = $stock->fqmarket()->name() ; | |||||||||||||||||||
| 115 | 0 | my $symbolID = $stock->symbolID() ; | |||||||||||||||||||
| 116 | 0 | my $fqsymbol = $stock->fqsymbol() ; | |||||||||||||||||||
| 117 | 0 | ${$symbolIDs{$fqmarket}}{ $fqsymbol } = $symbolID ; | |||||||||||||||||||
| 118 | 0 | print ("SCANNING : $fqmarket - $fqsymbol -> $symbolID\n"); | |||||||||||||||||||
| 119 | }; | ||||||||||||||||||||
| 120 | 0 | foreach my $market (keys %symbolIDs) { | |||||||||||||||||||
| 121 | 0 | DEBUG "$market -->" .join( "," , keys(%{$symbolIDs{$market}}) ) ."\n" ; | |||||||||||||||||||
| 122 | 0 | $self->updatedbMarketStock ( $market , \%{$symbolIDs{$market}} ) ; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head2 updatedbMarketStock | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | updatedbMarketStock($market,\%symbolIDs) | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | sub updatedbMarketStock { | ||||||||||||||||||||
| 133 | 0 | ▶ | my ($self,$market,$stockHash) = @_ ; | ||||||||||||||||||
|
Subroutine
updatedbMarketStock: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 134 | 0 | my $schema = $self->schema(); | |||||||||||||||||||
| 135 | 0 | my @fqsymbols = keys(%{$stockHash}) ; | |||||||||||||||||||
| 136 | 0 | DEBUG "UPDATEDBMARKETSTOCK: $market -->" .join(",",@fqsymbols)."\n" ; | |||||||||||||||||||
| 137 | 0 | my $q = Finance::Quote->new(); | |||||||||||||||||||
| 138 | 0 | my %quotes = $q->fetch($market,@fqsymbols); | |||||||||||||||||||
| 139 | 0 | foreach my $stock (@fqsymbols) { | |||||||||||||||||||
| 140 | 0 | ▶ | if ($quotes{$stock,"success"}) { # This quote was retrieved | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 0 | my $symbolID = ${$stockHash}{$stock} ; | |||||||||||||||||||
| 142 | 0 | print ("Updating stock $stock ($symbolID) --> $quotes{$stock,'last'}\n"); | |||||||||||||||||||
| 143 | my $quoters = $schema->resultset('Quote')->update_or_create( | ||||||||||||||||||||
| 144 | { symbolID => $symbolID, | ||||||||||||||||||||
| 145 | date => $quotes{$stock,'isodate'}, | ||||||||||||||||||||
| 146 | previous_close => $quotes{$stock,'close'}, | ||||||||||||||||||||
| 147 | day_open => $quotes{$stock,'open'}, | ||||||||||||||||||||
| 148 | day_high => $quotes{$stock,'high'}, | ||||||||||||||||||||
| 149 | day_low => $quotes{$stock,'low'}, | ||||||||||||||||||||
| 150 | day_close => $quotes{$stock,'last'}, | ||||||||||||||||||||
| 151 | bid => $quotes{$stock,'bid'}, | ||||||||||||||||||||
| 152 | ask => $quotes{$stock,'ask'}, | ||||||||||||||||||||
| 153 | 0 | volume => $quotes{$stock,'volume'} | |||||||||||||||||||
| 154 | }); | ||||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||
| 156 | 0 | print ("Could not retrieve $stock\n"); | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | }; | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =head2 backpopulate | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | backpopulate($start_date, $end_date, $overwrite, $stocks) | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | sub backpopulate { | ||||||||||||||||||||
| 168 | 0 | ▶ | my ($self, $start_date, $end_date, $overwrite, $stocks) = @_; | ||||||||||||||||||
|
Subroutine
backpopulate: not called CC=8 SCAR=42.8
| |||||||||||||||||||||
| 169 | 0 | ▶ | $end_date = $self->today() if (!$end_date); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | 0 | ▶ | if (my @symbolIDs = split(",",$stocks)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | 0 | print ("Retrieving data...\n"); | |||||||||||||||||||
| 172 | 0 | my $schema = $self->schema(); | |||||||||||||||||||
| 173 | 0 | my %symbolID ; | |||||||||||||||||||
| 174 | 0 | foreach my $symbolID (@symbolIDs) { | |||||||||||||||||||
| 175 | 0 | my $fqsymbol = $schema -> resultset('Symbol')->single({symbolID => $symbolID})->fqsymbol() ; | |||||||||||||||||||
| 176 | 0 | $symbolID{$fqsymbol} = $symbolID ; | |||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | 0 | my @fqsymbols = keys (%symbolID); | |||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 0 | my $q = Finance::QuoteHist->new( symbols => \@fqsymbols, | |||||||||||||||||||
| 181 | start_date => $start_date, | ||||||||||||||||||||
| 182 | end_date => $end_date ); | ||||||||||||||||||||
| 183 | 0 | my $line = "" ; | |||||||||||||||||||
| 184 | 0 | my %symbols ; | |||||||||||||||||||
| 185 | 0 | foreach my $row ($q->quotes()) { | |||||||||||||||||||
| 186 | 0 | my ($fqsymbol, $date, $open, $high, $low, $close, $volume) = @$row; | |||||||||||||||||||
| 187 | 0 | $date =~ tr|/|-|; | |||||||||||||||||||
| 188 | 0 | my $tline = substr($date,0,7) ; | |||||||||||||||||||
| 189 | 0 | ▶ | if ($line ne $tline) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | 0 | INFO ("$tline") ; | |||||||||||||||||||
| 191 | 0 | %symbols = () ; | |||||||||||||||||||
| 192 | }; | ||||||||||||||||||||
| 193 | 0 | $line = $tline ; | |||||||||||||||||||
| 194 | 0 | ▶ | if (!$symbols{$fqsymbol}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 195 | 0 | $symbols{$fqsymbol}=1; | |||||||||||||||||||
| 196 | 0 | INFO (" -> $fqsymbol") ; | |||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | 0 | my %data = ( symbolID => $symbolID{$fqsymbol}, | |||||||||||||||||||
| 199 | date => $date, | ||||||||||||||||||||
| 200 | day_open => $open, | ||||||||||||||||||||
| 201 | day_high => $high, | ||||||||||||||||||||
| 202 | day_low => $low, | ||||||||||||||||||||
| 203 | day_close => $close, | ||||||||||||||||||||
| 204 | volume => $volume | ||||||||||||||||||||
| 205 | ) ; | ||||||||||||||||||||
| 206 | 0 | ▶ | if ($overwrite) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 207 | 0 | $schema->resultset('Quote')->update_or_create( \%data ) ; | |||||||||||||||||||
| 208 | } else { | ||||||||||||||||||||
| 209 | 0 | $schema->resultset('Quote')->find_or_create( \%data ) ; | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | } | ||||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | =head2 delstock | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | delstock($stocks) | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | =cut | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | sub delstock { | ||||||||||||||||||||
| 222 | 0 | ▶ | my ($self,$stocks) = @_ ; | ||||||||||||||||||
|
Subroutine
delstock: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | 0 | ▶ | if (my @stocks = split(",",$stocks)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 225 | 0 | my $schema = $self->schema(); | |||||||||||||||||||
| 226 | 0 | foreach my $stock (@stocks) { | |||||||||||||||||||
| 227 | 0 | print ("Deleting stock $stock\n"); | |||||||||||||||||||
| 228 | 0 | my $rs = $schema -> resultset('Symbol')-> | |||||||||||||||||||
| 229 | search({'symbolID' => $stock}); | ||||||||||||||||||||
| 230 | 0 | $rs->delete_all(); | |||||||||||||||||||
| 231 | } | ||||||||||||||||||||
| 232 | } else { | ||||||||||||||||||||
| 233 | 0 | print ("No stocks specified\n") ; | |||||||||||||||||||
| 234 | } | ||||||||||||||||||||
| 235 | }; | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =head2 addstock | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | addstock($market,$stocks) | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | $stocks is in the format FQsymbol[USERsymbol],... | ||||||||||||||||||||
| 242 | If USERsymbol is ommitted then USERsymbol will be set to FQsymbol | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | =cut | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | sub addstock { | ||||||||||||||||||||
| 247 | 0 | ▶ | my ($self,$market,$stocks) = @_ ; | ||||||||||||||||||
|
Subroutine
addstock: not called CC=8 SCAR=42.8
| |||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | 0 | ▶ | if (!$market) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 250 | 0 | print ("No market specified\n") ; | |||||||||||||||||||
| 251 | return | ||||||||||||||||||||
| 252 | 0 | } else { | |||||||||||||||||||
| 253 | 0 | print ("Getting stocks from $market\n") ; | |||||||||||||||||||
| 254 | } | ||||||||||||||||||||
| 255 | 0 | ▶ | if (my @stocks = split(",",$stocks)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 256 | 0 | my %symbolIDs ; | |||||||||||||||||||
| 257 | 0 | foreach my $stockItem (@stocks) { | |||||||||||||||||||
| 258 | 0 | ▶ | if ( $stockItem =~ m/([^\[]+)(\[(.+)\])?/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 259 | 0 | my ($fqsymbol,$symbolID) = ($1,$3) ; | |||||||||||||||||||
| 260 | 0 | ▶ | $symbolID = $fqsymbol if (!$symbolID) ; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 261 | 0 | INFO (" Stock $fqsymbol <- $symbolID") ; | |||||||||||||||||||
| 262 | 0 | $symbolIDs{$fqsymbol}=$symbolID ; | |||||||||||||||||||
| 263 | } | ||||||||||||||||||||
| 264 | } | ||||||||||||||||||||
| 265 | 0 | my @fqsymbols = keys %symbolIDs ; | |||||||||||||||||||
| 266 | 0 | my $q = Finance::Quote->new(); | |||||||||||||||||||
| 267 | 0 | my %quotes = $q->fetch($market,@fqsymbols); | |||||||||||||||||||
| 268 | 0 | foreach my $stock (@fqsymbols) { | |||||||||||||||||||
| 269 | 0 | print ("Checking stock $stock\n"); | |||||||||||||||||||
| 270 | 0 | ▶ | if ($quotes{$stock,"success"}) { # This quote was retrieved | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 271 | 0 | print (" --> $quotes{$stock,'name'}\n") ; | |||||||||||||||||||
| 272 | 0 | my $schema = $self->schema(); | |||||||||||||||||||
| 273 | 0 | my $marketID = $schema->resultset('FQMarket')->find_or_create({name=>$market})->marketID(); | |||||||||||||||||||
| 274 | $schema->populate('Symbol', | ||||||||||||||||||||
| 275 | [[qw /symbolID name fqmarket fqsymbol isin currency/], | ||||||||||||||||||||
| 276 | 0 | [$symbolIDs{$stock}, $quotes{$stock,'name'}, $marketID, $stock, '', $quotes{$stock,'currency'} ]]); | |||||||||||||||||||
| 277 | } else { | ||||||||||||||||||||
| 278 | 0 | print ("Could not retrieve $stock\n"); | |||||||||||||||||||
| 279 | } | ||||||||||||||||||||
| 280 | } | ||||||||||||||||||||
| 281 | } else { | ||||||||||||||||||||
| 282 | 0 | print ("No stocks specified\n") ; | |||||||||||||||||||
| 283 | } | ||||||||||||||||||||
| 284 | }; | ||||||||||||||||||||
| 285 | |||||||||||||||||||||
| 286 | =head2 getquotes | ||||||||||||||||||||
| 287 | |||||||||||||||||||||
| 288 | getquotes( $USERsymbols, $date_start [,$date_end] ) | ||||||||||||||||||||
| 289 | |||||||||||||||||||||
| 290 | This function returns quotes between $date_start and $date_end for the specified | ||||||||||||||||||||
| 291 | user symbols (comma separated list). Range will be one day if $date_end is | ||||||||||||||||||||
| 292 | omitted. | ||||||||||||||||||||
| 293 | |||||||||||||||||||||
| 294 | =cut | ||||||||||||||||||||
| 295 | |||||||||||||||||||||
| 296 | sub getquotes { | ||||||||||||||||||||
| 297 | 0 | ▶ | my ($self,$USERsymbol,$date_start,$date_end) = @_ ; | ||||||||||||||||||
|
Subroutine
getquotes: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 298 | 0 | ▶ | $date_end = $date_start if !($date_end) ; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 299 | 0 | my $schema = $self->schema(); | |||||||||||||||||||
| 300 | |||||||||||||||||||||
| 301 | 0 | my @q = $schema->resultset('Quote') | |||||||||||||||||||
| 302 | ->search( { symbolID=>$USERsymbol, | ||||||||||||||||||||
| 303 | date=>{'BETWEEN',[$date_start, $date_end]} }, | ||||||||||||||||||||
| 304 | { columns=> [qw/ date day_open day_high day_low day_close volume /], | ||||||||||||||||||||
| 305 | order_by=> [qw/ date /] }); | ||||||||||||||||||||
| 306 | 0 | ▶ | @q ? return \@q : 0 ; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 307 | } | ||||||||||||||||||||
| 308 | |||||||||||||||||||||
| 309 | =head2 dumpquotes | ||||||||||||||||||||
| 310 | |||||||||||||||||||||
| 311 | dumpquotes ( $USERsymbols, $date_start [,$date_end] ) | ||||||||||||||||||||
| 312 | |||||||||||||||||||||
| 313 | This function dumps quotes between $date_start and $date_end for the specified | ||||||||||||||||||||
| 314 | user symbols (comma separated list). Range will be one day if $date_end is | ||||||||||||||||||||
| 315 | omitted. | ||||||||||||||||||||
| 316 | |||||||||||||||||||||
| 317 | =cut | ||||||||||||||||||||
| 318 | |||||||||||||||||||||
| 319 | sub dumpquotes { | ||||||||||||||||||||
| 320 | 0 | ▶ | my ($self,$USERsymbols,$date_start,$date_end) = @_ ; | ||||||||||||||||||
|
Subroutine
dumpquotes: not called CC=6 SCAR=37.4
| |||||||||||||||||||||
| 321 | 0 | ▶ | $date_end = $date_start if !($date_end) ; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 322 | 0 | my $schema = $self->schema(); | |||||||||||||||||||
| 323 | |||||||||||||||||||||
| 324 | 0 | ▶ | if (my @stocks = split(",",$USERsymbols)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 325 | 0 | foreach my $USERsymbol (@stocks) { | |||||||||||||||||||
| 326 | 0 | ▶ | if ( my $quotesArray = $self->getquotes ( $USERsymbol, $date_start, $date_end ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 327 | 0 | print "STOCK : $USERsymbol\n"; | |||||||||||||||||||
| 328 | 0 | print "DATE OPEN HIGH LOW CLOSE VOLUME\n" ; | |||||||||||||||||||
| 329 | 0 | foreach my $q (@$quotesArray) { | |||||||||||||||||||
| 330 | 0 | printf "%10s %8.2f %8.2f %8.2f %8.2f %12d\n", | |||||||||||||||||||
| 331 | $q->date(), $q->day_open(), $q->day_high(), $q->day_low(), $q->day_close(), $q->volume() ; | ||||||||||||||||||||
| 332 | } | ||||||||||||||||||||
| 333 | } else { | ||||||||||||||||||||
| 334 | 0 | print "NO DATA for stock $USERsymbol\n"; | |||||||||||||||||||
| 335 | } | ||||||||||||||||||||
| 336 | } | ||||||||||||||||||||
| 337 | } | ||||||||||||||||||||
| 338 | } | ||||||||||||||||||||
| 339 | |||||||||||||||||||||
| 340 | =head2 dumpstocks | ||||||||||||||||||||
| 341 | |||||||||||||||||||||
| 342 | dumpstocks () | ||||||||||||||||||||
| 343 | |||||||||||||||||||||
| 344 | This function dumps the symbols of the stocks in the database. | ||||||||||||||||||||
| 345 | |||||||||||||||||||||
| 346 | =cut | ||||||||||||||||||||
| 347 | |||||||||||||||||||||
| 348 | sub dumpstocks { | ||||||||||||||||||||
| 349 | 0 | ▶ | my $self = shift ; | ||||||||||||||||||
|
Subroutine
dumpstocks: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 350 | |||||||||||||||||||||
| 351 | 0 | my $dsn = $self->{dsn}; | |||||||||||||||||||
| 352 | 0 | INFO ("COMMAND: Dump stocks in database $dsn\n"); | |||||||||||||||||||
| 353 | |||||||||||||||||||||
| 354 | 0 | my $schema = $self->schema(); | |||||||||||||||||||
| 355 | 0 | my @stocks = $schema -> resultset('Symbol')-> | |||||||||||||||||||
| 356 | search(undef, { order_by => "symbolID,fqmarket,fqsymbol", | ||||||||||||||||||||
| 357 | columns => [qw / symbolID fqmarket fqsymbol /] }); | ||||||||||||||||||||
| 358 | 0 | print " USERSYMBOL FQMARKET FQSYMBOL\n"; | |||||||||||||||||||
| 359 | 0 | foreach my $stock (@stocks) { | |||||||||||||||||||
| 360 | 0 | my $fqmarket = $stock->fqmarket()->name() ; | |||||||||||||||||||
| 361 | 0 | my $symbolID = $stock->symbolID() ; | |||||||||||||||||||
| 362 | 0 | my $fqsymbol = $stock->fqsymbol() ; | |||||||||||||||||||
| 363 | 0 | printf "%15s %15s %15s\n",$symbolID,$fqmarket,$fqsymbol; | |||||||||||||||||||
| 364 | }; | ||||||||||||||||||||
| 365 | } | ||||||||||||||||||||
| 366 | |||||||||||||||||||||
| 367 | =head2 add_yahoo_stocks | ||||||||||||||||||||
| 368 | |||||||||||||||||||||
| 369 | add_yahoo_stocks( $exchanges , [ $refsearchlist ] ) | ||||||||||||||||||||
| 370 | |||||||||||||||||||||
| 371 | retrieves yahoo tickers for specified exchanges and stores them in your database | ||||||||||||||||||||
| 372 | NOTE: $exchanges being the ID as coming from yahoo. | ||||||||||||||||||||
| 373 | NYQ for Nyse, PAR for Paris | ||||||||||||||||||||
| 374 | $refsearchlist is an optional reference to a list of search patterns. defaults to [**AA .. **ZZ] | ||||||||||||||||||||
| 375 | |||||||||||||||||||||
| 376 | =cut | ||||||||||||||||||||
| 377 | |||||||||||||||||||||
| 378 | sub add_yahoo_stocks { | ||||||||||||||||||||
| 379 | # http://uk.biz.yahoo.com/p/uk/cpi/index.html -> list of european stocks | ||||||||||||||||||||
| 380 | my ($self,$exchanges,$refsearchlist) = @_ ; | ||||||||||||||||||||
| 381 | my $popquantity = 30 ; # number of stocks to add in 1 call of addstock | ||||||||||||||||||||
| 382 | |||||||||||||||||||||
| 383 | if (!defined($exchanges)) { | ||||||||||||||||||||
| 384 | ERROR ("No exchanges specified"); | ||||||||||||||||||||
| 385 | } else { | ||||||||||||||||||||
| 386 | my %exchanges ; | ||||||||||||||||||||
| 387 | foreach (split(',',$exchanges)) { | ||||||||||||||||||||
| 388 | $exchanges{$_}=1 ; | ||||||||||||||||||||
| 389 | } ; | ||||||||||||||||||||
| 390 | |||||||||||||||||||||
| 391 | 2 | ▶ | no strict 'subs' ; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 392 | if (!defined(@$refsearchlist)) { | ||||||||||||||||||||
| 393 | $refsearchlist = [AA .. ZZ] ; | ||||||||||||||||||||
| 394 | $$refsearchlist[$_] = "**".$$refsearchlist[$_] foreach (0 .. $#{@$refsearchlist}) ; # add ** in front of each list item | ||||||||||||||||||||
| 395 | } | ||||||||||||||||||||
| 396 | DEBUG ("$_") foreach (@$refsearchlist); | ||||||||||||||||||||
| 397 | |||||||||||||||||||||
| 398 | my $ua = LWP::UserAgent->new; | ||||||||||||||||||||
| 399 | $ua->env_proxy; | ||||||||||||||||||||
| 400 | INFO("Adding symbols from $exchanges.") ; | ||||||||||||||||||||
| 401 | |||||||||||||||||||||
| 402 | my %symbols ; | ||||||||||||||||||||
| 403 | |||||||||||||||||||||
| 404 | foreach my $letter (@$refsearchlist) { | ||||||||||||||||||||
| 405 | my $yahoo_url = "http://finance.yahoo.com/lookup?s=$letter&t=S" ; # t=S means ONLY stocks | ||||||||||||||||||||
| 406 | |||||||||||||||||||||
| 407 | my $b = 0 ; # counter in url | ||||||||||||||||||||
| 408 | my $cont ; | ||||||||||||||||||||
| 409 | do { | ||||||||||||||||||||
| 410 | $cont = 1; # continue increasing b | ||||||||||||||||||||
| 411 | my $url = $yahoo_url."&b=".$b ; | ||||||||||||||||||||
| 412 | DEBUG("URL: $url"); | ||||||||||||||||||||
| 413 | my $req = HTTP::Request->new(GET => $url); | ||||||||||||||||||||
| 414 | my $reply = $ua->request($req); | ||||||||||||||||||||
| 415 | if ($reply->is_success) { | ||||||||||||||||||||
| 416 | my ($from,$to,$total)=(0,0,0) ; | ||||||||||||||||||||
| 417 | if ($reply->content=~ m|Showing\s+(\d)+ - (\d+) of\s+(\d+)|) { # check if this is last page for this market | ||||||||||||||||||||
| 418 | ($from,$to,$total)=($1,$2,$3); | ||||||||||||||||||||
| 419 | INFO ("For $letter: ".int($to*100/$total)." % completed"); | ||||||||||||||||||||
| 420 | $b=$to; # next page should start at this symbol. actually starts at symbol+1 | ||||||||||||||||||||
| 421 | $cont = ($to < $total); | ||||||||||||||||||||
| 422 | } | ||||||||||||||||||||
| 423 | # scrape the symbols from this page | ||||||||||||||||||||
| 424 | my $te = HTML::TableExtract->new( headers=>[qw /Symbol Exchange/ ] ); | ||||||||||||||||||||
| 425 | $te->parse($reply->content); | ||||||||||||||||||||
| 426 | foreach my $ts ($te->tables) { | ||||||||||||||||||||
| 427 | my $countrows = 0; | ||||||||||||||||||||
| 428 | foreach my $tr ($ts->rows) { | ||||||||||||||||||||
| 429 | $countrows++; | ||||||||||||||||||||
| 430 | my $trsymb = @$tr[0] ; | ||||||||||||||||||||
| 431 | $trsymb =~ s/ //g ; | ||||||||||||||||||||
| 432 | my $exchsym = @$tr[1] ; | ||||||||||||||||||||
| 433 | $exchsym =~ s/ //g ; | ||||||||||||||||||||
| 434 | if (defined($exchanges{$exchsym})) { | ||||||||||||||||||||
| 435 | INFO (" Symbol: $trsymb - Exchange $exchsym"); | ||||||||||||||||||||
| 436 | $symbols{$trsymb}+=1 ; # add the symbol as a key in the hash removes duplicates automatically | ||||||||||||||||||||
| 437 | } | ||||||||||||||||||||
| 438 | } | ||||||||||||||||||||
| 439 | DEBUG ("--> $countrows rows"); | ||||||||||||||||||||
| 440 | if ($countrows && ($total==0)) { # there are rows on this page and we are on the last page (no total) | ||||||||||||||||||||
| 441 | INFO ("For $letter: 100 % completed"); | ||||||||||||||||||||
| 442 | $cont=0 ; | ||||||||||||||||||||
| 443 | } | ||||||||||||||||||||
| 444 | } | ||||||||||||||||||||
| 445 | } | ||||||||||||||||||||
| 446 | if ($cont) { | ||||||||||||||||||||
| 447 | my $sleeptime = int(15+rand(5)) ; | ||||||||||||||||||||
| 448 | INFO("Sleeping $sleeptime"); | ||||||||||||||||||||
| 449 | sleep $sleeptime; # needed otherwise we might overload yahoo server | ||||||||||||||||||||
| 450 | } | ||||||||||||||||||||
| 451 | } while ($cont); | ||||||||||||||||||||
| 452 | } | ||||||||||||||||||||
| 453 | |||||||||||||||||||||
| 454 | my @symbols = sort keys %symbols ; | ||||||||||||||||||||
| 455 | while ($#symbols>0) { # still elements in the array | ||||||||||||||||||||
| 456 | my $sleeptime = int(10+rand(10)) ; | ||||||||||||||||||||
| 457 | INFO("Sleeping $sleeptime"); | ||||||||||||||||||||
| 458 | sleep $sleeptime; # needed otherwise we might overload yahoo server | ||||||||||||||||||||
| 459 | my $stocks = join (",",splice(@symbols,0,$popquantity)); # take $popquantity number of elements out | ||||||||||||||||||||
| 460 | # my $stocks = join (",",@symbols[0..$popquantity-1]); | ||||||||||||||||||||
| 461 | # @symbols = @symbols[$popquantity+1..$#symbols-1]; | ||||||||||||||||||||
| 462 | INFO (" Adding stocks: $stocks"); | ||||||||||||||||||||
| 463 | $self->addstock('yahoo',$stocks); # add stocks in database | ||||||||||||||||||||
| 464 | } | ||||||||||||||||||||
| 465 | } | ||||||||||||||||||||
| 466 | INFO("Finished adding stocks from yahoo"); | ||||||||||||||||||||
| 467 | } | ||||||||||||||||||||
| 468 | |||||||||||||||||||||
| 469 | =head2 schema | ||||||||||||||||||||
| 470 | |||||||||||||||||||||
| 471 | schema() | ||||||||||||||||||||
| 472 | |||||||||||||||||||||
| 473 | If necessary, creates a DBIx::Class::Schema and returns a reference to that DBIx::Class::Schema. | ||||||||||||||||||||
| 474 | |||||||||||||||||||||
| 475 | =cut | ||||||||||||||||||||
| 476 | |||||||||||||||||||||
| 477 | sub schema { | ||||||||||||||||||||
| 478 | my $self = shift ; | ||||||||||||||||||||
| 479 | my $dsn = $self->{dsn}; | ||||||||||||||||||||
| 480 | my $dsnuser = $self->{dsnuser}; | ||||||||||||||||||||
| 481 | my $dsnpasswd = $self->{dsnpasswd}; | ||||||||||||||||||||
| 482 | if (!$self->{schema}) { | ||||||||||||||||||||
| 483 | if (my $schema = Finance::QuoteDB::Schema->connect($dsn,$dsnuser,$dsnpasswd)) { | ||||||||||||||||||||
| 484 | INFO ("Connected to database $dsn\n"); | ||||||||||||||||||||
| 485 | $self->{schema} = $schema ; | ||||||||||||||||||||
| 486 | } else { | ||||||||||||||||||||
| 487 | ERROR ("Could not connect to database $dsn\n") ; | ||||||||||||||||||||
| 488 | die ; | ||||||||||||||||||||
| 489 | } | ||||||||||||||||||||
| 490 | } | ||||||||||||||||||||
| 491 | return $self->{schema} | ||||||||||||||||||||
| 492 | } | ||||||||||||||||||||
| 493 | |||||||||||||||||||||
| 494 | =head2 today | ||||||||||||||||||||
| 495 | |||||||||||||||||||||
| 496 | today() | ||||||||||||||||||||
| 497 | |||||||||||||||||||||
| 498 | returns current date in isodate format | ||||||||||||||||||||
| 499 | |||||||||||||||||||||
| 500 | =cut | ||||||||||||||||||||
| 501 | |||||||||||||||||||||
| 502 | sub today { | ||||||||||||||||||||
| 503 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); | ||||||||||||||||||||
| 504 | return ($year+1900)."-".sprintf('%02d',$mon+1)."-".sprintf('%02d',$mday) ; | ||||||||||||||||||||
| 505 | } | ||||||||||||||||||||
| 506 | |||||||||||||||||||||
| 507 | =head1 AUTHOR | ||||||||||||||||||||
| 508 | |||||||||||||||||||||
| 509 | Erik Colson, C<< <eco at ecocode.net> >> | ||||||||||||||||||||
| 510 | |||||||||||||||||||||
| 511 | =head1 BUGS | ||||||||||||||||||||
| 512 | |||||||||||||||||||||
| 513 | Please report any bugs or feature requests to C<bug-finance-quotedb at | ||||||||||||||||||||
| 514 | rt.cpan.org>, or through the web interface at | ||||||||||||||||||||
| 515 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Finance-QuoteDB>. I will be | ||||||||||||||||||||
| 516 | notified, and you'll automatically be notified of progress on your bug as I make | ||||||||||||||||||||
| 517 | changes. | ||||||||||||||||||||
| 518 | |||||||||||||||||||||
| 519 | =head1 SUPPORT | ||||||||||||||||||||
| 520 | |||||||||||||||||||||
| 521 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||
| 522 | |||||||||||||||||||||
| 523 | perldoc Finance::QuoteDB | ||||||||||||||||||||
| 524 | |||||||||||||||||||||
| 525 | |||||||||||||||||||||
| 526 | You can also look for information at: | ||||||||||||||||||||
| 527 | |||||||||||||||||||||
| 528 | =over 4 | ||||||||||||||||||||
| 529 | |||||||||||||||||||||
| 530 | =item * RT: CPAN's request tracker | ||||||||||||||||||||
| 531 | |||||||||||||||||||||
| 532 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Finance-QuoteDB> | ||||||||||||||||||||
| 533 | |||||||||||||||||||||
| 534 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||
| 535 | |||||||||||||||||||||
| 536 | L<http://annocpan.org/dist/Finance-QuoteDB> | ||||||||||||||||||||
| 537 | |||||||||||||||||||||
| 538 | =item * CPAN Ratings | ||||||||||||||||||||
| 539 | |||||||||||||||||||||
| 540 | L<http://cpanratings.perl.org/d/Finance-QuoteDB> | ||||||||||||||||||||
| 541 | |||||||||||||||||||||
| 542 | =item * Search CPAN | ||||||||||||||||||||
| 543 | |||||||||||||||||||||
| 544 | L<http://search.cpan.org/dist/Finance-QuoteDB> | ||||||||||||||||||||
| 545 | |||||||||||||||||||||
| 546 | =back | ||||||||||||||||||||
| 547 | |||||||||||||||||||||
| 548 | |||||||||||||||||||||
| 549 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||
| 550 | |||||||||||||||||||||
| 551 | |||||||||||||||||||||
| 552 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 553 | |||||||||||||||||||||
| 554 | Copyright 2008-2011 Erik Colson, all rights reserved. | ||||||||||||||||||||
| 555 | |||||||||||||||||||||
| 556 | This file is part of Finance::QuoteDB. | ||||||||||||||||||||
| 557 | |||||||||||||||||||||
| 558 | Finance::QuoteDB is free software: you can redistribute it and/or | ||||||||||||||||||||
| 559 | modify it under the terms of the GNU General Public License as | ||||||||||||||||||||
| 560 | published by the Free Software Foundation, either version 3 of the | ||||||||||||||||||||
| 561 | License, or (at your option) any later version. | ||||||||||||||||||||
| 562 | |||||||||||||||||||||
| 563 | Finance::QuoteDB is distributed in the hope that it will be useful, | ||||||||||||||||||||
| 564 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||
| 565 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||||||||||||||||
| 566 | General Public License for more details. | ||||||||||||||||||||
| 567 | |||||||||||||||||||||
| 568 | You should have received a copy of the GNU General Public License | ||||||||||||||||||||
| 569 | along with Finance::QuoteDB. If not, see | ||||||||||||||||||||
| 570 | <http://www.gnu.org/licenses/>. | ||||||||||||||||||||
| 571 | |||||||||||||||||||||
| 572 | =cut | ||||||||||||||||||||
| 573 | |||||||||||||||||||||
| 574 | 1; # End of Finance::QuoteDB | ||||||||||||||||||||