-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex-body-summary.php
421 lines (348 loc) · 14 KB
/
index-body-summary.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
require_once( dirname( __FILE__) . '/lib/html_table.class.php' );
require_once( dirname( __FILE__) . '/lib/trades.class.php' );
require_once( dirname( __FILE__) . '/lib/summarize_trades.class.php' );
require_once( dirname( __FILE__) . '/lib/markets.class.php' );
require_once( dirname( __FILE__) . '/lib/primary_market.class.php' );
require_once( dirname( __FILE__) . '/lib/currencies.class.php' );
ini_set('memory_limit', '1G'); // just in case.
date_default_timezone_set ( 'UTC' ); // all dates expressed in UTC.
try {
$market = 'all';
$market_name = sprintf('All Currencies (%s Base) ', $pmarket);
$currencies = new currencies($network);
$fiats = $currencies->get_all_fiat();
// Obtain market summary info for today only.
$market_result = ['pchoose' => $pmarket_select,
'choose' => $market_select,
'market'=> $market_name,
'market_date'=> 'All Time',
'open'=> '--',
'last'=> '--',
'high'=> '--',
'low'=> '--',
'avg'=> '--',
'volume_right' => '--'
];
// get latest trades.
$trades = new trades($network);
$trades_result = $trades->get_trades( [ 'market' => null,
'limit' => 100 ] );
}
catch( Exception $e ) {
// for dev/debug.
if( @$_GET['debug'] ) {
_global_exception_handler( $e );
}
include(dirname(__FILE__) . '/404.html');
}
$table = new html_table();
$table->htmlescape = false;
$table->timestampjs_col_names['tradeDate'] = true;
function ibs_basecurrency_meta($market, $use_basecurrency) {
global $markets_result;
global $pmarket;
$market = strtolower( str_replace( '/', '_', $market));
$currmarket = $markets_result[$market];
$base_is_left = $pmarket == $currmarket['lsymbol'];
$use_left = ($base_is_left && $use_basecurrency) ||
(!$base_is_left && !$use_basecurrency);
$symbol = $use_left ? $currmarket['lsymbol'] : $currmarket['rsymbol'];
$key = $use_left ? 'lprecision' : 'rprecision';
$precision = $currmarket[$key];
return [
'base_is_left' => $base_is_left,
'use_left' => $use_left,
'symbol' => $symbol,
'precision' => $precision,
];
}
function ibs_display_currency_pair( $val_left, $val_right, $market, $use_basecurrency, $is_int=true ) {
$meta = ibs_basecurrency_meta( $market, $use_basecurrency );
extract( $meta );
$val = $use_left ? $val_left : $val_right;
$val = $is_int ? $val / 100000000 : $val;
return number_format( $val, $precision ) . ' ' . $symbol;
}
function ibs_display_trade_price( $val, $row ) {
$meta = ibs_basecurrency_meta( $row['currencyPair'], $use_basecurrency = false );
extract( $meta );
$val = $val / 100000000;
return number_format( $val, $precision ) . ' ' . $symbol;
}
function ibs_display_tradeamount_base( $val, $row ) {
return ibs_display_currency_pair( $row['tradeAmount'], $row['tradeVolume'], $row['currencyPair'], true );
}
function ibs_display_tradeamount_other( $val, $row ) {
return ibs_display_currency_pair( $row['tradeAmount'], $row['tradeVolume'], $row['currencyPair'], false );
}
?>
<link rel="stylesheet" href="css/styles.css" type="text/css">
<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<?php require_once( dirname( __FILE__) . '/widgets/timezone-js.html' ); ?>
<style>
#trade_history_scroll {
display: block;
height: 30em;
overflow-y: auto;
border: solid 1px #ccc;
box-shadow: 0 2px 6px #ccc;
}
.offers {
height: 21em;
overflow-y: auto;
}
.offers {
text-align: right;
}
#container {
height: 500px;
}
#market_info td {
text-align: center;
}
</style>
<?php $table->table_attrs = array( 'class' => 'bordered', 'id' => 'market_info' ); ?>
<?= $table->table_with_header( array( $market_result ),
array( 'Base Currency', 'Currency', 'Market', 'Market Date (UTC)', "Open", "Last", "High", "Low", "Avg", "Volume" ),
array( 'pchoose', 'choose', 'market', 'market_date', 'open', 'last', 'high', 'low', 'avg', 'volume_right' ) ); ?>
<?php if( !count( $trades_result ) ): ?>
<div class="widget" style="margin-top: 15px; text-align: center;">
There have been no trades in this base currency recently.
</div>
<?php else: ?>
<div class='widget' style="margin-top: 15px;">
<div id="container"></div>
</div>
<table width="100%" cellpadding="0" cellspacing="0" class="unbordered"><tr><th>Trade History</th><th align="right">( Last <?= count($trades_result) ?> trades )</th></tr></table>
<?php $table->table_attrs = array( 'class' => 'bordered', 'id' => 'trade_history', 'style'=>"border: none; box-shadow: none" ); ?>
<div id="trade_history_scroll" style="margin-bottom: 20px">
<?= $table->table_with_header( $trades_result,
array( 'Date', 'Action', 'Price', 'Amount in ' . $pmarket, 'Amount' ),
array( 'tradeDate',
'direction' => ['cb_format' => function($val, $r) {return $val;}],
'tradePrice' => ['cb_format' => 'ibs_display_trade_price'],
'tradeAmount' => ['cb_format' => 'ibs_display_tradeamount_base'],
'tradeVolume' => ['cb_format' => 'ibs_display_tradeamount_other'] )
); ?>
</div>
<script>
createStockChart();
function createStockChart() {
function server_base_url() {
return 'api/volumes?basecurrency=<?= urlencode($pmarket) ?>&milliseconds=true×tamp=no&format=jscallback&fillgaps=<?= urlencode(@$_GET['fillgaps']) ?>&callback=?';
}
function server_url_args(from, to, interval) {
return server_base_url() + '×tamp_from=' + Math.round(from) + '×tamp_to=' + Math.round(to) + "&interval=" + interval;
}
function server_url() {
return server_url_args( new Date('2016-01-01').getTime(), new Date().getTime(), 'minute' );
}
function polling_time() {
// 5 minutes.
return 5 * 60 * 1000;
}
/**
* Request data from the server, add it to the graph and set a timeout
* to request again
*/
// call it again after 5 minutes
setTimeout(requestData, polling_time());
function requestData() {
$.getJSON(server_url(), function (data) {
var chart = $('#container').highcharts();
chart.showLoading('Loading data from server...');
var m = munge_data(data);
chart.series[0].setData(m.volume);
chart.series[1].setData(m.num_trades);
chart.hideLoading();
// call it again after 5 minutes
setTimeout(requestData, polling_time());
});
}
function munge_data(data) {
var m = {volume: [], num_trades: []};
for (i = 0; i < data.length; i++) {
m.volume.push([
data[i][0], // the date
data[i][1], // the volume
]);
m.num_trades.push([
data[i][0], // the date
data[i][2], // num_trades
]);
}
return m;
}
function sum_cb(series) {
var s = 0;
for(var i=0; i < series.length; i++) {
if(true) {
s += series[i] * 100000000;
}
}
return s / 100000000;
}
function ucfirst(s) {
return s && s[0].toUpperCase() + s.slice(1);
}
$(function () {
Highcharts.setOptions({
global: {
useUTC: false // we do not want user's localtime.
}
});
// See source code from the JSONP handler at https://github.com/highcharts/highcharts/blob/master/samples/data/from-sql.php
$.getJSON(server_url(), function (data) {
m = munge_data(data);
var groupingUnits = [
[
'hour', // unit name
[1] // allowed multiples
],
[
'day', // unit name
[1] // allowed multiples
],
[
'week', // unit name
[1] // allowed multiples
],
[
'month',
[1]
]
];
// create the chart
Highcharts.stockChart('container', {
chart: {
alignTicks: false
},
plotOptions: {
series: {
// limit the maximum column width.
maxPointWidth: 20
}
},
tooltip:{
formatter: function() {
var points = this.point ? Highcharts.splat(this.point) : this.points,
point = points[0],
each = Highcharts.each,
txt = '';
var chart = $('#container').highcharts();
var unit = chart.series[0].currentDataGrouping.unitName;
var date_format;
switch( unit ) {
case 'day': date_format = '%B %e, %Y'; break;
case 'week': date_format = '%B %e, %Y'; break;
case 'month': date_format = '%B %Y'; break;
case 'year': date_format = '%B %Y'; break;
default:
date_format = '%B %e, %Y - %l:%M %p';
}
// workaround display bug.
var emptyline = '<span style="visibility: hidden;">-</span><br/>';
txt += '<span style="font-size: 10px"><b>' + Highcharts.dateFormat( date_format, point.x) + '</b></span><br/>\n';
txt += emptyline;
var rprecision = 8;
each(points, function(p) {
// console.log(p.series.name);
console.log(p);
if( p.series.name == 'Volume' ) {
var curr = '<?= $pmarket ?>';
txt += "<b>" + p.series.name + '</b>: ' + Highcharts.numberFormat(p.y, rprecision, '.', ',') + " " + curr +'<br/>';
}
if( p.series.name == 'NumTrades' ) {
var curr = '<?= $pmarket ?>';
txt += "<b>" + 'Trades' + '</b>: ' + p.y +'<br/>';
}
});
txt += emptyline;
txt += '<b>Period</b>: ' + ucfirst(unit) + '<br/>';
return txt;
}
},
rangeSelector : {
buttons: [
{
type: 'hour',
count: 1,
text: '1h'
},
{
type: 'day',
count: 1,
text: '1d'
},
{
type: 'week',
count: 1,
text: '1w'
},
{
type: 'month',
count: 1,
text: '1m'
},
{
type: 'year',
count: 1,
text: '1y'
},
{
type: 'all',
text: 'All'
}],
inputEnabled: false, // it supports only days
selected : 3 // month
},
title: {
text: 'Bisq : All <?= $pmarket ?> Base Markets'
},
xAxis : {
// permit gaps in data.
ordinal: false
},
series: [
{
type: 'column',
name: 'Volume',
data: m.volume,
dataGrouping: {
units: groupingUnits,
approximation: sum_cb,
groupPixelWidth: 40,
enabled: true,
forced: true
},
},
{
type: 'spline',
name: 'NumTrades',
data: m.num_trades,
lineWidth: 2,
marker: {
enabled: null,
fillColor: "lightblue",
lineColor: "darkblue",
lineWidth: 1,
radius: 4
},
dataGrouping: {
units: groupingUnits,
approximation: sum_cb,
groupPixelWidth: 40,
enabled: true,
forced: true
}
}
]
});
});
});
}
</script>
<?php endif; ?>