How do I generate multiple streams from the same stream in node?












0















I have 1m ohlc data coming from an external WebSocket API and I am trying to generate 5m 15m 30m 1h 1d and 1w streams from it. My current TransformStream can convert 1m to one of the above mentioned timeframes



How do I do



one.pipe(five)
one.pipe(fifteen)
one.pipe(thirty)


without creating multiple instances of the original stream "one". Currently my single TransformStream instance emits the original data plus all the required timeframes.



class TimeFrameGenerator extends Transform {
constructor(timeframes) {
super({ readableObjectMode: true, writableObjectMode: true });
this.kline = {}

//Consider using an object for this, looping with a for in is much faster for this case than using an array or accessing object keys
this.timeframes = {
"1m": 1000 * 60,
"5m": 1000 * 60 * 5,
"15m": 1000 * 60 * 15,
"30m": 1000 * 60 * 30,
"1h": 1000 * 60 * 60,
"1d": 1000 * 60 * 60 * 24
}
}

_transform(rawKline, encoding, callback) {

this.push(rawKline);

const { pairId, base, quote, interval, openTime, closeTime, timestamp, open, high, low, close, baseVolume, quoteVolume, isFinal, raw } = rawKline;

if (typeof this.kline[pairId] === "undefined")
this.kline[pairId] = {};

for (let timeframe in this.timeframes) {
if (timeframe === interval)
continue;

const millis = this.timeframes[timeframe];

const resampledOpenTime = Math.floor(openTime / millis) * millis;
const resampledCloseTime = Math.ceil(openTime / millis) * millis - 1;

if (typeof this.kline[pairId][timeframe] === "undefined") {
this.kline[pairId][timeframe] = {};
}

if (typeof this.kline[pairId][timeframe][resampledOpenTime] === "undefined") {
const newKline = {
pairId: pairId,
base: base,
quote: quote,
interval: timeframe,
openTime: resampledOpenTime,
closeTime: resampledCloseTime,
timestamp: timestamp,
open: open,
high: high,
low: low,
close: close,
baseVolume: baseVolume,
quoteVolume: quoteVolume,
isFinal: false,
raw: null
}

this.kline[pairId][timeframe][resampledOpenTime] = {
kline: newKline,
prevBaseVol: 0,
prevQuoteVol: 0
}
}
else {
const { kline, prevBaseVol, prevQuoteVol } = this.kline[pairId][timeframe][resampledOpenTime];
kline.high = high > kline.high ? high : kline.high;
kline.low = low < kline.low ? low : kline.low;
kline.close = close;
kline.baseVolume = parseFloat((prevBaseVol + baseVolume).toFixed(PRECISION));
kline.quoteVolume = parseFloat((prevQuoteVol + quoteVolume).toFixed(PRECISION));
}

if (isFinal) {
const { prevBaseVol, prevQuoteVol } = this.kline[pairId][timeframe][resampledOpenTime];
this.kline[pairId][timeframe][resampledOpenTime].prevBaseVol = parseFloat((prevBaseVol + baseVolume).toFixed(PRECISION));
this.kline[pairId][timeframe][resampledOpenTime].prevQuoteVol = parseFloat((prevQuoteVol + quoteVolume).toFixed(PRECISION));
}

let keys = Object.keys(this.kline[pairId][timeframe]);

if (keys.length > 1) {
let previousTimestamp = keys.shift();
this.kline[pairId][timeframe][previousTimestamp]['kline'].isFinal = true;
this.push(this.kline[pairId][timeframe][previousTimestamp].kline);
delete this.kline[pairId][timeframe][previousTimestamp];
console.log(Object.keys(this.kline[pairId][timeframe]).length, Object.keys(this.kline[pairId]).length)
}

this.push(this.kline[pairId][timeframe][resampledOpenTime].kline);
}

callback()
}
}


I am new to streams and some idea would be super appreciated
Should I create multiple TransformStream instances, one for each timeframe










share|improve this question


















  • 1





    Hmm... I think you did answer yourself it's simply one.pipe(three); one.pipe(five); //etc. All the streams will receive all the samples.

    – Michał Kapracki
    Nov 26 '18 at 14:55
















0















I have 1m ohlc data coming from an external WebSocket API and I am trying to generate 5m 15m 30m 1h 1d and 1w streams from it. My current TransformStream can convert 1m to one of the above mentioned timeframes



