This repository has been archived by the owner on Oct 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
382 lines (338 loc) · 12.5 KB
/
index.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<!-- D3 Trades Navigator -->
<!-- Initial PHP scripts -->
<?php
class SQLiteDB extends SQLite3
{
function __construct()
{
$this->open('crypto.db');// open database file
}
}
$db = new SQLiteDB(); // Call database file
if(!$db){ // If no database file exists
echo $db->lastErrorMsg();
} else { // If databse file is located
$trades=$db->prepare('SELECT*FROM test_trades WHERE pair1=:pair1 AND pair2=:pair2');
// Database values
$counts = $db->query('SELECT COUNT(*) as tradeCount FROM test_trades');
$count = $counts->fetchArray();
$tradeCount = $count['tradeCount'];
$pair1Data = array();
$pair2Data = array();
$currencies = array();
$pair1s = $db->query('SELECT DISTINCT pair1 FROM test_trades');
$pair2s = $db->query('SELECT DISTINCT pair2 FROM test_trades');
if ($pair1 or $pair2){
$plotData = [];
while ($row = $result->fetchArray()){
// Plot Data
$plotData[] = array("ts" => $row['ts'],"price" => $row['price']);
}
}
while ($pair1value = $pair1s->fetchArray()){
array_push($pair1Data, $pair1value[0]);
}
while ($pair2value = $pair2s->fetchArray()){
array_push($pair2Data, $pair2value[0]);
}
$currenciesData = $pair1Data + $pair2Data;
$rowLimit = 100;
$timeGranularity = 60;
}
// Handle Post requests
if (isset($_POST['submit']))
{
$pair1 = $_POST['pair1'];
$pair2 = $_POST['pair2'];
$chartType = $_POST['chartType'];
$rowLimit = $_POST['rowLimit'];
$timeGranularity = $_POST['timeGranularity'];
}
?>
<html>
<!-- Content Delivery Network Scripts -->
<head>
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
<!-- Bootstrap -->
<!-- Complete CSS-->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
<!-- Complete JavaScript -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
<!-- Complete JavaScript Bundle -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.bundle.min.js" integrity="sha384-CS0nxkpPy+xUkNGhObAISrkg/xjb3USVCwy+0/NMzd5VxgY4CMCyTkItmy5n0voC" crossorigin="anonymous"></script>
<!-- D3 -->
<script src="https://d3js.org/d3.v5.min.js"></script>
<title>
<?php
if($pair1 && $pair2 && $chartType){
echo $pair1."-".$pair2.": ".ucfirst($chartType)." Chart";
} else {
echo "Trades Database";
}
?>
</title>
</head>
<!-- HTML Page Content -->
<body>
<div class="row container m-2">
<h3>Trades Database</h3>
<h6 class="m-2 text-muted">
<?php echo $tradeCount;?> Total Trades,
</h6>
<h6 class="m-2 text-muted">
<?php echo count($currenciesData);?> Total Currencies,
</h6>
</div>
<!-- Set Chart Parameter -->
<form class="col" action="index.php" method="post">
<div class="row container m-2">
<!-- Exchange Pairs - Text Input -->
<div class="col-3 container m-2">
<h4>Exchange Pairs</h4>
<select name="pair1" placeholder="Pair 1">
<?php
if($pair1){
echo "<option value='".$pair1."'selected>".$pair1."</option>";
} else {
echo "<option value='' disabled selected>Pair 1</option>";
}
foreach ($pair1Data as $pair1Select){
echo "<option value='".$pair1Select."'>".$pair1Select."</option>";
}
?>
</select>
-<select name="pair2" placeholder="Pair 2">
<?php
if($pair2){
echo "<option value='".$pair2."'selected>".$pair2."</option>";
} else {
echo "<option value='' disabled selected>Pair 2</option>";
}
foreach ($pair2Data as $pair2Select){
echo "<option value='".$pair2Select."'>".$pair2Select."</option>";
}
?>
</select>
</div>
<!-- Chart Types - Radio Buttons -->
<h4 class="mt-4">Chart Types:</h4>
<div class="col btn-group btn-group-toggle mt-4 mb-4" data-toggle="buttons">
<!-- Price Chart Button -->
<label
<?php
echo "class='btn btn-secondary";
if($chartType=="price"){
echo "active'>";
} else {
echo "'>";
} ?>
<input class="btn btn-outline" type="radio" name="chartType" value="price"
<?php
if ((isset($chartType)&& $chartType=="price")or!isset($chartType)){
echo "checked";
};
?>
> Price
</label>
<!-- Sentiment Chart Button -->
<label <?php
echo "class='btn btn-secondary";
if($chartType=="sentiment"){
echo "active'>";
} else {
echo "'>";
} ?>
<input
class="btn btn-outline"
type="radio"
name="chartType"
value="sentiment"
<?php
if(isset($chartType)&&$chartType=="sentiment"){
echo"checked";
}?>> Sentiment
</label>
<!-- Volume Chart Button -->
<label <?php
echo "class='btn btn-secondary";
if($chartType=="volume"){
echo "active'>";
} else {
echo "'>";
} ?>
<input
class="btn btn-outline"
type="radio"
name="chartType"
value="volume"
<?php
if(isset($chartType)&& $chartType=="volume"){
echo"checked";
}?>> Volume
</label>
</div>
</div>
<!-- Preferences Row -->
<div class="container row m-2">
Row Limit:
<input
class="mr-4"
type="text"
name="rowLimit"
value=<?php echo $rowLimit; ?> />
Time Granularity:
<input
type="text"
name="timeGranularity"
value=<?php echo $timeGranularity; ?> /> Seconds
</div>
<!-- Submit Button -->
<div class="container row m-2">
<input class="btn btn-outline-primary col"type="submit" name="submit">
</div>
</form>
<!-- PHP framework for Chart Parameters-->
<?php
$trades->bindValue(':pair1',$pair1);
$trades->bindValue(':pair2',$pair2);
$result = $trades->execute();
if ($pair1 && $pair2){
$plotData = [];
$oldTimestamp = -1;
$cache = 0;
$cacheRows = 0;
while ($row = $result->fetchArray()){
if ($oldTimestamp == -1){ // set Initial Timestamp
$oldTimestamp = $row['ts'];
}
if (floatval($row['ts'])<(floatval($oldTimestamp)-$timeGranularity)){
// record plot point
$average = $cache/$cacheRows;
$plotData[] = array("x" => $row['ts'],"y" => $average);
// reset
$cache = 0;
$cacheRows = 0;
$oldTimestamp = $row['ts'];
}
// Chart Types
if ($chartType == "price"){
$cache += floatval($row['price']);
} elseif ($chartType == "sentiment") {
$cache += floatval($row['isbuy']);
} elseif ($chartType == "volume") {
$cache += floatval($row['volume']);
}
$cacheRows += 1;
}
}
?>
<!-- D3 Charts -->
<svg class="line-chart col"></svg>
<script type="text/javascript">
/**
* Loading data from API when DOM Content has been Loaded'.
*/
document.addEventListener("DOMContentLoaded", function(event){
drawChart(<?php echo json_encode($plotData); ?>);
});
/**
* Creates a chart using D3
* @param {object} data Object containing historical data of BPI
*/
function drawChart(data){
var svgWidth = 1080, svgHeight = 720;
var margin = { top: 20, right: 20, bottom: 30, left: 50};
var width = svgWidth - margin.left - margin.right;
var height = svgHeight -margin.top - margin.bottom;
var svg = d3.select("svg")
.attr("width", svgWidth)
.attr("height", svgHeight);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," +margin.top +")");
var x = d3.scaleTime()
.rangeRound([0,width]);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
var line = d3.line()
.x(function(d) { return x(parseFloat(d.x))})
.y(function(d) { return y(parseFloat(d.y))})
x.domain(d3.extent(data, function(d) {return parseFloat(d.x)}));
y.domain(d3.extent(data, function(d) {return parseFloat(d.y)}));
g.append("g")
.attr("transform", "translate(0,"+height+")")
.call(d3.axisBottom(x))
.select(".domain")
.remove();
g.append("g")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Price ($)");
g.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", line);
}
</script>
<!-- Trades PHP Table -->
<?php
if ($pair1 or $pair2){
echo "<div class='row container'>";
echo "<h2>".$pair1."-".$pair2."</h2>";
echo "<h6 class='m-2 text-muted'>".ucfirst($chartType)." chart,</h6>";
echo "<h6 class='m-2 text-muted'>".count($plotData)." plot points,</h6>";
echo "<h6 class='m-2 text-muted'>Viewing ".$rowLimit." trades per page,</h6>";
echo "<h6 class='m-2 text-muted'>Points are ".$chartType." average over ".$timeGranularity." seconds</h6>";
echo "</div>";
echo "<table class='table table-dark table-striped table-bordered table-sm table-hover'>";
echo "<thead class='thead-light'>";
echo "<tr>";
echo "<th scope='col'>Exchange</th>";
echo "<th scope='col'>Pair 1</th>";
echo "<th scope='col'>Pair 2</th>";
echo "<th scope='col'>Price</th>";
echo "<th scope='col'>Volume</th>";
echo "<th scope='col'>Buy</th>";
echo "<th scope='col'>Limit</th>";
echo "<th scope='col'>Exchange Timestamp</th>";
echo "<th scope='col'>Server Timestamp</th>";
echo "</tr>";
echo "</thead>";
$counter = 0;
$plotData = [];
echo "<tbody>";
while ($row = $result->fetchArray()){
// Output to Trades Table
if($counter < $rowLimit){
echo "<tr>";
echo "<td>". $row['exchange'] ."</td>";
echo "<td>". $row['pair1'] ."</td>";
echo "<td>". $row['pair2'] ."</td>";
echo "<td>". $row['price'] ."</td>";
echo "<td>". $row['volume'] ."</td>";
echo "<td>". $row['isbuy'] ."</td>";
echo "<td>". $row['islimit'] ."</td>";
echo "<td>". $row['exchangets'] ."</td>";
echo "<td>". $row['ts'] ."</td>";
echo "</tr>";
}
// Plot Data
$plotData[] = array("ts" => $row['ts'],"price" => $row['price']);
$counter += 1;
}
echo "</tbody>";
echo "</table>";
}
?>
</body>
</html>