Giter Club home page Giter Club logo

candlestick-convert's Introduction

candlestick-convert

Coverage Status DeepScan grade npm

This package allows you to batch OHLCV candlesticks or create them from trade (tick) datasets.

Supported formats:

  • OHLCV (CCXT format) [[time,open,high,low,close,volume]]
  • OHLCV JSON [{time: number,open: number, high: number, low: number close: number, volume: number}]
  • Trade JSON [{price: number, quantity: number, time:number}]

Features:

  • Typescript support!
  • CCXT support
  • No Dependencies
  • Performance single loop used
  • Skip missing candles

Important!:

  • Intervals are only supported as integers in seconds (1 minute = 60, 2 minutes = 120...).
  • Only positive integer multiples are allowed between the base interval and the new interval, e.g., 60->120, 60->180.

Install:

npm install candlestick-convert

Available functions:

import {batchCandleArray, batchCandleArrayCustomInterval, batchCandleJSON, batchTicksToCandle, ticksToTickChart} from "candlestick-convert";

batchCandleArray(candledata: OHLCV[], baseInterval = 60, targetInterval = 300, includeOpenCandle = false)
//return OHLCV[]

batchCandleArrayCustomInterval(candleData: OHLCV[], intervalFunction: IntervalFunction, includeOpenCandle = false)
//return OHLCV[]

batchCandleJSON(candledata: IOHLCV [], baseInterval = 60, argetInterval = 300)
// return IOHLCV[]

batchTicksToCandle(tradedata: TradeTick[], interval = 60,  includeOpenCandle = false)
// return IOHLCV[]

ticksToTickChart(tradedata: TradeTick[], numberOfTicks = 5)
// return IOHLCV[]

** includeOpenCandle allow non-complete candles in the result, useful for not normalized input data

Types

export type IOHLCV = {
  time: number,
  open: number,
  high: number,
  low: number,
  close: number,
  volume: number,
};

export type OHLCV = [number, number, number, number, number, number];
// OpenTime, Open, High, Low, Close, Volume

export type IntervalFunction = (timeStamp: number) => number;
// return the OpenTime for the given timestamp

export type TradeTick = {
  price: number,
  quantity: number,
  time: number,
};

Examples

CCXT OHLCV:

import { batchCandleJSON } from "candlestick-convert";

const link_btc_1m = [
  {
    time: 1563625680000,
    open: 0.00024824,
    high: 0.00024851,
    low: 0.00024798,
    close: 0.00024831,
    volume: 2264,
  },
  {
    time: 1563625740000,
    open: 0.00024817,
    high: 0.00024832,
    low: 0.00024795,
    close: 0.00024828,
    volume: 3145,
  },
];

const baseFrame = 60; // 60 seconds
const newFrame = 120; // 120 seconds

// Convert to 2m Candles

const link_btc_2m = batchCandleJSON(link_btc_1m, baseFrame, newFrame);

Tick Chart:

import { ticksToTickChart, TradeTick } from "candlestick-convert";

const adabnb_trades = [
  {
    time: "1564502620356",
    side: "sell",
    quantity: "4458",
    price: "0.00224",
    tradeId: "1221272",
  },
  {
    time: "1564503133949",
    side: "sell",
    quantity: "3480",
    price: "0.002242",
    tradeId: "1221273",
  },
  {
    time: "1564503134553",
    side: "buy",
    quantity: "51",
    price: "0.002248",
    tradeId: "1221274",
  },
];

const filtered_adabnb_trades: TradeTick[] = adabnb_trades.map((trade: any) => ({
  time: trade.time,
  quantity: trade.quantity,
  price: trade.price,
}));

const batchSize = 2; // Every TickCandle consist 2 trade
const tickChart = ticksToTickChart(filtered_adabnb_trades, batchSize);

candlestick-convert's People

Contributors

johanneshoppe avatar jorisw avatar valamidev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

candlestick-convert's Issues

Issue with converting

Hey, I have following issue with converting data. This is my code:

    let rawData = await ex.fetchOHLCV(market, '4h');
    rawData.forEach((row) => {
        let ts = new Date(row[0]);
        console.log(ts.toUTCString(), row.join(', '))
    });
    console.log('post')
    let data1 = candlestick.batchCandleArray(rawData, 4 * 60 * 60, 24 * 60 * 60);
    data1.forEach((row) => {
        let ts = new Date(row[0]);
        console.log(ts.toUTCString(), row.join(', '))
    });

and fragment of output:

Wed, 29 Apr 2020 00:00:00 GMT 1588118400000, 196.41724485, 199.60277445, 196.41724485, 197.91143927, 7186.48057256
Wed, 29 Apr 2020 04:00:00 GMT 1588132800000, 198.035, 206.97029999, 197.51, 205.21983725, 11727.01122452
Wed, 29 Apr 2020 08:00:00 GMT 1588147200000, 205.27223332, 209.77, 204.81732608, 207.48, 8875.60254148
Wed, 29 Apr 2020 12:00:00 GMT 1588161600000, 207.48, 210.86986835, 207.35389862, 209.68324844, 7072.17556023
Wed, 29 Apr 2020 16:00:00 GMT 1588176000000, 209.76355752, 218.77627466, 208.85, 215.96924559, 21403.18326119
Wed, 29 Apr 2020 20:00:00 GMT 1588190400000, 215.96, 216.72, 212.79458242, 215.35098483, 5371.18171718
Thu, 30 Apr 2020 00:00:00 GMT 1588204800000, 215.42830423, 226.35514441, 214.31875445, 225.57612125, 12046.65930005
Thu, 30 Apr 2020 04:00:00 GMT 1588219200000, 225.54010584, 227.13385486, 216.3234, 220.36815671, 14595.39307772
Thu, 30 Apr 2020 08:00:00 GMT 1588233600000, 220.35179971, 220.805, 202.19405314, 211.74182454, 15729.63045946
Thu, 30 Apr 2020 12:00:00 GMT 1588248000000, 211.74, 213.1836997, 206.35780003, 208.22505329, 8436.43548563
Thu, 30 Apr 2020 16:00:00 GMT 1588262400000, 208.05120001, 212.68949134, 202.0834746, 211.5, 15277.35560464
Thu, 30 Apr 2020 20:00:00 GMT 1588276800000, 211.50302713, 212.29, 205.29320752, 206.35436145, 7487.72388672
Fri, 01 May 2020 00:00:00 GMT 1588291200000, 206.03203023, 210.29056546, 205.94860207, 208.69240056, 8525.26761028


Mon, 27 Apr 2020 00:00:00 GMT 1587945600000, 194.80994837, 199.79820676, 192.37058705, 197.15280003, 29715.060915050002
Tue, 28 Apr 2020 00:00:00 GMT 1588032000000, 197.25393884, 197.65052676, 189.35838494, 194.545, 24953.3331591
Wed, 29 Apr 2020 00:00:00 GMT 1588118400000, 194.62622772, 199.60277445, 192.28000001, 197.91143927, 28518.834387939998
Thu, 30 Apr 2020 00:00:00 GMT 1588204800000, 198.035, 226.35514441, 197.51, 225.57612125, 66495.81360465

Closing price for Thu, 30 Apr 2020 00:00:00 GMT 1588204800000 should be closing price of last 4h candle that started that day: Thu, 30 Apr 2020 20:00:00 GMT 1588276800000, 211.50302713, 212.29, 205.29320752, **206.35436145**, 7487.72388672 not Thu, 30 Apr 2020 00:00:00 GMT 1588204800000, 215.42830423, 226.35514441, 214.31875445, 225.57612125, 12046.65930005

or am I wrong?

How to convert 1m array of data into month candles?

Hello, thank you for your script, very usefull!

Got a question...

Let's say I have 2 months worth of 1m candlestick data, so an array of 2 * <month_days> * 24 * 60 * 60.

Considering that february has 28 days and march has 31 days and I have an array of all 1m data for these 2 months, how would I make the function call to transform it to 1M if months can have different days?

const res = batchCandleJSON(
  candledata,
  60,
  (28|30|31)? * 24 * 60 * 60 
);

I would expect res to have an array with 2 candlestick values.

Essentially, I'd like to implement this function as well:

function timeframeToSeconds(timeframe: string): number {
  const map = {
    'm': 60,
    'h': 60 * 60,
    'd': 24 * 60 * 60,
    'w': 7 * 24 * 60 * 60,
    'M': ?? * 24 * 60 * 60 // what value to put here?
  };
  const res = timeframe.match(/(?<value>[0-9]+)(?<unit>[mhdwM])/);
  const value = parseInt(res.groups['value']);
  const unit = res.groups['unit'];

  return Object.keys(map).includes(unit) ? value * map[unit] : -1;
}

Thanks for the clarification :)

last candle missing after conversion

Hi

I noticed that when I convert 1m candle data to 5m and doing it rather 4th minute of time, the last candle (actually active candle) is missing

Here's how tp reproduce:

let candles = await binance.candles({ symbol: 'STORJUSDT', interval: '1m', limit: 900 })
let m5Candles = batchCandleJSON(candles.map(d => { return {
        time: d.openTime,
        open: d.open,
        high: d.high,
        low: d.low,
        close: d.close,
        volume: d.volume,
    } }), 60, 300)
let b5mcandles = await binance.candles({ symbol: 'STORJUSDT', interval: '5m', limit: 180 })
console.log('m5Candles: ', JSON.stringify(m5Candles.map(d => d.close.toFixed(4)).slice(-3)))
console.log('binance 5mcandles:', JSON.stringify(b5mcandles.map(d => d.close).slice(-3)))

if you run these codes for example on 14:41 of the time you can get this kind of data:

m5Candles:  ["0.9420","0.9410","0.9455"]
binance 5mcandles: ["0.94100000","0.94550000","0.94380000"]

If I change 76th line on index.js

from this:

if (j === convertRatio) {

to this:

if (j === convertRatio || (i == candledata.length - 1)) {

it gives me the correct conversation with last candle:

m5Candles:  ["0.9410","0.9455","0.9434"]
binance 5mcandles: ["0.94100000","0.94550000","0.94340000"]

I don't have enought time to pull your code and make a pull request for this but suggest you apply this fix from your side.

Btw, thanks for the whole library..

Ticks to OHLC: One candle is missing?

Hello,

I am not so familiar with the processing of financial data, so I hope for your patience. I have either not understood the underlying concept or I have found a bug. ๐Ÿ˜‡

Demo
https://tvjs.io/play?a=hfht2hd0
The whole example can be found there.

The data
I use data from Uniswap, every trade is readable. I want to group the raw data by different time frames to be able to display them in a chart. As an example we can take this dead fake-pair:

https://app.astrotools.io/pair-explorer/0x97289c815c495e53ba1fa12c1fdfb61ad6a2867f
https://tools.unimonitor.io/#/pair-explore/0x97289c815c495e53ba1fa12c1fdfb61ad6a2867f
https://www.dextools.io/app/uniswap/pair-explorer/0x97289c815c495e53ba1fa12c1fdfb61ad6a2867f

As you can see, we have 43 trades. The first trades occurred at 2021-02-08T07:16:33.000Z and the last one was at 2021-02-08T15:06:32.000Z. If I count the hours in between, I come up with 9 candles for a 1h time frame.

Screen Shot 2021-02-12 at 16 57 38

The problem

I use the following function:

var candles = batchTicksToCandle(swaps, 60 * 60);

Unfortunately, I then only get grouped OHLC data with 8 candles. Something must be wrong there. On the other websites shown above, however, you always see 9 candles. Have I used the library incorrectly?

Screen Shot 2021-02-12 at 17 09 02

Here you see 8 candles in my version:

Screen Shot 2021-02-12 at 17 11 42

But 9 candles on the other websites:

Screen Shot 2021-02-12 at 17 13 03

Screen Shot 2021-02-12 at 17 13 47

I would be very grateful if you could help in any way! Many thanks in adance! ๐Ÿ‘

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.