How do I do



one.pipe(five)
one.pipe(fifteen)
one.pipe(thirty)


without creating multiple instances of the original stream "one". Currently my single TransformStream instance emits the original data plus all the required timeframes.



class TimeFrameGenerator extends Transform {
constructor(timeframes) {
super({ readableObjectMode: true, writableObjectMode: true });
this.kline = {}

//Consider using an object for this, looping with a for in is much faster for this case than using an array or accessing object keys
this.timeframes = {
"1m": 1000 * 60,
"5m": 1000 * 60 * 5,
"15m": 1000 * 60 * 15,
"30m": 1000 * 60 * 30,
"1h": 1000 * 60 * 60,
"1d": 1000 * 60 * 60 * 24
}
}

_transform(rawKline, encoding, callback) {

this.push(rawKline);

const { pairId, base, quote, interval, openTime, closeTime, timestamp, open, high, low, close, baseVolume, quoteVolume, isFinal, raw } = rawKline;

if (typeof this.kline[pairId] === "undefined")
this.kline[pairId] = {};

for (let timeframe in this.timeframes) {
if (timeframe === interval)
continue;

const millis = this.timeframes[timeframe];

const resampledOpenTime = Math.floor(openTime / millis) * millis;
const resampledCloseTime = Math.ceil(openTime / millis) * millis - 1;

if (typeof this.kline[pairId][timeframe] === "undefined") {
this.kline[pairId][timeframe] = {};
}

if (typeof this.kline[pairId][timeframe][resampledOpenTime] === "undefined") {
const newKline = {
pairId: pairId,
base: base,
quote: quote,
interval: timeframe,
openTime: resampledOpenTime,
closeTime: resampledCloseTime,
timestamp: timestamp,
open: open,
high: high,
low: low,
close: close,
baseVolume: baseVolume,
quoteVolume: quoteVolume,
isFinal: false,
raw: null
}

this.kline[pairId][timeframe][resampledOpenTime] = {
kline: newKline,
prevBaseVol: 0,
prevQuoteVol: 0
}
}
else {
const { kline, prevBaseVol, prevQuoteVol } = this.kline[pairId][timeframe][resampledOpenTime];
kline.high = high > kline.high ? high : kline.high;
kline.low = low < kline.low ? low : kline.low;
kline.close = close;
kline.baseVolume = parseFloat((prevBaseVol + baseVolume).toFixed(PRECISION));
kline.quoteVolume = parseFloat((prevQuoteVol + quoteVolume).toFixed(PRECISION));
}

if (isFinal) {
const { prevBaseVol, prevQuoteVol } = this.kline[pairId][timeframe][resampledOpenTime];
this.kline[pairId][timeframe][resampledOpenTime].prevBaseVol = parseFloat((prevBaseVol + baseVolume).toFixed(PRECISION));
this.kline[pairId][timeframe][resampledOpenTime].prevQuoteVol = parseFloat((prevQuoteVol + quoteVolume).toFixed(PRECISION));
}

let keys = Object.keys(this.kline[pairId][timeframe]);

if (keys.length > 1) {
let previousTimestamp = keys.shift();
this.kline[pairId][timeframe][previousTimestamp]['kline'].isFinal = true;
this.push(this.kline[pairId][timeframe][previousTimestamp].kline);
delete this.kline[pairId][timeframe][previousTimestamp];
console.log(Object.keys(this.kline[pairId][timeframe]).length, Object.keys(this.kline[pairId]).length)
}

this.push(this.kline[pairId][timeframe][resampledOpenTime].kline);
}

callback()
}
}


I am new to streams and some idea would be super appreciated
Should I create multiple TransformStream instances, one for each timeframe










share|improve this question


















  • 1





    Hmm... I think you did answer yourself it's simply one.pipe(three); one.pipe(five); //etc. All the streams will receive all the samples.

    – Michał Kapracki
    Nov 26 '18 at 14:55














0












0








0








I have 1m ohlc data coming from an external WebSocket API and I am trying to generate 5m 15m 30m 1h 1d and 1w streams from it. My current TransformStream can convert 1m to one of the above mentioned timeframes



How do I do



one.pipe(five)
one.pipe(fifteen)
one.pipe(thirty)


