# candles-convert

> yet another util to convert the time-duration of OHLCV candles. results are either naively merged with n-candle chunks or can be time-aligned to the clock so that, ex: 15min candles start at 10:00, 10:15 etc

Latest version **0.0.12** (published 2023-06-14) · GPL license · 0 weekly downloads

## Install

```sh
npm install candles-convert
pnpm add candles-convert
yarn add candles-convert
bun add candles-convert
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.12 |
| Published | 2023-06-14 |
| First published | 2023-05-28 |
| Weekly downloads | 0 |
| License | GPL |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | stonkpunk |
| Maintainers | stonkpunk |

## Links

- npm: https://www.npmjs.com/package/candles-convert
- Homepage: https://github.com/stonkpunk/my-npm-modules/tree/main/candles-convert
- npm.io page: https://npm.io/package/candles-convert

## Recent versions

- 0.0.12 (latest) — 2023-06-14
- 0.0.11 — 2023-05-28
- 0.0.1 — 2023-05-28

## README

# candles-convert

Yet another util to convert the time-duration of OHLCV candles. results are either naively merged with n-candle chunks or can be time-aligned to the clock so that, ex: 15min candles start at 10:00, 10:15 etc

Experimental, use at your own risk. 

## Installation

```sh
npm i candles-convert
```

## Usage 

```javascript
var {
    convertToNMinuteIntervalsFromDate,
    convertToNMinuteIntervalsFromTime,
    convertToNPeriodIntervals} = require('candles-convert');

var timeRoundingFunction = Math.round; //round to nearest time boundary. 11:59 merges into candle for 12:00. makes for a cleaner example here but is less realistic.
// var timeRoundingFunction = Math.floor; //default -- round down. 11:59 merges into candle for 11:00 [assuming 1hr interval]

//candles with time in unix epoch seconds, and an arbitrary extra field "some_extraParam"
const candlesWithTime = [
  { time: 1685267880, open: 10, high: 12, low: 9,  close: 11, volume: 100, some_extraParam: 1},
  { time: 1685268060, open: 11, high: 13, low: 10, close: 12, volume: 150, some_extraParam: 2},
  { time: 1685268180, open: 12, high: 14, low: 11, close: 13, volume: 120, some_extraParam: 3},
  { time: 1685268480, open: 13, high: 15, low: 12, close: 14, volume: 180, some_extraParam: 4},
  { time: 1685268840, open: 14, high: 16, low: 13, close: 15, volume: 200, some_extraParam: 5},
  { time: 1685269020, open: 15, high: 17, low: 14, close: 16, volume: 140, some_extraParam: 6}
];

const convertedCandles = convertToNMinuteIntervalsFromTime(candlesWithTime, 15, timeRoundingFunction);
console.log(convertedCandles);

//notice how any 'extra params' are absorbed into the result form the first candle in each merged set
// [
//   { "time": 1685268000, "open": 10, "high": 14, "low": 9, "close": 13, "volume": 370, "nSrcCandles": 3, "some_extraParam": 1 },
//   { "time": 1685268900, "open": 13, "high": 17, "low": 12, "close": 16, "volume": 520, "nSrcCandles": 3, "some_extraParam": 4 }
// ]

//candles with date as iso string
const candlesWithDateStrings = [
  { date: "2023-05-28T09:58:00.000Z", open: 10, high: 12, low: 9, close: 11, volume: 100 },
  { date: "2023-05-28T10:01:00.000Z", open: 11, high: 13, low: 10, close: 12, volume: 150 },
  { date: "2023-05-28T10:03:00.000Z", open: 12, high: 14, low: 11, close: 13, volume: 120 },
  { date: "2023-05-28T10:08:00.000Z", open: 13, high: 15, low: 12, close: 14, volume: 180 },
  { date: "2023-05-28T10:14:00.000Z", open: 14, high: 16, low: 13, close: 15, volume: 200 },
  { date: "2023-05-28T10:17:00.000Z", open: 15, high: 17, low: 14, close: 16, volume: 140 }
];

const convertedCandles2 = convertToNMinuteIntervalsFromDate(candlesWithDateStrings, 15, timeRoundingFunction);
console.log(convertedCandles2);

// [
//   { "date": "2023-05-28T10:00:00.000Z", "open": 10, "high": 14, "low": 9, "close": 13, "volume": 370, "nSrcCandles": 3 },
//   { "date": "2023-05-28T10:15:00.000Z", "open": 13, "high": 17, "low": 12, "close": 16, "volume": 520, "nSrcCandles": 3 }
// ]

//naively merge candles as chunks of length n
const convertedCandles3 = convertToNPeriodIntervals(candlesWithDateStrings, 3);
console.log(convertedCandles3);

// [
//   { "date": "2023-05-28T09:58:00.000Z", "open": 10, "high": 14, "low": 9, "close": 13, "volume": 370, "nSrcCandles": 3 },
//   { "date": "2023-05-28T10:08:00.000Z", "open": 13, "high": 17, "low": 12, "close": 16, "volume": 520, "nSrcCandles": 3 }
// ]
```

[![stonks](https://i.imgur.com/UpDxbfe.png)](https://www.npmjs.com/~stonkpunk)

---
_Source: https://npm.io/package/candles-convert · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
