-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest-ajaxdata.php
569 lines (494 loc) · 18.2 KB
/
test-ajaxdata.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<?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' );
ini_set('memory_limit', '1G'); // just in case.
date_default_timezone_set ( 'UTC' ); // all dates expressed in UTC.
$market = @$_GET['market'];
$allmarkets = @$_GET['allmarkets'];
try {
// get list of markets.
$marketservice = new markets();
$markets_result = $allmarkets ? $marketservice->get_markets() : $marketservice->get_markets_with_trades();
// Default to eur market for now.
if( !$market || !@$markets_result[$market]) {
$market = "eur_btc";
}
$market_name = strtoupper( str_replace( '_', '/', $market));
$currmarket = $markets_result[$market];
// Obtain market summary info for today only.
$summarize_trades = new summarize_trades();
$market_result = $summarize_trades->get_trade_summaries_days( ['market' => $market,
'datetime_from' => strtotime( 'today 00:00:00' ),
'datetime_to' => strtotime( 'today 23:59:00' ),
'limit' => 1
] );
// create market select control.
$allparam = $allmarkets ? '&allmarkets=1' : '';
$market_select = sprintf( "<select onchange='document.location.replace(\"?market=\" + this.options[this.selectedIndex].value+\"%s\")'>%s\n", $allparam, $market );
foreach( $markets_result as $id => $m ) {
$market_select .= sprintf( "<option value=\"%s\"%s>%s</option>\n", $id, $id == $market ? ' selected' : '', strtoupper( str_replace('_', '/', $id )) );
}
$market_select .= "</select>\n";
$latest = @$market_result[0];
if( $latest ) {
$market_result = ['choose' => $market_select,
'market'=> $currmarket['name'],
'market_date'=> date('Y-m-d'),
'last'=> $latest['close'],
'high'=> $latest['high'],
'low'=> $latest['low'],
'avg'=> $latest['avg'],
'volume' => $latest['volume']
];
}
else {
$market_result = ['choose' => $market_select,
'market'=> $currmarket['name'],
'market_date'=> date('Y-m-d'),
'last'=> '--',
'high'=> '--',
'low'=> '--',
'avg'=> '--',
'volume' => '--'
];
}
// get latest trades.
$trades = new trades();
$trades_result = $trades->get_trades( [ 'market' => $market,
'limit' => 100 ] );
}
catch( Exception $e ) {
// for dev/debug.
_global_exception_handler( $e );
include(dirname(__FILE__) . '/404.html');
}
list( $curr_left, $curr_right ) = explode( '_', $market, 2);
$table = new html_table();
$table->timestampjs_col_names['tradeDate'] = true;
function display_crypto($val, $row) {
return $val / 100000000;
}
function display_fiat($val, $row) {
return $val / 10000;
}
function display_cryptotimesfiat($val, $row) {
return $val / 1000000000000;
}
$amcharts_cdn = 'https://www.amcharts.com/lib/3';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<?php include( dirname(__FILE__) . '/widgets/head.html' ); ?>
<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 {
display: block;
max-height: 30em;
height: 30em;
overflow-y: scroll;
}
#sell_orders td, #buy_orders td, #trade_history td {
width: 100%;
}
#container {
height: 500px;
}
#market_info td {
text-align: center;
}
</style>
</head>
<body>
<?php $table->table_attrs = array( 'class' => 'bordered', 'id' => 'market_info', 'style' => 'width: 800px' ); ?>
<?= $table->table_with_header( array( $market_result ),
array( 'Choose', 'Market', 'Date', "Last", "High", "Low", "Avg", "Volume" ),
array( 'choose', 'market', 'market_date', 'last', 'high', 'low', 'avg', 'volume' ) ); ?>
<?php if( !count( $trades_result ) ): ?>
<div class="widget" style="margin-top: 15px; text-align: center;">
There have been no trades in this market recently. You can get the ball rolling by placing an order now.
</div>
<?php else: ?>
<div class='widget' style="margin-top: 15px;">
<div id="container"></div>
</div>
<table width="100%" cellpadding="0" cellspacing="0"><tr><td><h3>Trade History</h3></td><td align="right">( Last <?= count($trades_result) ?> trades )</td></tr></table>
<?php $table->table_attrs = array( 'class' => 'bordered', 'id' => 'trade_history', 'style' => 'width: 800px' ); ?>
<div id="trade_history_scroll">
<?= $table->table_with_header( $trades_result,
array( 'Date', 'Action', 'Price', 'Size', 'Total' ),
array( 'tradeDate',
'direction',
'tradePrice' => ['cb_format' => 'display_fiat'],
'tradeAmount' => ['cb_format' => 'display_crypto'],
'total' => ['cb_format' => 'display_cryptotimesfiat'] ) ); ?>
</div>
<script type="text/javascript">
createStockChart();
function createStockChart() {
/*
$(function () {
// $.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?', function (data) {
$.getJSON('api/hloc?market=<?= $market ?>×tamp=no&format=jscallback&callback=?', function (data) {
// $.getJSON('api/hloc/?market=dash_btc&callback=?', function (data) {
// split the data set into ohlc and volume
var ohlc = [],
volume = [],
dataLength = data.length,
// set the allowed units for data grouping
groupingUnits = [[
'week', // unit name
[1] // allowed multiples
], [
'month',
[1, 2, 3, 4, 6]
]],
i = 0;
for (i; i < dataLength; i += 1) {
ohlc.push([
data[i][0], // the date
data[i][1], // open
data[i][2], // high
data[i][3], // low
data[i][4] // close
]);
volume.push([
data[i][0], // the date
data[i][5] // the volume
]);
}
// create the chart
$('#container').highcharts('StockChart', {
rangeSelector: {
selected: 1
},
title: {
text: 'AAPL Historical'
},
yAxis: [{
labels: {
align: 'right',
x: -3
},
title: {
text: 'OHLC'
},
height: '60%',
lineWidth: 2
}, {
labels: {
align: 'right',
x: -3
},
title: {
text: 'Volume'
},
top: '65%',
height: '35%',
offset: 0,
lineWidth: 2
}],
series: [{
type: 'candlestick',
name: 'AAPL',
data: ohlc,
dataGrouping: {
units: groupingUnits
}
}, {
type: 'column',
name: 'Volume',
data: volume,
yAxis: 1,
dataGrouping: {
units: groupingUnits
}
}]
});
});
});
*/
function get_interval(start, end) {
// Note: this logic mirrors the logic in api/hloc/index.php
var range = (end - start) / 1000; // in seconds.
console.log(range);
if(range <= 3600) {
// up to one hour range loads minutely data ( 60 / hour )
return 'minutes';
}
else if(range <= 1 * 24 * 3600) {
// up to one day range loads half-hourly data ( 48 / day )
return 'half_hours';
}
else if(range <= 3 * 24 * 3600) {
// up to 3 day range loads hourly data ( 72 / 3 days)
return 'hours';
}
else if(range <= 7 * 24 * 3600) {
// up to 7 day range loads half-daily data ( 84 / week )
return 'half_days';
}
else if(range <= 60 * 24 * 3600) {
// up to 2 month range loads daily data ( 48 / 2 months )
return 'days';
}
else if(range <= 12 * 31 * 24 * 3600) {
// up to one year range loads weekly data ( 52 / year )
return 'weeks';
}
else if(range <= 12 * 31 * 24 * 3600) {
// up to 5 year range loads monthly data ( 60 / 5 years )
return 'months';
}
else {
// greater range loads yearly data
return 'years';
}
}
function get_point_interval(start, end) {
var interval = get_interval(start, end);
switch( interval ) {
case 'minutes': return 60 * 1000;
case 'half_hours': return 1800 * 1000;
case 'hours': return 3600 * 1000;
case 'half_days': return 3600 * 12 * 1000;
case 'days': return 24 * 3600 * 1000;
case 'weeks': return 24 * 7 * 3600 * 1000;
case 'months': return 24 * 3600 * 30 *1000;
case 'years': return 86400 * 365 * 1000;
default: return null;
}
}
function server_base_url(args) {
return 'api/hloc?market=<?= $market ?>&milliseconds=true×tamp=no&format=jscallback&fillgaps=<?= @$_GET['fillgaps'] ?>&callback=?';
}
function server_url(from, to, interval) {
return server_base_url() + '×tamp_from=' + Math.round(from) + '×tamp_to=' + Math.round(to) + "&interval=" + interval;
}
function polling_time() {
// 5 minutes.
return 5 * 60 * 1000;
}
function requestData(e) {
var chart = $('#container').highcharts();
if ( e == null ) {
e = {min: chart.xAxis[0].min, max: chart.xAxis[0].max};
}
chart.showLoading('Loading data from server...');
$.getJSON( server_url( e.min, e.max, ''), function (data) {
var ohlc = [],
volume = [],
avg = [],
dataLength = data.length,
pointInterval;
for (i = 0; i < dataLength; i++) {
ohlc.push([
data[i][0], // the date
data[i][1], // open
data[i][2], // high
data[i][3], // low
data[i][4] // close
]);
avg.push([
data[i][0], // the date
data[i][6] // the average
]);
volume.push([
data[i][0], // the date
data[i][5] // the volume
])
}
chart.series[0].setData(ohlc);
chart.series[1].setData(avg);
chart.series[2].setData(volume);
chart.hideLoading();
// refresh after 5 minutes
setTimeout(requestData, polling_time());
});
}
$(function () {
/**
* Load new data depending on the selected min and max
*/
function afterSetExtremes(e) {
requestData(e);
}
// refresh after 5 minutes
setTimeout(requestData, polling_time());
// See source code from the JSONP handler at https://github.com/highcharts/highcharts/blob/master/samples/data/from-sql.php
var url = server_url( new Date('2016-01-01').getTime(), new Date().getTime(), 'day' );
$.getJSON(url, function (data) {
var ohlc = [],
volume = [],
avg = [],
dataLength = data.length;
for (i = 0; i < dataLength; i++) {
ohlc.push([
data[i][0], // the date
data[i][1], // open
data[i][2], // high
data[i][3], // low
data[i][4] // close
]);
avg.push([
data[i][0], // the date
data[i][6] // the average
]);
volume.push([
data[i][0], // the date
data[i][5] // the volume
]);
}
// create the chart
$('#container').highcharts('StockChart', {
chart : {
type: 'candlestick',
zoomType: 'x',
},
plotOptions: {
candlestick: {
color: 'red',
upColor: 'green',
pointWidth: 10,
},
column: {
pointWidth: 10,
}
/*,
spline: {
connectNulls: false
// pointWidth: 10,
} */
},
yAxis: [{
title: {
text: 'Price (<?= $market_name ?>)'
},
height: 200,
lineWidth: 2
}, {
title: {
text: 'Volume'
},
top: 290,
height: 95,
offset: 0,
lineWidth: 2
}],
navigator : {
adaptToUpdatedData: false,
series : {
data : data
}
},
scrollbar: {
liveRedraw: true
},
title: {
text: '<?= $market_name ?> Price History'
},
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 date_format;
var interval = get_interval( chart.xAxis[0].min, chart.xAxis[0].max );
console.log( interval, chart.xAxis[0].min, chart.xAxis[0].max );
switch( interval ) {
case 'minutes': date_format = '%B %e, %Y - %l:%M %p'; break;
case 'half_hours': date_format = '%B %e, %Y - %l:%M %p'; break;
case 'hours': date_format = '%B %e, %Y - %l %p'; break;
case 'half_days': date_format = '%B %e, %Y - %l %p'; break;
case 'days': date_format = '%B %e, %Y'; break;
case 'weeks': date_format = 'Week of %B %e, %Y'; break;
case 'months': date_format = '%B %Y'; break;
case 'years': date_format = '%Y'; break;
}
txt += '<span style="font-size: 10px"><b>' + Highcharts.dateFormat( date_format, point.x) + '</b></span><br/>';
var empty_buf = txt + "No trades";
var found = false;
each(points, function(p, i) {
if(p.point && p.point.open) {
txt += 'Open: ' + p.point.open + '<br/>High: ' + p.point.high + '<br/>Low: ' + p.point.low + '<br/>Close: ' + p.point.close + '<br/><br/>';
found = true;
} else {
txt += p.series.name + ': ' + p.y + '<br/>';
}
});
return found ? txt : empty_buf;
}
},
rangeSelector : {
buttons: [{
type: 'hour',
count: 1,
text: '1h'
}, {
type: 'day',
count: 1,
text: '1d'
}, {
type: 'month',
count: 1,
text: '1m'
}, {
type: 'year',
count: 1,
text: '1y'
}, {
type: 'all',
text: 'All'
}],
inputEnabled: false, // it supports only days
selected : 2 // month
},
xAxis : {
ordinal: false,
events : {
afterSetExtremes : afterSetExtremes
},
minRange: 3600 * 1000, // one hour
},
series: [
{
type: 'candlestick',
name: 'ohlc',
data: ohlc,
dataGrouping: {
enabled: false
}
},
{
type: 'spline',
name: 'Average',
data: avg,
dataGrouping: {
enabled: false
}
},
{
type: 'column',
name: 'Volume',
data: volume,
yAxis: 1,
dataGrouping: {
enabled: false
}
}
]
});
});
});
}
</script>
<?php endif; ?>
</body>
</html>