-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
83 lines (63 loc) · 2.03 KB
/
index.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
// this code uses undocumented /v3/ohlc WS to directly download candles from api.bitso.com
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
const pageSize = 1000
let currentTime = from - (from % tfms)
let lines = ["Time UTC,Open,High,Low,Close,Volume,Trades"]
while (currentTime < to) {
// log progress
console.log(new Date(currentTime).toISOString())
const data = await getData(book, currentTime, Math.min(to, currentTime + (tfms * (pageSize - 1))), tf)
currentTime += tfms * pageSize
if (data && data.success) {
for (let i = 0; i < data.payload.length; i++) {
const p = data.payload[i]
const line = `${new Date(p.bucket_start_time).toISOString()},${p.first_rate},${p.max_rate},${p.min_rate},${p.last_rate},${p.volume},${p.trade_count}`
lines.push(line)
}
// wait 1 second for bitso rate limit
await delay(1000)
} else {
console.log("Error fetching data", data)
break
}
}
fs.writeFileSync(fileName, lines.join('\n'))
}
async function getData(book, currentTimeFrom, currentTimeTo, tf) {
return new Promise((resolve, reject) => {
const req = https.request({
hostname: 'bitso.com',
path: `/api/v3/ohlc?book=${book}&time_bucket=${tf}&start=${currentTimeFrom}&end=${currentTimeTo}`,
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()