binance-historical
Version:
Download historical klines from binance api
79 lines (78 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatDate = exports.saveKline = void 0;
exports.intervalToSeconds = intervalToSeconds;
exports.calculateNumberOfCall = calculateNumberOfCall;
exports.divideInterval = divideInterval;
const fs = require("fs");
const saveKline = (fileName, jsonArray) => {
fs.writeFileSync(fileName, JSON.stringify(jsonArray, null, 2));
};
exports.saveKline = saveKline;
const formatDate = (date, withHour = false) => {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
return withHour
? `${year}-${month}-${day} ${hour}:${minute}:${second}`
: `${year}-${month}-${day}`;
};
exports.formatDate = formatDate;
function intervalToSeconds(interval) {
switch (interval) {
case '1m':
return 60;
case '3m':
return 180;
case '5m':
return 300;
case '15m':
return 900;
case '30m':
return 1800;
case '1h':
return 3600;
case '2h':
return 7200;
case '4h':
return 14400;
case '6h':
return 21600;
case '8h':
return 28800;
case '12h':
return 43200;
case '1d':
return 86400;
case '3d':
return 259200;
case '1w':
return 604800;
default:
return 0;
}
}
function calculateNumberOfCall(interval, startTime, endTime) {
const intervalSeconds = intervalToSeconds(interval);
const start = Math.floor(startTime / intervalSeconds) * intervalSeconds;
const end = Math.floor(endTime / intervalSeconds) * intervalSeconds;
const diff = end - start;
const optimal = Math.ceil(diff / intervalSeconds);
const result = Math.ceil(optimal / 1000);
if (isNaN(result)) {
return 0;
}
return result;
}
function divideInterval(numberOfDivisions, startDate, endDate) {
const interval = (endDate - startDate) / numberOfDivisions;
const divisions = [];
for (let i = 0; i < numberOfDivisions; i++) {
divisions.push(Math.round(startDate + interval * i));
}
divisions.push(endDate);
return divisions;
}