without creating multiple instances of the original stream "one". Currently my single TransformStream instance emits the original data plus all the required timeframes.



class TimeFrameGenerator extends Transform {
constructor(timeframes) {
super({ readableObjectMode: true, writableObjectMode: true });
this.kline = {}

//Consider using an object for this, looping with a for in is much faster for this case than using an array or accessing object keys
this.timeframes = {
"1m": 1000 * 60,
"5m": 1000 * 60 * 5,
"15m": 1000 * 60 * 15,
"30m": 1000 * 60 * 30,
"1h": 1000 * 60 * 60,
"1d": 1000 * 60 * 60 * 24
}
}

_transform(rawKline, encoding, callback) {

this.push(rawKline);

const { pairId, base, quote, interval, openTime, closeTime, timestamp, open, high, low, close, baseVolume, quoteVolume, isFinal, raw } = rawKline;

if (typeof this.kline[pairId] === "undefined")
this.kline[pairId] = {};

for (let timeframe in this.timeframes) {
if (timeframe === interval)
continue;

const millis = this.timeframes[timeframe];

const resampledOpenTime = Math.floor(openTime / millis) * millis;
const resampledCloseTime = Math.ceil(openTime / millis) * millis - 1;

if (typeof this.kline[pairId][timeframe] === "undefined") {
this.kline[pairId][timeframe] = {};
}

if (typeof this.kline[pairId][timeframe][resampledOpenTime] === "undefined") {
const newKline = {
pairId: pairId,
base: base,
quote: quote,
interval: timeframe,
openTime: resampledOpenTime,
closeTime: resampledCloseTime,
timestamp: timestamp,
open: open,
high: high,
low: low,
close: close,
baseVolume: baseVolume,
quoteVolume: quoteVolume,
isFinal: false,
raw: null
}

this.kline[pairId][timeframe][resampledOpenTime] = {
kline: newKline,
prevBaseVol: 0,
prevQuoteVol: 0
}
}
else {
const { kline, prevBaseVol, prevQuoteVol } = this.kline[pairId][timeframe][resampledOpenTime];
kline.high = high > kline.high ? high : kline.high;
kline.low = low < kline.low ? low : kline.low;
kline.close = close;
kline.baseVolume = parseFloat((prevBaseVol + baseVolume).toFixed(PRECISION));
kline.quoteVolume = parseFloat((prevQuoteVol + quoteVolume).toFixed(PRECISION));
}

if (isFinal) {
const { prevBaseVol, prevQuoteVol } = this.kline[pairId][timeframe][resampledOpenTime];
this.kline[pairId][timeframe][resampledOpenTime].prevBaseVol = parseFloat((prevBaseVol + baseVolume).toFixed(PRECISION));
this.kline[pairId][timeframe][resampledOpenTime].prevQuoteVol = parseFloat((prevQuoteVol + quoteVolume).toFixed(PRECISION));
}

let keys = Object.keys(this.kline[pairId][timeframe]);

if (keys.length > 1) {
let previousTimestamp = keys.shift();
this.kline[pairId][timeframe][previousTimestamp]['kline'].isFinal = true;
this.push(this.kline[pairId][timeframe][previousTimestamp].kline);
delete this.kline[pairId][timeframe][previousTimestamp];
console.log(Object.keys(this.kline[pairId][timeframe]).length, Object.keys(this.kline[pairId]).length)
}

this.push(this.kline[pairId][timeframe][resampledOpenTime].kline);
}

callback()
}
}


I am new to streams and some idea would be super appreciated
Should I create multiple TransformStream instances, one for each timeframe










share|improve this question














I have 1m ohlc data coming from an external WebSocket API and I am trying to generate 5m 15m 30m 1h 1d and 1w streams from it. My current TransformStream can convert 1m to one of the above mentioned timeframes



How do I do



one.pipe(five)
one.pipe(fifteen)
one.pipe(thirty)


without creating multiple instances of the original stream "one". Currently my single TransformStream instance emits the original data plus all the required timeframes.



