UNPKG

hft-js

Version:

High-Frequency Trading in Node.js

106 lines 4.03 kB
"use strict"; /* * utils.ts * * Copyright (c) 2025 Xiongfei Shi * * Author: Xiongfei Shi <xiongfei.shi(a)icloud.com> * License: Apache-2.0 * * https://github.com/shixiongfei/hft.js */ 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mergeBarData = exports.getBarVolume = exports.getBarSellVolume = exports.getBarBuyVolume = exports.parseSymbol = void 0; var parseSymbol = function (symbol) { var _a = symbol.split("."), instrumentId = _a[0], exchangeId = _a[1]; return [instrumentId, exchangeId]; }; exports.parseSymbol = parseSymbol; var getBarBuyVolume = function (bar, price) { var _a; return (_a = bar.buyVolumes[price]) !== null && _a !== void 0 ? _a : 0; }; exports.getBarBuyVolume = getBarBuyVolume; var getBarSellVolume = function (bar, price) { var _a; return (_a = bar.sellVolumes[price]) !== null && _a !== void 0 ? _a : 0; }; exports.getBarSellVolume = getBarSellVolume; var getBarVolume = function (bar, price) { return (0, exports.getBarBuyVolume)(bar, price) + (0, exports.getBarSellVolume)(bar, price); }; exports.getBarVolume = getBarVolume; var mergeBarData = function (bars) { var _a, _b; if (bars.length === 0) { throw new Error("Bars is empty"); } if (bars.length === 1) { return bars[1]; } var bar = { symbol: bars[0].symbol, date: bars[0].date, time: bars[0].time, openInterest: bars[0].openInterest, openPrice: bars[0].openPrice, highPrice: bars[0].highPrice, lowPrice: bars[0].lowPrice, closePrice: bars[0].closePrice, volume: bars[0].volume, amount: bars[0].volume, delta: bars[0].delta, poc: bars[0].poc, buyVolumes: __assign({}, bars[0].buyVolumes), sellVolumes: __assign({}, bars[0].sellVolumes), }; for (var i = 1; i < bars.length; ++i) { var nextBar = bars[i]; bar.openInterest = nextBar.openInterest; bar.closePrice = nextBar.closePrice; bar.highPrice = Math.max(bar.highPrice, nextBar.highPrice); bar.lowPrice = Math.min(bar.lowPrice, nextBar.lowPrice); bar.volume += nextBar.volume; bar.amount += nextBar.amount; for (var price in nextBar.buyVolumes) { var volumeDelta = nextBar.buyVolumes[price]; if (price in bar.buyVolumes) { bar.buyVolumes[price] += volumeDelta; } else { bar.buyVolumes[price] = volumeDelta; } bar.delta += volumeDelta; var priceVP = bar.buyVolumes[price] + ((_a = bar.sellVolumes[price]) !== null && _a !== void 0 ? _a : 0); var pocVP = (0, exports.getBarVolume)(bar, bar.poc); if (priceVP > pocVP) { bar.poc = parseFloat(price); } } for (var price in nextBar.sellVolumes) { var volumeDelta = nextBar.sellVolumes[price]; if (price in bar.sellVolumes) { bar.sellVolumes[price] += volumeDelta; } else { bar.sellVolumes[price] = volumeDelta; } bar.delta -= volumeDelta; var priceVP = bar.sellVolumes[price] + ((_b = bar.buyVolumes[price]) !== null && _b !== void 0 ? _b : 0); var pocVP = (0, exports.getBarVolume)(bar, bar.poc); if (priceVP > pocVP) { bar.poc = parseFloat(price); } } } Object.freeze(bar.buyVolumes); Object.freeze(bar.sellVolumes); return Object.freeze(bar); }; exports.mergeBarData = mergeBarData; //# sourceMappingURL=utils.js.map