@aurox/ohlcv-helpers
Advanced tools
Comparing version 0.8.0 to 0.9.0
@@ -1,1 +0,1 @@ | ||
export * from './pricePointListToOHLCV'; | ||
export * from './pricePointListToOHLCV.js'; |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
(0, tslib_1.__exportStar)(require("./pricePointListToOHLCV"), exports); | ||
export * from './pricePointListToOHLCV.js'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { OHLCVTimeUnit, OHLCVDataItem } from '../types'; | ||
import { OHLCVTimeUnit, OHLCVDataItem } from '../types.js'; | ||
export interface PricePointListItem { | ||
@@ -3,0 +3,0 @@ timestamp: number; |
@@ -1,15 +0,12 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.pricePointListToOHLCV = void 0; | ||
const tslib_1 = require("tslib"); | ||
const avl_1 = (0, tslib_1.__importDefault)(require("avl")); | ||
const utils_1 = require("../utils"); | ||
function pricePointListToOHLCV(list, timeUnit, [rangeStart, rangeEnd], defaultValue = 0) { | ||
const avl = new avl_1.default(undefined, true); | ||
import AVLTreeModule from 'avl'; | ||
import { startOfTimeUnit, getTimeUnitDuration } from '../utils.js'; | ||
const AVLTree = AVLTreeModule.default; | ||
export function pricePointListToOHLCV(list, timeUnit, [rangeStart, rangeEnd], defaultValue = 0) { | ||
const avl = new AVLTree(undefined, true); | ||
for (const item of list) { | ||
avl.insert(item.timestamp, item); | ||
} | ||
const timeUnitDuration = (0, utils_1.getTimeUnitDuration)(timeUnit); | ||
const startOfInitialTimeUnit = (0, utils_1.startOfTimeUnit)(rangeStart, timeUnit); | ||
const startOfFinalTimeUnit = (0, utils_1.startOfTimeUnit)(rangeEnd, timeUnit); | ||
const timeUnitDuration = getTimeUnitDuration(timeUnit); | ||
const startOfInitialTimeUnit = startOfTimeUnit(rangeStart, timeUnit); | ||
const startOfFinalTimeUnit = startOfTimeUnit(rangeEnd, timeUnit); | ||
const result = []; | ||
@@ -64,3 +61,2 @@ let previousClose = defaultValue; | ||
} | ||
exports.pricePointListToOHLCV = pricePointListToOHLCV; | ||
//# sourceMappingURL=pricePointListToOHLCV.js.map |
@@ -1,2 +0,2 @@ | ||
import { OHLCVDataItem, OHLCVTimeUnit } from './types'; | ||
import { OHLCVDataItem, OHLCVTimeUnit } from './types.js'; | ||
export interface GapFillRangeOptions { | ||
@@ -3,0 +3,0 @@ lastKnownPrice?: number; |
@@ -1,13 +0,9 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.gapFillRange = void 0; | ||
const utils_1 = require("./utils"); | ||
function gapFillRange(timeUnit, range, data, options) { | ||
var _a, _b; | ||
const lastKnownPrice = (_a = options === null || options === void 0 ? void 0 : options.lastKnownPrice) !== null && _a !== void 0 ? _a : null; | ||
const backfill = (_b = options === null || options === void 0 ? void 0 : options.backfill) !== null && _b !== void 0 ? _b : null; | ||
import { getTimeUnitStartsInRange } from './utils.js'; | ||
export function gapFillRange(timeUnit, range, data, options) { | ||
const lastKnownPrice = options?.lastKnownPrice ?? null; | ||
const backfill = options?.backfill ?? null; | ||
if (data.length === 0) { | ||
const filler = lastKnownPrice !== null ? lastKnownPrice : (backfill === null || backfill === void 0 ? void 0 : backfill.strategy) === 'constant' ? backfill.value : null; | ||
const filler = lastKnownPrice !== null ? lastKnownPrice : backfill?.strategy === 'constant' ? backfill.value : null; | ||
if (filler !== null) { | ||
return (0, utils_1.getTimeUnitStartsInRange)(range[0], range[1], timeUnit, 'inclusive').map(timeUnitStart => ({ | ||
return getTimeUnitStartsInRange(range[0], range[1], timeUnit, 'inclusive').map(timeUnitStart => ({ | ||
timeUnitStart, | ||
@@ -27,5 +23,5 @@ timeOpen: timeUnitStart, | ||
const result = []; | ||
let carryBackwardRefs = (backfill === null || backfill === void 0 ? void 0 : backfill.strategy) === 'carry-backward' ? [] : null; | ||
let carryBackwardRefs = backfill?.strategy === 'carry-backward' ? [] : null; | ||
let lastKnownPriceCursor = lastKnownPrice; | ||
for (const timeUnitStart of (0, utils_1.getTimeUnitStartsInRange)(range[0], range[1], timeUnit, 'inclusive')) { | ||
for (const timeUnitStart of getTimeUnitStartsInRange(range[0], range[1], timeUnit, 'inclusive')) { | ||
const existing = mapping.get(timeUnitStart); | ||
@@ -59,3 +55,3 @@ if (existing) { | ||
} | ||
else if ((backfill === null || backfill === void 0 ? void 0 : backfill.strategy) === 'constant') { | ||
else if (backfill?.strategy === 'constant') { | ||
carried = { | ||
@@ -72,3 +68,3 @@ timeUnitStart, | ||
} | ||
else if ((backfill === null || backfill === void 0 ? void 0 : backfill.strategy) === 'carry-backward') { | ||
else if (backfill?.strategy === 'carry-backward') { | ||
carried = { | ||
@@ -84,3 +80,3 @@ timeUnitStart, | ||
}; | ||
carryBackwardRefs === null || carryBackwardRefs === void 0 ? void 0 : carryBackwardRefs.push(carried); | ||
carryBackwardRefs?.push(carried); | ||
} | ||
@@ -94,3 +90,2 @@ if (carried) { | ||
} | ||
exports.gapFillRange = gapFillRange; | ||
//# sourceMappingURL=gapFillRange.js.map |
@@ -1,7 +0,7 @@ | ||
export * from './types'; | ||
export * from './utils'; | ||
export * from './timeUnit'; | ||
export * from './OHLCVLiveAggregator'; | ||
export * from './reduceOHLCV'; | ||
export * from './converters'; | ||
export * from './gapFillRange'; | ||
export * from './converters/index.js'; | ||
export * from './types.js'; | ||
export * from './utils.js'; | ||
export * from './timeUnit.js'; | ||
export * from './OHLCVLiveAggregator.js'; | ||
export * from './reduceOHLCV.js'; | ||
export * from './gapFillRange.js'; |
@@ -1,11 +0,8 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
(0, tslib_1.__exportStar)(require("./types"), exports); | ||
(0, tslib_1.__exportStar)(require("./utils"), exports); | ||
(0, tslib_1.__exportStar)(require("./timeUnit"), exports); | ||
(0, tslib_1.__exportStar)(require("./OHLCVLiveAggregator"), exports); | ||
(0, tslib_1.__exportStar)(require("./reduceOHLCV"), exports); | ||
(0, tslib_1.__exportStar)(require("./converters"), exports); | ||
(0, tslib_1.__exportStar)(require("./gapFillRange"), exports); | ||
export * from './converters/index.js'; | ||
export * from './types.js'; | ||
export * from './utils.js'; | ||
export * from './timeUnit.js'; | ||
export * from './OHLCVLiveAggregator.js'; | ||
export * from './reduceOHLCV.js'; | ||
export * from './gapFillRange.js'; | ||
//# sourceMappingURL=index.js.map |
import { TypedEmitter } from 'tiny-typed-emitter'; | ||
import { OHLCVTimeUnit, OHLCVDataItem } from './types'; | ||
import { OHLCVTimeUnit, OHLCVDataItem } from './types.js'; | ||
export interface OHLCVAggregatorRecordLimitRetentionOptions { | ||
@@ -11,3 +11,3 @@ method: 'record-limit'; | ||
} | ||
export declare type OHLCVAggregatorRetentionOptions = OHLCVAggregatorRecordLimitRetentionOptions | OHLCVAggregatorTimeWindowRetentionOptions; | ||
export type OHLCVAggregatorRetentionOptions = OHLCVAggregatorRecordLimitRetentionOptions | OHLCVAggregatorTimeWindowRetentionOptions; | ||
export interface OHLCVAggregatorOptions { | ||
@@ -14,0 +14,0 @@ timeUnit: OHLCVTimeUnit; |
@@ -1,165 +0,14 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OHLCVLiveAggregator = void 0; | ||
const tslib_1 = require("tslib"); | ||
const tiny_typed_emitter_1 = require("tiny-typed-emitter"); | ||
const avl_1 = (0, tslib_1.__importDefault)(require("avl")); | ||
const utils_1 = require("./utils"); | ||
class OHLCVLiveAggregator extends tiny_typed_emitter_1.TypedEmitter { | ||
constructor(options) { | ||
var _a; | ||
super(); | ||
this._historyLoaded = false; | ||
this._preHistoryUpdateBuffer = null; | ||
this._data = new avl_1.default(undefined, true); | ||
this.resetHistory = () => { | ||
if (!this._historyEnabled) { | ||
throw new Error('History is disabled'); | ||
} | ||
this._historyLoaded = false; | ||
this.emit('history-reset'); | ||
}; | ||
this.setHistory = (history) => { | ||
if (!this._historyEnabled) { | ||
throw new Error('History is disabled'); | ||
} | ||
if (this._historyLoaded) { | ||
throw new Error('History cannot be loaded more than once, to reset the history first call resetHistory then setHistory'); | ||
} | ||
this._historyLoaded = true; | ||
this._data.clear(); | ||
for (const historyItem of history) { | ||
this.applyItem(historyItem); | ||
} | ||
if (this._preHistoryUpdateBuffer && this._preHistoryUpdateBuffer.length > 0) { | ||
for (const bufferedItem of this._preHistoryUpdateBuffer) { | ||
this.applyItem(bufferedItem); | ||
} | ||
} | ||
this._preHistoryUpdateBuffer = null; | ||
this.retain(); | ||
this.emit('history-set'); | ||
}; | ||
this.applyUpdate = (update) => { | ||
if (this._historyEnabled && !this._historyLoaded) { | ||
if (!this._preHistoryUpdateBuffer) { | ||
this._preHistoryUpdateBuffer = []; | ||
} | ||
this._preHistoryUpdateBuffer.push(update); | ||
return null; | ||
} | ||
const inserted = this.applyItem(update); | ||
this.retain(); | ||
this.emit('update', inserted); | ||
return inserted; | ||
}; | ||
this.getHasData = () => { | ||
return this._data.size !== 0; | ||
}; | ||
this.getData = () => { | ||
if (this._autoGapFillEnabled) { | ||
const result = []; | ||
let lastTimeUnitStart = null; | ||
let lastPrice = null; | ||
this._data.forEach(node => { | ||
const item = node.data; | ||
if (item) { | ||
if (lastTimeUnitStart !== null && lastPrice !== null) { | ||
const gaps = (0, utils_1.getTimeUnitStartsInRange)(lastTimeUnitStart, item.timeUnitStart, this._timeUnit, 'exclusive'); | ||
for (const gap of gaps) { | ||
result.push({ | ||
timeUnitStart: gap, | ||
timeOpen: gap, | ||
timeClose: gap, | ||
open: lastPrice, | ||
high: lastPrice, | ||
low: lastPrice, | ||
close: lastPrice, | ||
volume: 0, | ||
}); | ||
} | ||
} | ||
result.push(item); | ||
lastTimeUnitStart = item.timeUnitStart; | ||
lastPrice = item.close; | ||
} | ||
}); | ||
const currentTimeUnitStart = (0, utils_1.startOfTimeUnit)(Date.now(), this._timeUnit); | ||
const last = result[result.length - 1]; | ||
if (last && last.timeUnitStart !== currentTimeUnitStart) { | ||
const gaps = (0, utils_1.getTimeUnitStartsInRange)(last.timeUnitStart, currentTimeUnitStart, this._timeUnit); | ||
for (const gap of gaps) { | ||
if (gap !== last.timeUnitStart) { | ||
result.push({ | ||
timeUnitStart: gap, | ||
timeOpen: gap, | ||
timeClose: gap, | ||
open: last.close, | ||
high: last.close, | ||
low: last.close, | ||
close: last.close, | ||
volume: 0, | ||
}); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
return this._data.values(); | ||
}; | ||
this.getMostRecentOHLCVItem = () => { | ||
var _a; | ||
return (this._data.size > 0 && ((_a = this._data.maxNode()) === null || _a === void 0 ? void 0 : _a.data)) || null; | ||
}; | ||
this.getCurrentTimeUnitOHLCVItem = () => { | ||
var _a; | ||
const lastBar = this._data.size > 0 && ((_a = this._data.maxNode()) === null || _a === void 0 ? void 0 : _a.data); | ||
const currentTimeUnitStart = (0, utils_1.startOfTimeUnit)(Date.now(), this._timeUnit); | ||
if (lastBar) { | ||
if (lastBar.timeUnitStart === currentTimeUnitStart) { | ||
return lastBar; | ||
} | ||
else { | ||
return { | ||
timeUnitStart: currentTimeUnitStart, | ||
timeOpen: currentTimeUnitStart, | ||
timeClose: currentTimeUnitStart, | ||
open: lastBar.close, | ||
high: lastBar.close, | ||
low: lastBar.close, | ||
close: lastBar.close, | ||
volume: 0, | ||
}; | ||
} | ||
} | ||
else if (this._initialLastKnowClosingPrice !== null) { | ||
return { | ||
timeUnitStart: currentTimeUnitStart, | ||
timeOpen: currentTimeUnitStart, | ||
timeClose: currentTimeUnitStart, | ||
open: this._initialLastKnowClosingPrice, | ||
high: this._initialLastKnowClosingPrice, | ||
low: this._initialLastKnowClosingPrice, | ||
close: this._initialLastKnowClosingPrice, | ||
volume: 0, | ||
}; | ||
} | ||
return null; | ||
}; | ||
this.getLastKnownClosingPrice = () => { | ||
var _a; | ||
if (this._data.size > 0) { | ||
const lastEntry = (_a = this._data.maxNode()) === null || _a === void 0 ? void 0 : _a.data; | ||
if (lastEntry) { | ||
return lastEntry.close; | ||
} | ||
} | ||
return this._initialLastKnowClosingPrice; | ||
}; | ||
this._timeUnit = options.timeUnit; | ||
this._retention = options.retention || null; | ||
this._historyEnabled = options.enableHistory; | ||
this._autoGapFillEnabled = (_a = options.autofillGaps) !== null && _a !== void 0 ? _a : false; | ||
this._initialLastKnowClosingPrice = typeof options.lastKnowClosingPrice === 'number' ? options.lastKnowClosingPrice : null; | ||
} | ||
import { TypedEmitter } from 'tiny-typed-emitter'; | ||
import AVLTreeModule from 'avl'; | ||
import { startOfTimeUnit, getTimeUnitStartsInRange } from './utils.js'; | ||
const AVLTree = AVLTreeModule.default; | ||
export class OHLCVLiveAggregator extends TypedEmitter { | ||
_timeUnit; | ||
_retention; | ||
_historyEnabled; | ||
_autoGapFillEnabled; | ||
_historyLoaded = false; | ||
_preHistoryUpdateBuffer = null; | ||
_initialLastKnowClosingPrice; | ||
_data = new AVLTree(undefined, true); | ||
get timeUnit() { | ||
@@ -177,2 +26,10 @@ return this._timeUnit; | ||
} | ||
constructor(options) { | ||
super(); | ||
this._timeUnit = options.timeUnit; | ||
this._retention = options.retention || null; | ||
this._historyEnabled = options.enableHistory; | ||
this._autoGapFillEnabled = options.autofillGaps ?? false; | ||
this._initialLastKnowClosingPrice = typeof options.lastKnowClosingPrice === 'number' ? options.lastKnowClosingPrice : null; | ||
} | ||
retain() { | ||
@@ -195,3 +52,3 @@ if (!this._retention) { | ||
applyItem(update) { | ||
const targetTimeUnit = (0, utils_1.startOfTimeUnit)(update.timeUnitStart, this._timeUnit); | ||
const targetTimeUnit = startOfTimeUnit(update.timeUnitStart, this._timeUnit); | ||
const old = this._data.find(targetTimeUnit); | ||
@@ -235,4 +92,144 @@ let inserted; | ||
} | ||
resetHistory = () => { | ||
if (!this._historyEnabled) { | ||
throw new Error('History is disabled'); | ||
} | ||
this._historyLoaded = false; | ||
this.emit('history-reset'); | ||
}; | ||
setHistory = (history) => { | ||
if (!this._historyEnabled) { | ||
throw new Error('History is disabled'); | ||
} | ||
if (this._historyLoaded) { | ||
throw new Error('History cannot be loaded more than once, to reset the history first call resetHistory then setHistory'); | ||
} | ||
this._historyLoaded = true; | ||
this._data.clear(); | ||
for (const historyItem of history) { | ||
this.applyItem(historyItem); | ||
} | ||
if (this._preHistoryUpdateBuffer && this._preHistoryUpdateBuffer.length > 0) { | ||
for (const bufferedItem of this._preHistoryUpdateBuffer) { | ||
this.applyItem(bufferedItem); | ||
} | ||
} | ||
this._preHistoryUpdateBuffer = null; | ||
this.retain(); | ||
this.emit('history-set'); | ||
}; | ||
applyUpdate = (update) => { | ||
if (this._historyEnabled && !this._historyLoaded) { | ||
if (!this._preHistoryUpdateBuffer) { | ||
this._preHistoryUpdateBuffer = []; | ||
} | ||
this._preHistoryUpdateBuffer.push(update); | ||
return null; | ||
} | ||
const inserted = this.applyItem(update); | ||
this.retain(); | ||
this.emit('update', inserted); | ||
return inserted; | ||
}; | ||
getHasData = () => { | ||
return this._data.size !== 0; | ||
}; | ||
getData = () => { | ||
if (this._autoGapFillEnabled) { | ||
const result = []; | ||
let lastTimeUnitStart = null; | ||
let lastPrice = null; | ||
this._data.forEach(node => { | ||
const item = node.data; | ||
if (item) { | ||
if (lastTimeUnitStart !== null && lastPrice !== null) { | ||
const gaps = getTimeUnitStartsInRange(lastTimeUnitStart, item.timeUnitStart, this._timeUnit, 'exclusive'); | ||
for (const gap of gaps) { | ||
result.push({ | ||
timeUnitStart: gap, | ||
timeOpen: gap, | ||
timeClose: gap, | ||
open: lastPrice, | ||
high: lastPrice, | ||
low: lastPrice, | ||
close: lastPrice, | ||
volume: 0, | ||
}); | ||
} | ||
} | ||
result.push(item); | ||
lastTimeUnitStart = item.timeUnitStart; | ||
lastPrice = item.close; | ||
} | ||
}); | ||
const currentTimeUnitStart = startOfTimeUnit(Date.now(), this._timeUnit); | ||
const last = result[result.length - 1]; | ||
if (last && last.timeUnitStart !== currentTimeUnitStart) { | ||
const gaps = getTimeUnitStartsInRange(last.timeUnitStart, currentTimeUnitStart, this._timeUnit); | ||
for (const gap of gaps) { | ||
if (gap !== last.timeUnitStart) { | ||
result.push({ | ||
timeUnitStart: gap, | ||
timeOpen: gap, | ||
timeClose: gap, | ||
open: last.close, | ||
high: last.close, | ||
low: last.close, | ||
close: last.close, | ||
volume: 0, | ||
}); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
return this._data.values(); | ||
}; | ||
getMostRecentOHLCVItem = () => { | ||
return (this._data.size > 0 && this._data.maxNode()?.data) || null; | ||
}; | ||
getCurrentTimeUnitOHLCVItem = () => { | ||
const lastBar = this._data.size > 0 && this._data.maxNode()?.data; | ||
const currentTimeUnitStart = startOfTimeUnit(Date.now(), this._timeUnit); | ||
if (lastBar) { | ||
if (lastBar.timeUnitStart === currentTimeUnitStart) { | ||
return lastBar; | ||
} | ||
else { | ||
return { | ||
timeUnitStart: currentTimeUnitStart, | ||
timeOpen: currentTimeUnitStart, | ||
timeClose: currentTimeUnitStart, | ||
open: lastBar.close, | ||
high: lastBar.close, | ||
low: lastBar.close, | ||
close: lastBar.close, | ||
volume: 0, | ||
}; | ||
} | ||
} | ||
else if (this._initialLastKnowClosingPrice !== null) { | ||
return { | ||
timeUnitStart: currentTimeUnitStart, | ||
timeOpen: currentTimeUnitStart, | ||
timeClose: currentTimeUnitStart, | ||
open: this._initialLastKnowClosingPrice, | ||
high: this._initialLastKnowClosingPrice, | ||
low: this._initialLastKnowClosingPrice, | ||
close: this._initialLastKnowClosingPrice, | ||
volume: 0, | ||
}; | ||
} | ||
return null; | ||
}; | ||
getLastKnownClosingPrice = () => { | ||
if (this._data.size > 0) { | ||
const lastEntry = this._data.maxNode()?.data; | ||
if (lastEntry) { | ||
return lastEntry.close; | ||
} | ||
} | ||
return this._initialLastKnowClosingPrice; | ||
}; | ||
} | ||
exports.OHLCVLiveAggregator = OHLCVLiveAggregator; | ||
//# sourceMappingURL=OHLCVLiveAggregator.js.map |
@@ -1,3 +0,3 @@ | ||
import { OHLCVDataItem } from './types'; | ||
export declare type ReduceOHLCVItemsResult = Omit<OHLCVDataItem, 'timeUnitStart'>; | ||
import { OHLCVDataItem } from './types.js'; | ||
export type ReduceOHLCVItemsResult = Omit<OHLCVDataItem, 'timeUnitStart'>; | ||
export declare function reduceOHLCVItems(items: OHLCVDataItem[]): ReduceOHLCVItemsResult | null; |
@@ -1,6 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.reduceOHLCVItems = void 0; | ||
const tslib_1 = require("tslib"); | ||
function reduceOHLCVItems(items) { | ||
export function reduceOHLCVItems(items) { | ||
if (items.length === 0) { | ||
@@ -10,3 +6,3 @@ return null; | ||
if (items.length === 1) { | ||
const _a = items[0], { timeUnitStart: time_unit_start } = _a, result = (0, tslib_1.__rest)(_a, ["timeUnitStart"]); | ||
const { timeUnitStart: time_unit_start, ...result } = items[0]; | ||
return result; | ||
@@ -34,3 +30,2 @@ } | ||
} | ||
exports.reduceOHLCVItems = reduceOHLCVItems; | ||
//# sourceMappingURL=reduceOHLCV.js.map |
@@ -1,3 +0,3 @@ | ||
import { OHLCVTimeUnit, OHLCVTimeUnitGranularity } from './types'; | ||
export declare type OHLCVTimeUnitStringFormats = 'default' | 'trading-view' | 'coin-api'; | ||
import { OHLCVTimeUnit, OHLCVTimeUnitGranularity } from './types.js'; | ||
export type OHLCVTimeUnitStringFormats = 'default' | 'trading-view' | 'coin-api'; | ||
export declare const ohlcvTimeUnitGranularities: readonly OHLCVTimeUnitGranularity[]; | ||
@@ -4,0 +4,0 @@ export declare function isOHLCVTimeUnitValid(timeUnit: any): timeUnit is OHLCVTimeUnit; |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.convertTimeUnitStringFormat = exports.ohlcvTimeUnitFromString = exports.ohlcvTimeUnitToDisplayString = exports.ohlcvTimeUnitToString = exports.isOHLCVTimeUnitValid = exports.ohlcvTimeUnitGranularities = void 0; | ||
exports.ohlcvTimeUnitGranularities = Object.freeze([ | ||
export const ohlcvTimeUnitGranularities = Object.freeze([ | ||
'second', | ||
@@ -13,3 +10,3 @@ 'minute', | ||
]); | ||
function isOHLCVTimeUnitValid(timeUnit) { | ||
export function isOHLCVTimeUnitValid(timeUnit) { | ||
if (!timeUnit) { | ||
@@ -24,3 +21,3 @@ return false; | ||
} | ||
if (!exports.ohlcvTimeUnitGranularities.includes(timeUnit.granularity)) { | ||
if (!ohlcvTimeUnitGranularities.includes(timeUnit.granularity)) { | ||
return false; | ||
@@ -30,4 +27,3 @@ } | ||
} | ||
exports.isOHLCVTimeUnitValid = isOHLCVTimeUnitValid; | ||
function ohlcvTimeUnitToString(timeUnit, format = 'default') { | ||
export function ohlcvTimeUnitToString(timeUnit, format = 'default') { | ||
switch (format) { | ||
@@ -74,9 +70,7 @@ case 'default': | ||
} | ||
exports.ohlcvTimeUnitToString = ohlcvTimeUnitToString; | ||
function ohlcvTimeUnitToDisplayString(timeUnit) { | ||
export function ohlcvTimeUnitToDisplayString(timeUnit) { | ||
const titleText = `${timeUnit.granularity[0].toUpperCase()}${timeUnit.granularity.substring(1)}`; | ||
return `${timeUnit.period} ${titleText}${timeUnit.period > 1 ? 's' : ''}`; | ||
} | ||
exports.ohlcvTimeUnitToDisplayString = ohlcvTimeUnitToDisplayString; | ||
function ohlcvTimeUnitFromString(str) { | ||
export function ohlcvTimeUnitFromString(str) { | ||
if (!str || typeof str !== 'string') { | ||
@@ -145,8 +139,6 @@ return null; | ||
} | ||
exports.ohlcvTimeUnitFromString = ohlcvTimeUnitFromString; | ||
function convertTimeUnitStringFormat(timeUnitStr, toFormat) { | ||
export function convertTimeUnitStringFormat(timeUnitStr, toFormat) { | ||
const timeUnit = timeUnitStr ? ohlcvTimeUnitFromString(timeUnitStr) : null; | ||
return timeUnit ? ohlcvTimeUnitToString(timeUnit, toFormat) : null; | ||
} | ||
exports.convertTimeUnitStringFormat = convertTimeUnitStringFormat; | ||
//# sourceMappingURL=timeUnit.js.map |
@@ -1,2 +0,2 @@ | ||
export declare type OHLCVTimeUnitGranularity = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'; | ||
export type OHLCVTimeUnitGranularity = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'; | ||
export interface OHLCVTimeUnit { | ||
@@ -3,0 +3,0 @@ granularity: OHLCVTimeUnitGranularity; |
@@ -1,3 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
export {}; | ||
//# sourceMappingURL=types.js.map |
@@ -1,2 +0,2 @@ | ||
import { OHLCVTimeUnitGranularity, OHLCVTimeUnit } from './types'; | ||
import { OHLCVTimeUnitGranularity, OHLCVTimeUnit } from './types.js'; | ||
export declare function getGranularityDuration(granularity: OHLCVTimeUnitGranularity): number; | ||
@@ -3,0 +3,0 @@ export declare function getTimeUnitDuration(timeUnit: OHLCVTimeUnit): number; |
@@ -1,7 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getTimeUnitStartsInRange = exports.startOfClosestTimeUnit = exports.startOfTimeUnit = exports.startOfGranularity = exports.getTimeUnitDuration = exports.getGranularityDuration = void 0; | ||
const tslib_1 = require("tslib"); | ||
const moment_1 = (0, tslib_1.__importDefault)(require("moment")); | ||
function getGranularityDuration(granularity) { | ||
import moment from 'moment'; | ||
export function getGranularityDuration(granularity) { | ||
switch (granularity) { | ||
@@ -24,27 +20,24 @@ case 'second': | ||
} | ||
exports.getGranularityDuration = getGranularityDuration; | ||
function getTimeUnitDuration(timeUnit) { | ||
export function getTimeUnitDuration(timeUnit) { | ||
return Math.round(getGranularityDuration(timeUnit.granularity) * timeUnit.period); | ||
} | ||
exports.getTimeUnitDuration = getTimeUnitDuration; | ||
function startOfGranularity(timestamp, granularity) { | ||
export function startOfGranularity(timestamp, granularity) { | ||
switch (granularity) { | ||
case 'second': | ||
return (0, moment_1.default)(timestamp).utc().startOf('second').valueOf(); | ||
return moment(timestamp).utc().startOf('second').valueOf(); | ||
case 'minute': | ||
return (0, moment_1.default)(timestamp).utc().startOf('minute').valueOf(); | ||
return moment(timestamp).utc().startOf('minute').valueOf(); | ||
case 'hour': | ||
return (0, moment_1.default)(timestamp).utc().startOf('hour').valueOf(); | ||
return moment(timestamp).utc().startOf('hour').valueOf(); | ||
case 'day': | ||
return (0, moment_1.default)(timestamp).utc().startOf('day').valueOf(); | ||
return moment(timestamp).utc().startOf('day').valueOf(); | ||
case 'week': | ||
return (0, moment_1.default)(timestamp).utc().startOf('isoWeek').valueOf(); | ||
return moment(timestamp).utc().startOf('isoWeek').valueOf(); | ||
case 'month': | ||
return (0, moment_1.default)(timestamp).utc().startOf('month').valueOf(); | ||
return moment(timestamp).utc().startOf('month').valueOf(); | ||
case 'year': | ||
return (0, moment_1.default)(timestamp).utc().startOf('year').valueOf(); | ||
return moment(timestamp).utc().startOf('year').valueOf(); | ||
} | ||
} | ||
exports.startOfGranularity = startOfGranularity; | ||
function startOfTimeUnit(timestamp, timeUnit) { | ||
export function startOfTimeUnit(timestamp, timeUnit) { | ||
const timeUnitDuration = getTimeUnitDuration(timeUnit); | ||
@@ -54,4 +47,3 @@ const intervalStartTimestamp = timeUnit.period === 1 ? timestamp : Math.floor(timestamp / timeUnitDuration) * timeUnitDuration; | ||
} | ||
exports.startOfTimeUnit = startOfTimeUnit; | ||
function startOfClosestTimeUnit(timestamp, timeUnit) { | ||
export function startOfClosestTimeUnit(timestamp, timeUnit) { | ||
const timeUnitDuration = getTimeUnitDuration(timeUnit); | ||
@@ -68,4 +60,3 @@ const timestamp1 = timestamp; | ||
} | ||
exports.startOfClosestTimeUnit = startOfClosestTimeUnit; | ||
function getTimeUnitStartsInRange(start, end, timeUnit, inclusion = 'inclusive') { | ||
export function getTimeUnitStartsInRange(start, end, timeUnit, inclusion = 'inclusive') { | ||
const initialTimeUnitStart = startOfClosestTimeUnit(start, timeUnit) === start ? start : startOfTimeUnit(start, timeUnit); | ||
@@ -95,3 +86,2 @@ const finalTimeUnitStart = startOfClosestTimeUnit(end, timeUnit) === end ? end : startOfTimeUnit(end, timeUnit); | ||
} | ||
exports.getTimeUnitStartsInRange = getTimeUnitStartsInRange; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@aurox/ohlcv-helpers", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"type": "module", | ||
"scripts": { | ||
@@ -11,3 +12,3 @@ "prepublishOnly": "npm test && npm run build", | ||
"clean": "rimraf ./dist", | ||
"test": "mocha -r ts-node/register \"src/**/*.spec.ts\"" | ||
"test": "mocha \"src/**/*.spec.ts\"" | ||
}, | ||
@@ -22,23 +23,23 @@ "files": [ | ||
"dependencies": { | ||
"@types/node": "^17.0.8", | ||
"@types/node": "^20.3.0", | ||
"avl": "^1.5.3", | ||
"moment": "^2.29.1", | ||
"moment": "^2.29.4", | ||
"tiny-typed-emitter": "^2.1.0", | ||
"tslib": "^2.3.1" | ||
"tslib": "^2.5.3" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.3.0", | ||
"@types/mocha": "^9.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.9.0", | ||
"@typescript-eslint/parser": "^5.9.0", | ||
"chai": "^4.3.4", | ||
"eslint": "^8.6.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"mocha": "^9.1.3", | ||
"prettier": "^2.5.1", | ||
"rimraf": "^3.0.2", | ||
"ts-node": "^10.4.0", | ||
"typescript": "^4.5.4" | ||
"@types/chai": "^4.3.5", | ||
"@types/mocha": "^10.0.1", | ||
"@typescript-eslint/eslint-plugin": "^5.59.9", | ||
"@typescript-eslint/parser": "^5.59.9", | ||
"chai": "^4.3.7", | ||
"eslint": "^8.42.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"mocha": "^10.2.0", | ||
"prettier": "^2.8.8", | ||
"rimraf": "^5.0.1", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.1.3" | ||
} | ||
} |
@@ -1,1 +0,1 @@ | ||
export * from './pricePointListToOHLCV'; | ||
export * from './pricePointListToOHLCV.js'; |
@@ -1,6 +0,8 @@ | ||
import AVLTree from 'avl'; | ||
import AVLTreeModule from 'avl'; | ||
import { OHLCVTimeUnit, OHLCVDataItem } from '../types'; | ||
import { startOfTimeUnit, getTimeUnitDuration } from '../utils'; | ||
import { OHLCVTimeUnit, OHLCVDataItem } from '../types.js'; | ||
import { startOfTimeUnit, getTimeUnitDuration } from '../utils.js'; | ||
const AVLTree = AVLTreeModule.default; | ||
export interface PricePointListItem { | ||
@@ -7,0 +9,0 @@ timestamp: number; |
import { expect } from 'chai'; | ||
import { gapFillRange } from './gapFillRange'; | ||
import { OHLCVTimeUnit, OHLCVDataItem } from './types'; | ||
import { gapFillRange } from './gapFillRange.js'; | ||
import { OHLCVTimeUnit, OHLCVDataItem } from './types.js'; | ||
@@ -6,0 +6,0 @@ describe('gapFillRange', () => { |
@@ -1,3 +0,3 @@ | ||
import { OHLCVDataItem, OHLCVTimeUnit } from './types'; | ||
import { getTimeUnitStartsInRange } from './utils'; | ||
import { OHLCVDataItem, OHLCVTimeUnit } from './types.js'; | ||
import { getTimeUnitStartsInRange } from './utils.js'; | ||
@@ -4,0 +4,0 @@ export interface GapFillRangeOptions { |
@@ -1,7 +0,7 @@ | ||
export * from './types'; | ||
export * from './utils'; | ||
export * from './timeUnit'; | ||
export * from './OHLCVLiveAggregator'; | ||
export * from './reduceOHLCV'; | ||
export * from './converters'; | ||
export * from './gapFillRange'; | ||
export * from './converters/index.js'; | ||
export * from './types.js'; | ||
export * from './utils.js'; | ||
export * from './timeUnit.js'; | ||
export * from './OHLCVLiveAggregator.js'; | ||
export * from './reduceOHLCV.js'; | ||
export * from './gapFillRange.js'; |
import { TypedEmitter } from 'tiny-typed-emitter'; | ||
import AVLTree from 'avl'; | ||
import AVLTreeModule from 'avl'; | ||
import { OHLCVTimeUnit, OHLCVDataItem } from './types'; | ||
import { startOfTimeUnit, getTimeUnitStartsInRange } from './utils'; | ||
import { OHLCVTimeUnit, OHLCVDataItem } from './types.js'; | ||
import { startOfTimeUnit, getTimeUnitStartsInRange } from './utils.js'; | ||
const AVLTree = AVLTreeModule.default; | ||
export interface OHLCVAggregatorRecordLimitRetentionOptions { | ||
@@ -8,0 +10,0 @@ method: 'record-limit'; |
@@ -1,2 +0,2 @@ | ||
import { OHLCVDataItem } from './types'; | ||
import { OHLCVDataItem } from './types.js'; | ||
@@ -3,0 +3,0 @@ export type ReduceOHLCVItemsResult = Omit<OHLCVDataItem, 'timeUnitStart'>; |
@@ -1,2 +0,2 @@ | ||
import { OHLCVTimeUnit, OHLCVTimeUnitGranularity } from './types'; | ||
import { OHLCVTimeUnit, OHLCVTimeUnitGranularity } from './types.js'; | ||
@@ -3,0 +3,0 @@ export type OHLCVTimeUnitStringFormats = 'default' | 'trading-view' | 'coin-api'; |
import { expect } from 'chai'; | ||
import moment from 'moment'; | ||
import { getTimeUnitStartsInRange } from './utils'; | ||
import { getTimeUnitStartsInRange } from './utils.js'; | ||
@@ -6,0 +6,0 @@ describe('Utils', () => { |
import moment from 'moment'; | ||
import { OHLCVTimeUnitGranularity, OHLCVTimeUnit } from './types'; | ||
import { OHLCVTimeUnitGranularity, OHLCVTimeUnit } from './types.js'; | ||
@@ -5,0 +5,0 @@ export function getGranularityDuration(granularity: OHLCVTimeUnitGranularity): number { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
106101
45
1813
Yes
1
+ Added@types/node@20.17.19(transitive)
+ Addedundici-types@6.19.8(transitive)
- Removed@types/node@17.0.45(transitive)
Updated@types/node@^20.3.0
Updatedmoment@^2.29.4
Updatedtslib@^2.5.3