candlestick-to-png
Version:
Lightweight library to draw an array of candles into a canvas and render it as png file.
164 lines (163 loc) • 8 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchCandles = exports.candleStickToPNG = void 0;
var candlestickChart_1 = require("./utils/candlestickChart");
var fs = require("fs");
var binance_api_node_1 = require("binance-api-node");
var canvas_1 = require("@napi-rs/canvas");
var client = (0, binance_api_node_1.default)();
function executeOnCanvas(createCanvas, candleArray, moves, candleStickGraphOptions, candleChartColors, fileName) {
var canvas = createCanvas(800, 800);
canvas_1.GlobalFonts.registerFromPath("".concat(__dirname, "/fonts/gilroy/gilroy-bold-webfont.woff"), 'gilroy-bold');
canvas_1.GlobalFonts.registerFromPath("".concat(__dirname, "/fonts/gilroy/gilroy-semibold-webfont.woff"), 'gilroy-semibold');
var basicOptions = __assign({ granularity: 1, wantBollingerBands: false, wantCandles: true, wantEMA: true, wantLines: true, wantMACD: false, wantRSI: false, wantSMA: false, wantTrades: true, zoom: 0, zoomSpeed: 0, wantGrid: false, wantStats: true, triangleSize: 10, lineWidth: 1, lineWidthGreen: 1, lineWidthRed: 1, wantSideMarksMove: false, baseFontName: 'gilroy-bold' }, candleStickGraphOptions);
var colors = __assign({ gridColor: "rgb(24,24,24)", gridTextColor: "#ffffff", mouseHoverBackgroundColor: "#84817a", lineColor: "rgba(238,238,238,0.34)", mouseHoverTextColor: "#000000", greenColor: "#77E15E", redColor: "#E94334FF", greenHoverColor: "#77E15E", redHoverColor: "#E94334FF", debugLineColor: "#D11538", growLineColor: "rgba(255,255,255,0.67)", blackColor: "#000000", whiteColor: "#eeeeee", yellowColor: "#f9ca24", purpleColor: "#e056fd", purpleColorTransparent: "rgba(217,86,253,0.21)", yellowColorTransparent: "rgba(253,206,86,0.2)", whiteColorTrasparent: 'rgba(255,255,255,0.55)', blueColor: '#4ac4e0', greenAreaColor: 'rgba(119,255,1,0.27)', redAreaColor: 'rgba(255,1,22,0.27)', greenAreaColorIntens: 'rgba(119,255,1,0.57)', redAreaColorIntes: 'rgba(255,1,22,0.57)', whiteColorMoreTrasparent: 'rgba(255,255,255,0.25)', logoColor: 'white' }, candleChartColors);
var gen = new candlestickChart_1.CandleStickGraph(basicOptions);
gen.applyColors(colors);
// @ts-ignore
gen.initCanvasHeadless(canvas);
gen.concatCandleSticks(candleArray);
moves.forEach(function (el) { return gen.addTrade(el); });
gen.draw();
var buffer = canvas.toBuffer("image/png");
if (fileName) {
fs.writeFileSync(fileName, buffer);
}
return buffer;
}
/**
* Return buffer
* @param candleArray
* @param moves
* @param candleStickGraphOptions
* @param candleChartColors
* @param customCanvas
* @param fileName
*/
var candleStickToPNG = function (candleArray, moves, candleStickGraphOptions, candleChartColors, customCanvas2, fileName) {
if (moves === void 0) { moves = []; }
if (candleStickGraphOptions === void 0) { candleStickGraphOptions = {}; }
if (candleChartColors === void 0) { candleChartColors = {}; }
try {
return executeOnCanvas(customCanvas2 ? function (a, b) { return customCanvas2; } : canvas_1.createCanvas, candleArray, moves, candleStickGraphOptions, candleChartColors, fileName);
}
catch (e) {
console.error(e);
}
return null;
};
exports.candleStickToPNG = candleStickToPNG;
var fetchCandles = function (asset, startMillis, endMillis) { return __awaiter(void 0, void 0, void 0, function () {
var desiredIntervalString;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
desiredIntervalString = calculateIntervalBasedOnDuration(startMillis, endMillis);
return [4 /*yield*/, client.candles({
interval: desiredIntervalString,
symbol: asset + 'USDT',
startTime: startMillis,
endTime: endMillis
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
}); };
exports.fetchCandles = fetchCandles;
function calculateIntervalBasedOnDuration(startMillis, endMillis) {
var minutesDuration = (endMillis - startMillis) / 1000 / 60;
var hoursDuration = minutesDuration / 60;
if (minutesDuration < 60) {
return '1m';
}
if (hoursDuration < 8) {
return '5m';
}
if (hoursDuration < 15) {
return '15m';
}
if (hoursDuration < 20) {
return '15m';
}
if (hoursDuration < 1.5 * 24) {
return '15m';
}
if (hoursDuration < 24 * 2) {
return '15m';
}
if (hoursDuration < 24 * 3) {
return '30m';
}
if (hoursDuration < 24 * 4) {
return '1h';
}
if (hoursDuration < 24 * 6) {
return '2h';
}
if (hoursDuration < 24 * 8) {
return '2h';
}
if (hoursDuration < 24 * 12) {
return '4h';
}
if (hoursDuration < 24 * 60) {
return '6h';
}
if (hoursDuration < 24 * 90) {
return '8h';
}
if (hoursDuration < 24 * 365) {
return '12h';
}
if (hoursDuration < 24 * 3 * 365) {
return '1d';
}
return '1w';
}