Comparing version 2.1.2 to 2.2.0
@@ -7,3 +7,3 @@ # Changelog | ||
## [2.1.2] - 2023-01-11 | ||
## [2.1.2] - 2023-01-19 | ||
### Fixed | ||
@@ -10,0 +10,0 @@ - fix for very specific event subscriptions. |
{ | ||
"name": "candles", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "Generate candlesticks from your tick data in realtime.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -52,3 +52,3 @@ # Candles | ||
Candlecollection.on('close', candle => { | ||
Candlecollection.on('close', (candle, candles) => { | ||
console.log(candle); | ||
@@ -195,3 +195,3 @@ }); | ||
```js | ||
Candlecollection.on('close BTC-USD 30s', currentCandle, candles => { | ||
Candlecollection.on('close BTC-USD 30s', (currentCandle, candles) => { | ||
console.log(currentCandle); | ||
@@ -198,0 +198,0 @@ }); |
@@ -21,3 +21,3 @@ const Clock = require('./Clock'); | ||
addProduct(product, timeframe, serieslength=0) { | ||
addProduct(product, timeframe, serieslength = 0) { | ||
if (!product) { | ||
@@ -29,19 +29,16 @@ throw 'product is missing' | ||
} | ||
if (Array.isArray(product)) { | ||
product.forEach(product => { | ||
(Array.isArray(product) ? product : [product]).forEach(product => { | ||
if (!this.series[product]) { | ||
this.addTimeframe(product, timeframe, serieslength); | ||
}) | ||
} else { | ||
this.addTimeframe(product, timeframe, serieslength); | ||
} | ||
} | ||
}) | ||
}; | ||
addTimeframe(product, timeframe, serieslength) { | ||
if (Array.isArray(timeframe)) { | ||
timeframe.forEach(timeframe => { | ||
this.addSeries(product, timeframe, serieslength); | ||
}) | ||
} else { | ||
(Array.isArray(timeframe) ? timeframe : [timeframe]).forEach(timeframe => { | ||
if (this.series[product]?.timeframe[timeframe]) { | ||
return | ||
} | ||
this.addSeries(product, timeframe, serieslength); | ||
} | ||
}) | ||
} | ||
@@ -56,4 +53,4 @@ | ||
this.clock.on('tick ' + timeframe, series.onSeriesClockTick); | ||
series.on('open',this.seriesOpen.bind(this)); | ||
series.on('close',this.seriesClose.bind(this)); | ||
series.on('open', this.seriesOpen.bind(this)); | ||
series.on('close', this.seriesClose.bind(this)); | ||
if (!this.series[product]) { | ||
@@ -69,18 +66,30 @@ this.series[product] = {}; | ||
getTimeDrift() { | ||
return this.clock.timediff.drift | ||
return this.clock.timediff.drift | ||
} | ||
removeProduct(product, timeframe) { | ||
if (this.series[product].timeframe[timeframe]) { | ||
this.clock.off('tick ' + timeframe, this.series[product].timeframe[timeframe].onSeriesClockTick); | ||
this.series[product].timeframe[timeframe].off('open',this.seriesOpen.bind(this)); | ||
this.series[product].timeframe[timeframe].off('close',this.seriesClose.bind(this)); | ||
delete this.series[product].timeframe[timeframe]; | ||
} | ||
if (Object.keys(this.series[product].timeframe).length === 0) { | ||
delete this.series[product]; | ||
} | ||
(Array.isArray(product) ? product : [product]).forEach(product => { | ||
if (this.series[product]) { | ||
this.removeTimeframe(product, timeframe); | ||
Object.keys(this.series[product].timeframe).length || delete this.series[product]; | ||
} | ||
}) | ||
} | ||
SetSeriesPrice(product, price, size=0) { | ||
removeTimeframe(product, timeframe) { | ||
(Array.isArray(timeframe) ? timeframe : [timeframe]).forEach(timeframe => { | ||
if (this.series[product]?.timeframe[timeframe]) { | ||
this.removeSeries(product, timeframe) | ||
} | ||
}) | ||
} | ||
removeSeries(product, timeframe) { | ||
this.clock.off('tick ' + timeframe, this.series[product].timeframe[timeframe].onSeriesClockTick); | ||
this.series[product].timeframe[timeframe].off('open', this.seriesOpen.bind(this)); | ||
this.series[product].timeframe[timeframe].off('close', this.seriesClose.bind(this)); | ||
delete this.series[product].timeframe[timeframe]; | ||
} | ||
SetSeriesPrice(product, price, size = 0) { | ||
Object.keys(this.series[product].timeframe).forEach(timeframe => { | ||
@@ -92,6 +101,6 @@ if (!this.series[product].timeframe[timeframe]) { | ||
this.series[product].timeframe[timeframe].currentCandle.updatePrice(Number.parseFloat(price), Number.parseFloat(size).toFixed(8)); | ||
this.emit('current ' + this.series[product].timeframe[timeframe].product + ' ' + this.series[product].timeframe[timeframe].timeframe, this.series[product].timeframe[timeframe].currentCandle); | ||
this.emit('current ' + this.series[product].timeframe[timeframe].product, this.series[product].timeframe[timeframe].currentCandle); | ||
this.emit('current ' + this.series[product].timeframe[timeframe].timeframe, this.series[product].timeframe[timeframe].currentCandle); | ||
this.emit('current', this.series[product].timeframe[timeframe].currentCandle); | ||
this.emit('current ' + this.series[product].timeframe[timeframe].product + ' ' + this.series[product].timeframe[timeframe].timeframe, this.series[product].timeframe[timeframe].currentCandle, this.series[product].timeframe[timeframe].candles); | ||
this.emit('current ' + this.series[product].timeframe[timeframe].product, this.series[product].timeframe[timeframe].currentCandle, this.series[product].timeframe[timeframe].candles); | ||
this.emit('current ' + this.series[product].timeframe[timeframe].timeframe, this.series[product].timeframe[timeframe].currentCandle, this.series[product].timeframe[timeframe].candles); | ||
this.emit('current', this.series[product].timeframe[timeframe].currentCandle, this.series[product].timeframe[timeframe].candles); | ||
}); | ||
@@ -121,2 +130,2 @@ } | ||
module.exports = candles; | ||
module.exports = candles; |
@@ -5,3 +5,6 @@ const units = { | ||
h: 3600, | ||
d: 86400 | ||
d: 86400, | ||
w: 604800, | ||
M: 2592000, | ||
y: 31536000 | ||
} | ||
@@ -25,15 +28,23 @@ | ||
module.exports.secToTimeframe = (secs) => { | ||
return; | ||
const keys = Object.keys(units); | ||
for (let i = keys.length - 1; i >= 0; i--) { | ||
let key = keys[i] | ||
if (secs % units[key] === 0) { | ||
const count = secs / units[key] | ||
return count + key; | ||
} | ||
} | ||
return undefined; | ||
} | ||
module.exports.timeLeft = (epoch, interval) => { | ||
return interval - (Math.floor(Number(epoch)/1000) % interval); | ||
return interval - (Math.floor(Number(epoch) / 1000) % interval); | ||
} | ||
module.exports.nextTime = (epoch, interval) => { | ||
return (Math.floor(epoch/1000) + this.timeLeft(epoch, interval)) * 1000; | ||
return (Math.floor(epoch / 1000) + module.exports.timeLeft(epoch, interval)) * 1000; | ||
} | ||
module.exports.average = (arr) => { | ||
return arr.reduce((acc, val) => acc + parseFloat(val, 10), 0) / arr.length; | ||
} | ||
return arr.reduce((acc, val) => acc + parseFloat(val, 10), 0) / arr.length; | ||
} |
56628
11
311