-
Notifications
You must be signed in to change notification settings - Fork 1
/
index_slow.js
106 lines (89 loc) · 3.02 KB
/
index_slow.js
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
const https = require('https')
const fs = require('fs')
async function run() {
const args = process.argv.slice(2)
// process command line arguments
const book = args[0]
const fileName = args[1]
const from = new Date(args[2]).getTime()
const to = new Date(args[3]).getTime()
const tf = Number(args[4])
const tfms = tf * 1000
let finished = false
let currentTime = null
let marker = null
let lines = ["Time UTC,Open,High,Low,Close,Volume"]
let open = null, high = null, low = null, volume = 0, close = null
while (!finished) {
const data = await getData(book, marker)
if (data && data.success) {
if (currentTime === null && data.payload.length > 0) {
const time = new Date(data.payload[0].created_at).getTime()
currentTime = time - (time % tfms)
}
console.log(new Date(currentTime).toISOString())
for (let i = 0; i < data.payload.length; i++) {
const order = data.payload[i]
const price = Number(order.price)
const time = new Date(data.payload[i].created_at).getTime()
if (time >= currentTime) {
if (high === null || high < price) high = price
if (low === null || low > price) low = price
if (close === null) close = price
if (time === currentTime) open = price
volume += Number(order.amount)
} else {
const line = `${new Date(currentTime).toISOString()},${open || price},${Math.max(open || price, high)},${Math.min(open || price, low)},${close},${volume.toFixed(8) }`
lines.push(line)
currentTime -= tfms
while(time < currentTime) {
const line = `${new Date(currentTime).toISOString()},${price},${price},${price},${price},0`
lines.push(line)
currentTime -= tfms
}
open = null, high = null, low = null, volume = 0, close = null
i--
}
marker = order.tid
}
// if no more data is delivered or current time before from - finish
if (data.payload.length === 0 || (currentTime < from)) {
finished = true
}
// wait 1 second for bitso rate limit
await delay(1000)
} else {
console.log("Error fetching data", data)
finished = true
}
}
fs.writeFileSync(fileName, lines.join('\n'))
}
async function getData(book, marker) {
return new Promise((resolve, reject) => {
const req = https.request({
hostname: 'bitso.com',
path: `/api/v3/trades/?book=${book}&limit=100${marker ? '&marker=' + marker : ''}`,
port: 443,
method: 'GET'
}, (res) => {
let str = ''
res.on('data', function (chunk) {
str += chunk
})
res.on('error', (err) => {
reject(err)
})
res.on('end', function () {
resolve(JSON.parse(str))
})
})
req.end()
})
}
async function delay(millis) {
return new Promise((resolve, reject) => {
setTimeout(resolve, millis)
})
}
run()