forked from mcafeeryan/portfolio_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_stock.pl
executable file
·107 lines (84 loc) · 2.73 KB
/
plot_stock.pl
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
98
99
100
101
102
103
104
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
use DBI;
use Time::ParseDate;
use Time::Local;
BEGIN {
$ENV{PORTF_DBMS}="oracle";
$ENV{PORTF_DB}="cs339";
$ENV{PORTF_DBUSER}="rpm267";
$ENV{PORTF_DBPASS}="Qea42wvW";
unless ($ENV{BEGIN_BLOCK}) {
use Cwd;
$ENV{ORACLE_BASE}="/raid/oracle11g/app/oracle/product/11.2.0.1.0";
$ENV{ORACLE_HOME}=$ENV{ORACLE_BASE}."/db_1";
$ENV{ORACLE_SID}="CS339";
$ENV{LD_LIBRARY_PATH}=$ENV{ORACLE_HOME}."/lib";
$ENV{BEGIN_BLOCK} = 1;
exec 'env',cwd().'/'.$0,@ARGV;
}
};
use stock_data_access;
my $type = param('type');
my $symbol = param('symbol');
my $start_year = param('start_year');
my $start_month = param('start_month');
my $start_day =param('start_day');
my $end_year = param('end_year');
my $end_month = param('end_month');
my $end_day = param('end_day');
my @start_date = (0,0,0, $start_day, $start_month, $start_year);
my @end_date = (0,0,0, $end_day, $end_month, $end_year);
my $start_timestamp = timelocal(@start_date);
my $end_timestamp = timelocal(@end_date);
if (!defined($type) || $type eq "text" || !($type eq "plot") ) {
print header(-type => 'text/html', -expires => '-1h' );
print "<html>";
print "<head>";
print "<title>Stock Data</title>";
print "</head>";
print "<body>";
if (!defined($type) || !($type eq "text") ) {
$type = "text";
print "<p>You should give type=text or type=plot - I'm assuming you mean text</p>";
}
if (!defined($symbol)) {
$symbol = "AAPL";
print "<p>You should give symbol=symbolname - I'm just giving you AAPL now </p>";
}
} elsif ($type eq "plot") {
print header(-type => 'image/png', -expires => '-1h' );
if (!defined($symbol)) {
$symbol = 'AAPL'; # default
}
}
my @rows = ExecStockSQL("2D","select timestamp, close from ".GetStockPrefix()."StocksDaily where symbol=rpad(?,16) and timestamp between ? and ?",$symbol, $start_timestamp, $end_timestamp);
if ($type eq "text") {
print "<pre>";
foreach my $r (@rows) {
print $r->[0], "\t", $r->[1], "\n";
}
print "</pre>";
print "</body>";
print "</html>";
} elsif ($type eq "plot") {
#
# This is how to drive gnuplot to produce a plot
# The basic idea is that we are going to send it commands and data
# at stdin, and it will print the graph for us to stdout
#
#
open(GNUPLOT,"| gnuplot") or die "Cannot run gnuplot";
print GNUPLOT "set term png\n"; # we want it to produce a PNG
print GNUPLOT "set output\n"; # output the PNG to stdout
print GNUPLOT "plot '-' using 1:2 with linespoints\n"; # feed it data to plot
foreach my $r (@rows) {
print GNUPLOT $r->[0], "\t", $r->[1], "\n";
}
print GNUPLOT "e\n"; # end of data
#
# Here gnuplot will print the image content
#
close(GNUPLOT);
}