candlestick-convert
Version:
OHLCV Candlestick converter and batcher with Typescript support
167 lines • 7.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.batchCandlesJSON = exports.batchCandlesWithCustomInterval = exports.batchCandles = void 0;
const interfaces_1 = require("../interfaces");
const batchCandles = (candleData, baseFrame = 60, newFrame = 300, includeOpenCandle = false) => {
const result = [];
baseFrame *= 1000;
newFrame *= 1000;
const convertRatio = Math.floor(newFrame / baseFrame);
if (convertRatio % 1 !== 0) {
throw new Error("Convert ratio should integer an >= 2");
}
if (Array.isArray(candleData)) {
if (candleData.length == 0 || candleData.length < convertRatio) {
return result;
}
}
else {
throw new Error("candleData is empty or not an array!");
}
// Sort Data to ascending by Time
candleData.sort((a, b) => a[interfaces_1.OHLCVField.TIME] - b[interfaces_1.OHLCVField.TIME]);
// Buffer values
let open = 0;
let high = 0;
let close = 0;
let low = 0;
let volume = 0;
let timeOpen = null;
let j = 0;
for (let i = 0; i < candleData.length; i++) {
const candle = candleData[i];
// Type convert
candle[interfaces_1.OHLCVField.TIME] = Number(candle[interfaces_1.OHLCVField.TIME]);
candle[interfaces_1.OHLCVField.OPEN] = Number(candle[interfaces_1.OHLCVField.OPEN]);
candle[interfaces_1.OHLCVField.HIGH] = Number(candle[interfaces_1.OHLCVField.HIGH]);
candle[interfaces_1.OHLCVField.LOW] = Number(candle[interfaces_1.OHLCVField.LOW]);
candle[interfaces_1.OHLCVField.CLOSE] = Number(candle[interfaces_1.OHLCVField.CLOSE]);
candle[interfaces_1.OHLCVField.VOLUME] = Number(candle[interfaces_1.OHLCVField.VOLUME]);
// First / Force New Candle
if (timeOpen === null) {
timeOpen = candle[interfaces_1.OHLCVField.TIME];
if (candle[interfaces_1.OHLCVField.TIME] % newFrame > 0) {
timeOpen =
candle[interfaces_1.OHLCVField.TIME] - (candle[interfaces_1.OHLCVField.TIME] % newFrame);
}
open = candle[interfaces_1.OHLCVField.OPEN];
high = candle[interfaces_1.OHLCVField.HIGH];
low = candle[interfaces_1.OHLCVField.LOW];
close = candle[interfaces_1.OHLCVField.CLOSE];
volume = 0;
j = 1;
}
// New Candle
if (candle[interfaces_1.OHLCVField.TIME] - (candle[interfaces_1.OHLCVField.TIME] % newFrame) !==
timeOpen) {
result.push([timeOpen, open, high, low, close, volume]);
timeOpen = candle[interfaces_1.OHLCVField.TIME] - (candle[interfaces_1.OHLCVField.TIME] % newFrame);
open = candle[interfaces_1.OHLCVField.OPEN];
high = candle[interfaces_1.OHLCVField.HIGH];
low = candle[interfaces_1.OHLCVField.LOW];
close = candle[interfaces_1.OHLCVField.CLOSE];
volume = 0;
j = 1;
}
high = Math.max(candle[interfaces_1.OHLCVField.HIGH], high);
low = Math.min(candle[interfaces_1.OHLCVField.LOW], low);
close = candle[interfaces_1.OHLCVField.CLOSE];
volume = volume + candle[interfaces_1.OHLCVField.VOLUME];
// Batch counter
if (j === convertRatio) {
result.push([timeOpen, open, high, low, close, volume]);
timeOpen = null;
}
else if (includeOpenCandle === true && i == candleData.length - 1) {
// Allow OpenCandles
result.push([timeOpen, open, high, low, close, volume]);
timeOpen = null;
}
j = j + 1;
}
return result;
};
exports.batchCandles = batchCandles;
const batchCandlesWithCustomInterval = (candleData, intervalFunction, includeOpenCandle = false) => {
const result = [];
if (Array.isArray(candleData)) {
if (candleData.length == 0) {
return result;
}
}
else {
throw new Error("candleData is empty or not an array!");
}
// Sort Data to ascending by Time
candleData.sort((a, b) => a[interfaces_1.OHLCVField.TIME] - b[interfaces_1.OHLCVField.TIME]);
// Buffer values
let open = 0;
let high = 0;
let close = 0;
let low = 0;
let volume = 0;
let timeOpen = null;
let j = 0;
for (let i = 0; i < candleData.length; i++) {
const candle = candleData[i];
// Type convert
candle[interfaces_1.OHLCVField.TIME] = Number(candle[interfaces_1.OHLCVField.TIME]);
candle[interfaces_1.OHLCVField.OPEN] = Number(candle[interfaces_1.OHLCVField.OPEN]);
candle[interfaces_1.OHLCVField.HIGH] = Number(candle[interfaces_1.OHLCVField.HIGH]);
candle[interfaces_1.OHLCVField.LOW] = Number(candle[interfaces_1.OHLCVField.LOW]);
candle[interfaces_1.OHLCVField.CLOSE] = Number(candle[interfaces_1.OHLCVField.CLOSE]);
candle[interfaces_1.OHLCVField.VOLUME] = Number(candle[interfaces_1.OHLCVField.VOLUME]);
// First / Force New Candle
if (timeOpen === null) {
timeOpen = intervalFunction(candle[interfaces_1.OHLCVField.TIME]);
open = candle[interfaces_1.OHLCVField.OPEN];
high = candle[interfaces_1.OHLCVField.HIGH];
low = candle[interfaces_1.OHLCVField.LOW];
close = candle[interfaces_1.OHLCVField.CLOSE];
volume = 0;
j = 1;
}
// New Candle
if (intervalFunction(candle[interfaces_1.OHLCVField.TIME]) !== timeOpen) {
result.push([timeOpen, open, high, low, close, volume]);
timeOpen = intervalFunction(candle[interfaces_1.OHLCVField.TIME]);
open = candle[interfaces_1.OHLCVField.OPEN];
high = candle[interfaces_1.OHLCVField.HIGH];
low = candle[interfaces_1.OHLCVField.LOW];
close = candle[interfaces_1.OHLCVField.CLOSE];
volume = 0;
j = 1;
}
high = Math.max(candle[interfaces_1.OHLCVField.HIGH], high);
low = Math.min(candle[interfaces_1.OHLCVField.LOW], low);
close = candle[interfaces_1.OHLCVField.CLOSE];
volume = volume + candle[interfaces_1.OHLCVField.VOLUME];
if (i === candleData.length - 1 && includeOpenCandle) {
result.push([timeOpen, open, high, low, close, volume]);
}
j = j + 1;
}
return result;
};
exports.batchCandlesWithCustomInterval = batchCandlesWithCustomInterval;
const batchCandlesJSON = (candleData, baseFrame = 60, newFrame = 300) => {
const ohlcvArray = candleData.map((e) => [
e.time,
e.open,
e.high,
e.low,
e.close,
e.volume,
]);
const batchedOhlcvArray = (0, exports.batchCandles)(ohlcvArray, baseFrame, newFrame);
return batchedOhlcvArray.map((candle) => ({
time: candle[interfaces_1.OHLCVField.TIME],
open: candle[interfaces_1.OHLCVField.OPEN],
high: candle[interfaces_1.OHLCVField.HIGH],
low: candle[interfaces_1.OHLCVField.LOW],
close: candle[interfaces_1.OHLCVField.CLOSE],
volume: candle[interfaces_1.OHLCVField.VOLUME],
}));
};
exports.batchCandlesJSON = batchCandlesJSON;
//# sourceMappingURL=batchCandle.js.map
;