-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-historic.php
87 lines (86 loc) · 2.08 KB
/
build-historic.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
<?php
require_once 'lib.php';
define('TEST', false);
try {
$products=$client->getProducts();
$productsById=[];
foreach ($products as $pro) {
$productsById[$pro['id']]=$pro;
}
}
catch(Exception $e) {
echo $e->getMessage();
exit;
}
// { pre sort and uniq
$cmd='sort data/LTC-'.$currency.'-historic | uniq > data/LTC-'.$currency.'-historic.tmp && mv data/LTC-'.$currency.'-historic.tmp data/LTC-'.$currency.'-historic';
`$cmd`;
// }
$l=`tail -1 data/LTC-$currency-historic`;
$from=0;
if ($l) {
$l=json_decode(trim($l), true);
if ($l && isset($l[0]) && $l[0]) {
$from=$l[0]+1;
}
}
if (!$from) {
$from=strtotime('2017-06-01 00:00:00');
}
do {
$data=$client->getProductHistoricRates('LTC-'.$currency, [
'start'=>date('c', $from),
'end'=>date('c', $from+3601),
'granularity'=>'60'
]);
$data=array_reverse($data);
if (count($data)) {
$fromChanged=0;
foreach ($data as $d) {
if ($d[0]>$from) {
$fromChanged=1;
$from=$d[0];
}
echo date('c', $d[0]).' '.json_encode($d)."\n";
file_put_contents('data/LTC-'.$currency.'-historic', json_encode($d)."\n", FILE_APPEND);
}
if ($fromChanged) {
$from+=1;
}
else {
$from+=3600;
}
}
else {
$from+=3600;
}
sleep(1);
} while($from<time()-3600);
echo "sorting and discarding duplicates\n";
$cmd='sort data/LTC-'.$currency.'-historic | uniq > data/LTC-'.$currency.'-historic.tmp && mv data/LTC-'.$currency.'-historic.tmp data/LTC-'.$currency.'-historic';
`$cmd`;
echo "filling in gaps\n";
$lines=file('data/LTC-'.$currency.'-historic');
$numLines=count($lines);
$newLines=[];
$line=json_decode($lines[0], true);
$newLines[]=trim($lines[0]);
$at=$line[0];
$lastline=$line;
for ($i=1; $i<$numLines; ++$i) {
$line=json_decode($lines[$i], true);
if ($line[0]==$at) { // duplicate time stamp
continue;
}
for ($j=$at+60; $j<$line[0]; $j+=60) { // fill in the minutes where nothing happened
$line2=$lastline;
$line2[0]=$j;
$line2=json_encode($line2);
$newLines[]=$line2;
}
$at=$line[0];
$lastline=$line;
$line=json_encode($line);
$newLines[]=$line;
}
file_put_contents('data/LTC-'.$currency.'-historic', join("\n", $newLines));