class TimeFrameGenerator extends Transform {
constructor(timeframes) {
super({ readableObjectMode: true, writableObjectMode: true });
this.kline = {}

//Consider using an object for this, looping with a for in is much faster for this case than using an array or accessing object keys
this.timeframes = {
"1m": 1000 * 60,
"5m": 1000 * 60 * 5,
"15m": 1000 * 60 * 15,
"30m": 1000 * 60 * 30,
"1h": 1000 * 60 * 60,
"1d": 1000 * 60 * 60 * 24
}
}

_transform(rawKline, encoding, callback) {

this.push(rawKline);

const { pairId, base, quote, interval, openTime, closeTime, timestamp, open, high, low, close, baseVolume, quoteVolume, isFinal, raw } = rawKline;

if (typeof this.kline[pairId] === "undefined")
this.kline[pairId] = {};

for (let timeframe in this.timeframes) {
if (timeframe === interval)
continue;

const millis = this.timeframes[timeframe];

const resampledOpenTime = Math.floor(openTime / millis) * millis;
const resampledCloseTime = Math.ceil(openTime / millis) * millis - 1;

if (typeof this.kline[pairId][timeframe] === "undefined") {
this.kline[pairId][timeframe] = {};
}

if (typeof this.kline[pairId][timeframe][resampledOpenTime] === "undefined") {
const newKline = {
pairId: pairId,
base: base,
quote: quote,
interval: timeframe,
openTime: resampledOpenTime,
closeTime: resampledCloseTime,
timestamp: timestamp,
open: open,
high: high,
low: low,
close: close,
baseVolume: baseVolume,
quoteVolume: quoteVolume,
isFinal: false,
raw: null
}

this.kline[pairId][timeframe][resampledOpenTime] = {
kline: newKline,
prevBaseVol: 0,
prevQuoteVol: 0
}
}
else {
const { kline, prevBaseVol, prevQuoteVol } = this.kline[pairId][timeframe][resampledOpenTime];
kline.high = high > kline.high ? high : kline.high;
kline.low = low < kline.low ? low : kline.low;
kline.close = close;
kline.baseVolume = parseFloat((prevBaseVol + baseVolume).toFixed(PRECISION));
kline.quoteVolume = parseFloat((prevQuoteVol + quoteVolume).toFixed(PRECISION));
}

if (isFinal) {
const { prevBaseVol, prevQuoteVol } = this.kline[pairId][timeframe][resampledOpenTime];
this.kline[pairId][timeframe][resampledOpenTime].prevBaseVol = parseFloat((prevBaseVol + baseVolume).toFixed(PRECISION));
this.kline[pairId][timeframe][resampledOpenTime].prevQuoteVol = parseFloat((prevQuoteVol + quoteVolume).toFixed(PRECISION));
}

let keys = Object.keys(this.kline[pairId][timeframe]);

if (keys.length > 1) {
let previousTimestamp = keys.shift();
this.kline[pairId][timeframe][previousTimestamp]['kline'].isFinal = true;
this.push(this.kline[pairId][timeframe][previousTimestamp].kline);
delete this.kline[pairId][timeframe][previousTimestamp];
console.log(Object.keys(this.kline[pairId][timeframe]).length, Object.keys(this.kline[pairId]).length)
}

this.push(this.kline[pairId][timeframe][resampledOpenTime].kline);
}

callback()
}
}


I am new to streams and some idea would be super appreciated
Should I create multiple TransformStream instances, one for each timeframe







node.js stream node-streams






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 4:34









PirateAppPirateApp

1,92921831




1,92921831








  • 1





    Hmm... I think you did answer yourself it's simply one.pipe(three); one.pipe(five); //etc. All the streams will receive all the samples.

    – Michał Kapracki
    Nov 26 '18 at 14:55














  • 1





    Hmm... I think you did answer yourself it's simply one.pipe(three); one.pipe(five); //etc. All the streams will receive all the samples.

    – Michał Kapracki
    Nov 26 '18 at 14:55








1




1





Hmm... I think you did answer yourself it's simply one.pipe(three); one.pipe(five); //etc. All the streams will receive all the samples.

– Michał Kapracki
Nov 26 '18 at 14:55





Hmm... I think you did answer yourself it's simply one.pipe(three); one.pipe(five); //etc. All the streams will receive all the samples.

– Michał Kapracki
Nov 26 '18 at 14:55












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455155%2fhow-do-i-generate-multiple-streams-from-the-same-stream-in-node%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455155%2fhow-do-i-generate-multiple-streams-from-the-same-stream-in-node%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin