0.0.1 • Published 7 months ago

olhc-candles-emitter-multiscale v0.0.1

Weekly downloads
-
License
GPL
Repository
-
Last release
7 months ago

olhc-candles-emitter-multiscale

class for converting a stream of OHLCV (Open/High/Low/Close/Volume) candlestick data into different timeframe candles.

given a stream of period-1 candles, emits events every n candles with period-n candles merged from the period-1 candles.

Installation

npm i olhc-candles-emitter-multiscale

Usage

const OHLCVConverter = require('olhc-candles-emitter-multiscale').OHLCVConverter;

//in this example we wait for 3 and 5 period candles based on a stream of 1 period candles
const ohlcvConverter = new OHLCVConverter([3, 5]);

// Handle new higher-timeframe candle event
// every n periods, an n-period candle is emitted
ohlcvConverter.on('newCandle', ({ period, candle }) => {
    console.log(`New ${period}-candle:`, candle);
});

// Submit a new candle
setInterval(function(){
    //format {o h l c v} OR {open high low close volume}
    ohlcvConverter.submitCandle({ o: 100, h: 110+Math.random()*100, l: 95-Math.random()*100, c: 105, v: 1000 });

    // Retrieve and log candles in progress
    const inProgress = ohlcvConverter.getCandlesInProgress();
    console.log('Candles in progress:', inProgress);

    // Candles in progress: [
    //     {
    //         period: 3,
    //         newCandleAge: 0, //note how candle in progress is null if we have just emitted a new candle for this period
    //         candleMerged: null,
    //         candlesIncluded: []
    //     },
    //     {
    //         period: 5,
    //         newCandleAge: 1,
    //         candleMerged: {
    //             open: 100,
    //             high: 202.67194182182755,
    //             low: 92.1559343731847,
    //             close: 105,
    //             volume: 1000
    //         },
    //         candlesIncluded: [ [Object] ]
    //     }
    // ]
},100);

See Also

stonks

0.0.1

7 months ago