-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
binance-fetch-ohlcv-many-symbols-since.js
50 lines (41 loc) · 1.88 KB
/
binance-fetch-ohlcv-many-symbols-since.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
"use strict";
const ccxt = require ('../../ccxt.js')
async function fetchOHLCVSince (exchange, symbol, timeframe, since) {
let ohlcvs = []
while (true) {
try {
const response = await exchange.fetchOHLCV (symbol, timeframe, since)
if (response.length) {
const firstCandle = exchange.safeValue (response, 0)
const lastCandle = exchange.safeValue (response, response.length - 1)
const firstTimestamp = exchange.safeInteger (firstCandle, 0)
const lastTimestamp = exchange.safeInteger (lastCandle, 0)
const firstDatetime = exchange.iso8601 (firstTimestamp)
const lastDatetime = exchange.iso8601 (lastTimestamp)
const currentDatetime = exchange.iso8601 (exchange.milliseconds ())
since = lastTimestamp + 1
ohlcvs = ohlcvs.concat (response)
console.log (currentDatetime, symbol, timeframe, 'fetched', response.length, 'candles since', firstDatetime, 'till', lastDatetime, 'total', ohlcvs.length)
} else {
break
}
} catch (e) {
console.log (e.constructor.name, e.message)
}
}
return { symbol, ohlcvs }
}
async function main () {
const exchange = new ccxt.binance()
const markets = await exchange.loadMarkets ()
const timeframe = '5m'
const firstOneHundredSymbols = exchange.symbols.slice (0, 10)
const now = exchange.milliseconds ()
const oneWeek = 60 * 60 * 24 * 7 * 1000 // milliseconds
const since = now - oneWeek
const allCandles = await Promise.all (firstOneHundredSymbols.map (symbol => fetchOHLCVSince (exchange, symbol, timeframe, since)))
const allCandlesBySymbol = exchange.indexBy (allCandles, 'symbol')
console.log ('Fetched', Object.keys (allCandlesBySymbol))
console.log ('Done')
}
main ()