-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen_sql.pm
97 lines (69 loc) · 2.36 KB
/
screen_sql.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use DBI;
use conf;
my $dbh;
my $history_table = "historical";
my $fundamental_table = "fundamentals";
my $pull_cmd = "select date,open,high,low,close,volume from $history_table where ticker=? and date <= ? order by date desc limit ?";
my $cache_cmd = "select date,open,high,low,close,volume from $history_table where ticker=? and date >= ? and date <= ? order by date desc";
my $fund_cmd = "select * from fundamentals where ticker=? and quarter_date <= ? order by quarter_date desc limit ?;";
my $div_cmd = "select ticker,date,divamt from dividends where ticker=? and date >= ? and date <= ?";
my $ondate_cmd = "select close from historical where ticker=? and date<=? order by date desc limit 1";
my $split_cmd = "select date,bef,after from splits where ticker=?";
my ($pull_sql, $cache_sql, $split_sql);
my ($div_sql, $date_cmd, $fund_sql);
sub init_mod {
$dbh = DBI->connect(conf::connect_string(), conf::connect_user() );
$cache_sql = $dbh->prepare($cache_cmd);
$split_sql = $dbh->prepare($split_cmd);
$date_cmd = $dbh->prepare($ondate_cmd);
$pull_sql = $dbh->prepare($pull_cmd);
$fund_sql = $dbh->prepare($fund_cmd);
$div_sql = $dbh->prepare($div_cmd);
}
sub pull_history_by_limit {
my $ticker = shift;
my $date = shift;
my $limit = shift;
$pull_sql->execute($ticker, $date, $limit);
my $data = $pull_sql->fetchall_arrayref();
if(scalar @$data == $limit) {
return $data;
}
}
sub pull_history_by_dates {
my $ticker = shift;
my $sdate = shift;
my $edate = shift;
$cache_sql->execute($ticker, $sdate, $edate);
return $cache_sql->fetchall_arrayref();
}
sub pull_close_on_date {
my $ticker = shift;
my @closes;
foreach $day (@_) {
$date_cmd->execute($ticker, $day);
my @t = $date_cmd->fetchrow_array();
push @closes, $t[0];
}
return @closes;
}
sub pull_dividends {
my $ticker = shift;
my $startdate = shift;
my $enddate = shift;
$div_sql->execute($ticker, $startdate, $enddate);
return $div_sql->fetchall_hashref('date');
}
sub pull_splits {
my $ticker = shift;
$split_sql->execute($ticker);
return $split_sql->fetchall_arrayref();
}
sub pull_fundamentals {
my $ticker = shift;
my $sdate = shift;
my $count = shift;
$fund_sql->execute($ticker, $sdate, $count);
return $fund_sql->fetchall_hashref('quarter_date');
}
1;