-
Notifications
You must be signed in to change notification settings - Fork 0
/
createDataSet.js
100 lines (90 loc) · 3.16 KB
/
createDataSet.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
/* Produces about 600k elements JSON array in "row" format for later CSV conversion
(500 bars * 150 pairs * 8 intervals)
Takes about 5 mins with intel i5 */
require('dotenv').config({ path: 'variables.env' });
process.env.UV_THREADPOOL_SIZE = 128;
const moment = require('moment');
const async = require("async");
const fs = require('fs');
const intervals = ['1m', '3m', '5m', '15m', '30m', '1h', '4h', '1d'];
const date = moment().format('YYYY-MMM-D-H-mm').split('-');
const datehuman = `${date[2]}-${date[1]}-${date[0]}_${date[3]}h${date[4]}`;
let failedPairs = [];
let dataset = [];
let runsCount = 0;
let pairsCount = 0;
// create /dataSets if not there
if (!fs.existsSync('./dataSets')) fs.mkdirSync('./dataSets');
const binance = require('node-binance-api')().options({
APIKEY: process.env.APIKEY,
APISECRET: process.env.APISECRET,
useServerTime: true,
recvWindow: 3200000,
test: true
});
const queryData = ([pair, interval], cb) => {
binance.candlesticks(pair, interval, (err, ticks, symbol) => {
if (err) {
console.log(err);
failedPairs.push([pair, interval]); // keep track of errors
cb(null, `Error ${interval}, ${symbol}`);
return;
}
ticks.map(tick => {
dataset.push({
symbol,
interval,
time: parseFloat(tick[0]),
open: parseFloat(tick[1]),
high: parseFloat(tick[2]),
low: parseFloat(tick[3]),
close: parseFloat(tick[4]),
volume: parseFloat(tick[5])
});
});
process.stdout.write('\033c');
console.log(`Done: ${symbol}\t${interval}\t( ${(runsCount / (intervals.length * pairsCount) * 100).toFixed(1)}% )`);
runsCount++;
cb(null, `Done ${interval}, ${symbol}`);
});
};
// Save to .json
const saveDatasetToFile = () => {
fs.writeFile(`./dataSets/dataset_${datehuman}.json`, JSON.stringify(dataset), () => {
console.log('File saved, refresh explorer.');
process.exit(0);
});
};
// Loop queries asynchronously in series with parameters
const queryLoop = params => {
async.mapSeries(params, queryData, () => {
console.log(failedPairs); // re-run erroed pairs
if (failedPairs.length > 0) {
queryLoop(failedPairs);
failedPairs = [];
} else saveDatasetToFile();
});
};
// prepare all parameters [[pair, interval], ...]
const makeArr = pairs => {
const params = [];
pairs.map(pair => {
dataset[pair] = {};
intervals.map(interval => {
if (!dataset[pair][interval]) dataset[pair][interval] = {};
params.push([pair, interval]);
});
});
queryLoop(params);
};
// Get exchange infos and list of pairs at
// https://api.binance.com/api/v1/exchangeInfo
binance.exchangeInfo((error, data) => {
if (error) console.error(error);
fs.writeFileSync('./exchangeInfos.json', JSON.stringify(data, null, 4));
const allPairs = data.symbols
.filter(pair => pair.quoteAsset == 'BTC')
.map(pair => pair.symbol);
pairsCount = allPairs.length;
makeArr(allPairs);
});