This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
forked from KennyYou/Stock_API_Trading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.php
200 lines (154 loc) · 5.45 KB
/
graph.php
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!DOCTYPE html>
<html>
<body>
<div class="topnav">
<a class="active" href="loginsuccess.php">HOME</a>
<a class="active" href="profile.php">PROFILE</a>
<a class="active" href="buy.php">BUY</a>
<a class="active" href="sell.php">SELL</a>
<a class="active" href="index.html">LOGOUT<a>
</div>
<form method="POST">
<h2>Search a Stock:</h2>
<input type="text" name="searchgraph" placeholder="Enter a stock symbol:" required>
<button type="submit">Search</button><br>
</form>
<?php
function media_graph($symbol, $args = '') {
//Get response for data.
$title = $_POST['searchgraph'];
echo "<br>Stock History of stock symbol: " . $title;
require_once('path.inc');
require_once('get_host_info.inc');
require_once('rabbitMQLib.inc');
if(isset($_POST['searchgraph'])){
$client = new rabbitMQClient("testRabbitMQ.ini","testServer");
if (isset($argv[1]))
{
$msg = $argv[1];
}
else
{
$msg = "graph";
}
//Send search request over
$request = array();
$request['type'] = "graph";
$request['searchgraph'] = $_POST['searchgraph'];
$request['message'] = $msg;
$response = $client->send_request($request);
echo "".PHP_EOL;
echo"\n";
echo "<br>";
} //End updating emails %
$farray = array('width' => '1200', 'height' => '500','time' => '2','number' => '90',
'size' => 'full','interval' => '60','cache' => 3600);
// Merge $args with $farray
$farray = (empty($args)) ? $farray : array_merge($farray, $args);
$stcks = 'alpha_vantage_stock_' . md5(serialize($farray) . $symbol);
$cachedposts = cachefunc($stcks, $farray['cache']);
//If there isn't something already in the cache
if ($cachedposts !== false) {
return $cachedposts;
} else {
switch ($farray['time']) {
case 1:
$series = 'TIME_SERIES_INTRADAY';
$series_name = 'Time Series (' . $farray['interval'] . 'min)';
break;
case 2:
$series = 'TIME_SERIES_DAILY';
$series_name = 'Time Series (Daily)';
break;
case 3:
$series = 'TIME_SERIES_DAILY_ADJUSTED';
$series_name = 'Time Series (Daily)';
break;
case 4:
$series = 'TIME_SERIES_WEEKLY';
$series_name = 'Weekly Time Series';
break;
case 5:
$series = 'TIME_SERIES_MONTHLY';
$series_name = 'Monthly Time Series';
break;
default:
$series = 'Time Series (Daily)';
break;
}
//Hit response
$data = @file_get_contents($response[1] . $series . '&symbol=' . $symbol . '&interval=' . $farray['interval'] . 'min&apikey=' . $response[0] . '&interval=' . $farray['interval'] . 'min&outputsize=' . $farray['size']);
if ($data === false) return '<p>Data currently unavailable.</p>';
$data = json_decode($data, true);
$data = $data[$series_name];
//Checking out the data recieved.
//Testing responses from the DMZ
//var_dump($data);
if ($farray['number'] != '') $data = array_slice($data, 0, $farray['number'], true);
$data = array_reverse($data, true);
foreach ($data AS $key => $value) {
$chart .= ',[new Date(' . str_replace(array('-', ' ', ':'), ',', $key) . '), ' . $value['4. close'] . ']';
}
$chart = ltrim($chart, ',');
$return = "<script type='text/javascript' src='https://www.gstatic.com/charts/loader.js'></script>
<script type='text/javascript'>
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawTrendlines);
function drawTrendlines() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Close');
data.addRows([
$chart
]);
var options = {
hAxis: {
title: 'Date'
},
backgroundColor: 'transparent',
vAxis: {
title: 'Stock Price'
},
colors: ['#06ab09'],
trendlines: {
// 0: {type: 'exponential', color: '#333', opacity: 1},
// 1: {type: 'linear', color: '#111', opacity: .3}
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div_$interval'));
chart.draw(data, options);
}
</script>";
$return .= '<div id="chart_div_' . $interval . '" style="width: ' . $farray['width'] . 'px; height: ' . $farray['height'] . 'px;"></div>';
readfunc($stcks, $return, $farray['cache']);
return $return;
}
}
// Send out the user inputed function for graphing.
/*
if(isset($_POST['searchg'])){
$request = $_POST['searchg'];
*/
echo media_graph($symbol = $_POST['searchgraph']);
//END PHP
function readfunc($stcks, $data) {
$location = '/path/to/public_html/your/cache/' . $stcks . '.txt';
if (is_array($data)) $data = serialize($data);
$fp = @fopen($location, 'w');
$result = fwrite($fp, $data);
fclose($fp);
return ($result !== false) ? true : false;
}
function cachefunc($stcks, $cache = '10000') {
$location = '/path/to/public_html/your/cache/' . $stcks . '.txt';
if ( file_exists($location) && (time() - $cache < filemtime($location)) ) {
$result = @file_get_contents($location);
if (beliefmedia_is_serialized($result) === true) $result = unserialize($result);
return ($result) ? $result : false;
} else {
return false;
}
}
?>
</body>
</html>