npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ohlc-aggregator

v1.0.16

Published

Aggregates ohlcv values into coarse grain intervals

Downloads

15

Readme

ohlc-aggregator

NPM NPM Downloads

Aggregates ohlcv candle values into predictable coarse-grained intervals. The intervals should be either minutes, hours, or days.

The difference between this package and other packages is that rather than simply aggregating each n candles into a group, we create predictable interval where the start time of each interval is divisible by n. If some candles from an interval are missing, we still create those interval.

Example 1

In converting 1m to 5m candles, a naive implementation creates only one group for the following 5 candles:

  1. time: 8:59am
  2. time: 9:00am
  3. time: 9:01am
  4. time: 9:02am
  5. time: 9:03am

However, this package creates two groups based on predictable timing intervals, i.e., the start of each timing interval is divisible by 5m:

  • Group 1: 8:55 to 8:59
  • Group 2: 9:00 to 9:05

We create two groups although some candles are missing from each group.

Example 2

Consider these candles:

  1. time: 8:59am
  2. time: 9:00am
  3. time: 9:01am
  4. time: 9:02am
  5. time: 9:03am
  6. time: 9:04am
  7. time: 9:05am
  8. time: 9:06am

A naive implementation will create these groups:

  • Group 1: 8:59 to 9:03, (complete candle)
  • Group 2: 9:04 to 9:06, (incomplete candle)

However, a predictable grouping would be:

  • Group 1: 8:55 to 9:00, (incomplete candle)
  • Group 1: 9:00 to 9:04, (complete candle)
  • Group 2: 9:05 to 9:09, (incomplete candle)

Again, the start of each timing interval is divisible by 5m.

Install

npm i -s ohlc-aggregator

Usage

// Converting 1m to 5m candles
let ohlc_aggregate = require("ohlc-aggregator");
var moment = require("moment");

// Converting 1m candles to 5min candles:
let result = ohlc_aggregate(
  [
    {
      time: moment("10/15/2017 8:59", "M/D/YYYY H:mm").valueOf(), // timestamp in milliseconds
      open: 1,
      high: 5,
      low: 1,
      close: 2,
      volume: 100
    },
    {
      time: moment("10/15/2017 9:00", "M/D/YYYY H:mm").valueOf(), // timestamp in milliseconds
      open: 1,
      high: 5,
      low: 1,
      close: 2,
      volume: 100
    },
    {
      time: moment("10/15/2017 9:01", "M/D/YYYY H:mm").valueOf(), // timestamp in milliseconds
      open: 3,
      high: 10,
      low: 0,
      close: 6,
      volume: 200
    }
  ],
  /*intervalRatio=*/ 5,   // ration between original interval and the desired interval
  /*intervalInSeconds=*/, 5 * 60 // Interval duration in seconds
  /*arrayTimeCoefficient=*/ 1 // Set this to 1000 if the time values are in second
);

console.log("result: ", JSON.stringify(result, null, 1));

See test/test.js for more examples.