GeniusTrader/DB/genericdbi.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 122 CC 9 SCAR 28.2
CC
9
Cov
0%
new
30.0
get_last_prices
30.0
disconnect
2.0
CRAP 16.7
1 package Finance::GeniusTrader::DB::genericdbi;
2  
3 # Copyright 2005 João Antunes Costa
4 # This file is distributed under the terms of the General Public License
5 # version 2 or (at your option) any later version.
6  
7 use strict;
8 use vars qw(@ISA);
9  
10 use Finance::GeniusTrader::DB;
11 use Finance::GeniusTrader::Conf;
12 use Finance::GeniusTrader::DateTime qw();
13 use DBI;
14  
15 @ISA = qw(Finance::GeniusTrader::DB);
16  
17 =head1 NAME
18  
19 Finance::GeniusTrader::DB::genericdbi - Access to any database of quotes, as long
20 as a dbi driver is available
21  
22 =head1 DESCRIPTION
23  
24 This module is used to retrieve quotes from your existing database
25  
26  
27 =head2 Configuration
28  
29 You can put some configuration items in ~/.gt/options to indicate where
30 the database is.
31  
32 =over
33  
34 =item DB::genericdbi::dbname : the name of the database
35  
36 =item DB::genericdbi::dbhost : the host of the database
37  
38 =item DB::genericdbi::dbport : the port where the server is running
39  
40 =item DB::genericdbi::dbuser : the user account on the database
41  
42 =item DB::genericdbi::dbpasswd : the password of the user account
43  
44 =item DB::genericdbi::db : the database being used (mysql|Pg|...) ("mysql" by default)
45  
46 =item DB::genericdbi::prices_sql : The query used to retrieve price data.
47  
48  
49 Make sure to retrieve the data in the following order:
50 open, high, low, close, volume, date/time
51  
52 Also, make sure to retrieve the data ordered by date/time descending
53 Example:
54  
55 SELECT period_open, period_high, period_low, period_close, volume, Concat(date, ' ', time) FROM stockprices WHERE symbol = '$code' AND timeframe='$timeframe' ORDER BY Concat(date, ' ', time) DESC LIMIT $limit
56  
57  
58 =item DB::genericdbi::name_sql : The query used to retrieve a symbol's description.
59  
60 Example:
61  
62 SELECT name FROM stockinfo WHERE symbol = '$code';
63  
64 =back
65  
66 =head2 Functions
67  
68 =over
69  
70 =item C<< Finance::GeniusTrader::DB::genericdbi->new() >>
71  
72 =cut
73 sub new {
74 my $type = shift;
75 my $class = ref($type) || $type;
76 my $self = shift;
77  
78 Finance::GeniusTrader::Conf::default("DB::genericdbi::dbhost", ""); #aka localhost
79 Finance::GeniusTrader::Conf::default("DB::genericdbi::dbport", ""); #aka std port
80 Finance::GeniusTrader::Conf::default("DB::genericdbi::dbuser", ""); #aka current user
81 Finance::GeniusTrader::Conf::default("DB::genericdbi::dbpasswd", "");#aka user is already identified
82 Finance::GeniusTrader::Conf::default("DB::genericdbi::db", "mysql");
83  
84 my $dbdriver= Finance::GeniusTrader::Conf::get("DB::genericdbi::db");
85 my $dbname = Finance::GeniusTrader::Conf::get("DB::genericdbi::dbname");
86  
87 die("Invalid configuration. Please specify a valid dbi driver in your options file (DB::genericdbi::db)") unless ($dbdriver);
88 die("Invalid configuration. Please specify a valid database name in your options file (DB::genericdbi::dbname)") unless ($dbname);
89  
90 eval "use DBD::" . $dbdriver . ";";
91  
92 my $dbhost = Finance::GeniusTrader::Conf::get("DB::genericdbi::dbhost");
93 my $dbport = Finance::GeniusTrader::Conf::get("DB::genericdbi::dbport");
94 my $dbuser = Finance::GeniusTrader::Conf::get("DB::genericdbi::dbuser");
95 my $dbpasswd= Finance::GeniusTrader::Conf::get("DB::genericdbi::dbpasswd");
96  
97 my $connect_string = "dbi:$dbdriver:dbname=$dbname";
98 $connect_string .= ";host=$dbhost" if ($dbhost);
99 $connect_string .= ";port=$dbport" if ($dbport);
100  
101 $self->{'_dbh'} = DBI->connect($connect_string, $dbuser,
102 $dbpasswd) || die "Couldn't connect to database !\n";
103  
104 return bless $self, $class;
105 }
106  
107 =item C<< $db->disconnect >>
108  
109 Disconnects from the database.
110  
111 =cut
112 sub disconnect {
113 my $self = shift;
114 $self->{'_dbh'}->disconnect;
115 delete $self->{'prices'};
116 delete $self->{'dates'};
117 }
118  
119 =item C<< $db->get_prices($code, $timeframe) >>
120  
121 Returns a Finance::GeniusTrader::Prices object containing all known prices for the symbol $code.
122  
123 =cut
124 sub get_prices {
125 my ($self, $code, $timeframe) = @_;
126 return get_last_prices($self, $code, -1, $timeframe);
127 }
128  
129 =item C<< $db->get_last_prices($code, $limit, $timeframe) >>
130  
131 Returns a Finance::GeniusTrader::Prices object containing the $limit last known prices for
132 the symbol $code in the given $timeframe.
133  
134 =cut
135 sub get_last_prices {
136 my ($self, $code, $limit, $timeframe) = @_;
137 $timeframe = $Finance::GeniusTrader::DateTime::DAY unless ($timeframe);
138 $limit = 99999999 if ($limit==-1);
139  
140 my $q = Finance::GeniusTrader::Prices->new($limit);
141 $q->set_timeframe($timeframe);
142  
143 my $sql = Finance::GeniusTrader::Conf::get("DB::genericdbi::prices_sql::$timeframe", Finance::GeniusTrader::Conf::get('DB::genericdbi::prices_sql'));
144 die("Invalid configuration. You must specify a valid prices sql statment for your database in the options file") if (!defined($sql));
145 $sql =~ s/\$code/$code/;
146 my $tf_map_value = Finance::GeniusTrader::Conf::get("DB::genericdbi::tf_map::$timeframe",$timeframe);
147 $sql =~ s/\$timeframe/$tf_map_value/;
148 $sql =~ s/\$limit/$limit/;
149  
150 my $sth = $self->{'_dbh'}->prepare($sql)
151 || die $self->{'_dbh'}->errstr;
152 if ($sth->execute()) {# || die $self->{'_dbh'}->errstr;
153 my $array_ref = $sth->fetchall_arrayref();
154 $q->add_prices_array(reverse(@$array_ref));
155 }
156 return $q;
157 }
158  
159 =item C<< $db->get_db_name($code) >>
160  
161 Returns the name of the stock designated by $code.
162  
163 =back
164  
165 =cut
166 sub get_db_name {
167 my ($self, $code) = @_;
168  
169 my $sql = Finance::GeniusTrader::Conf::get("DB::genericdbi::name_sql") || die("Invalid configuration. You must specify a valid name_sql sql statment for your database in the options file");
170 $sql =~ s/\$code/$code/;
171  
172 my $sth = $self->{'_dbh'}->prepare($sql) || die $self->{'_dbh'}->errstr;
173 $sth->execute() || die $self->{'_dbh'}->errstr;
174 my $res = $sth->fetchrow_arrayref;
175 return $res->[0];
176 }
177  
178 1;