candles v2.2.0
Candles
Candles gives you the ability to create candlesticks from your tick data. The candlesticks will be created from live tick data and not historical tick data. For example, via websocket clients.
About
This project started as an fork of gdax-candles. During use, it turned out that there was a need to use it on other exchanges. So it changed to candles.
Installation
To install the most recent release from npm, run:
npm i candles --saveExample
Here is a basic example using gdax as data provider:
const candles = require('candles');
const Gdax = require('gdax');
const product = ['ETH-USD', 'BTC-USD'];
const timeframe = ['30s', '1m', '2m', '5m', '15m', '30m', '1h', '3h', '6h', '12h', '24h'];
const options = {
timediff: {
enabled: true,
fixed: false,
value: 50,
samples: 1000
}
}
const Candlecollection = new candles(options);
Candlecollection.addProduct(product, timeframe);
const websocket = new Gdax.WebsocketClient(product);
websocket.on('message', data => {
if (data.time) {
Candlecollection.adjustClock(data.time);
}
if (data.type === 'match') {
Candlecollection.SetSeriesPrice(data.product_id, data.price, data.size);
}
});
Candlecollection.on('close', (candle, candles) => {
console.log(candle);
});
/*
Candlestick {
timestamp: 2018-11-08T15:20:00.000Z,
timeframe: '30s',
product: 'BTC-USD',
price: '6479.01000000',
open: '6479.01000000',
high: '6479.01000000',
low: '6479.00000000',
close: '6479.01000000',
volume: '0.39818029',
size: '0.01000000',
wick:
{ top: '0.00000000',
bottom: '0.01000000',
size: '0.01000000',
ratio: '1.00000000' },
closed: true,
market: 'bullish' }
*/Constructor
When a new instance is created, some options for fixing timelag between provider and your local system can be specified. By default lagtime correction is disabled.
const options = {
timediff: {
enabled: true,
fixed: false,
value: 50,
samples: 1000
}
}
const Candlecollection = new candles(options);enabledEnabling the correction of timelag.fixedFor a fixed lag between your provider and you local system set fixed to true. Otherwise the lagtime will be calculated by the given message timestamp, you have to pass.valueOnly when fixed lag selected, the lag in ms has to be specified.samplesFor the calculated lagtime an average of x samples will be calculated.
Functions
addProduct()
After creating an instance of candles, you have to add products and timeframes.
const product = ['ETH-USD', 'BTC-USD'];
const timeframe = ['30s', '1m', '2m', '5m', '15m', '30m', '1h', '3h', '6h', '12h', '24h'];
Candlecollection.addProduct(product, timeframe, serieslength);productYou can set a name for your own, but it make sense to put the name of the currencypair in here. You have to tell candle this name, every time you update the current candle.timeframeSetup the timeframe of your choice. The timeframes have to be writen in any number of s=seconds, m=minutes, h=hours and d=days. Feel free to use 13m or 7s as well as 21h. Product and Timeframe can both be arrays. Then for every product the given timeframe will be created. You can also seperate it.serieslengthoptional If you like to collect a amount of last x candles, set the amount here. If not passed, only the last candle will be hold in memmory.
Candlecollection.addProduct('ETH-USD', '30s');
Candlecollection.addProduct('BTC-USD', '1m', 60);removeProduct()
Removing products is as easy as adding, just provide the product(s) and timeframe(s).
// removing these timeframes in these products
const product = ['ETH-USD', 'BTC-USD'];
const timeframe = ['30s', '1m', '2m', '5m', '15m', '30m', '1h', '3h', '6h', '12h', '24h'];
Candlecollection.removeProduct(product, timeframe);productYou have to set the same name(s), you use for adding your product(s).timeframesame as above.
// removing one timeframe in a product
Candlecollection.removeProduct('ETH-USD', '30s');
Candlecollection.removeProduct('BTC-USD', '1m');SetSeriesPrice()
Candles need to know when the price of the product has been changed.
const product = 'BTC-USD';
const price = 6479.01;
const size = 0.39818029;
Candlecollection.SetSeriesPrice(product, price, size)productThe name you set in addProduct() before.priceThe actual price you got from provider.sizeThis is an optional parameter. If your provider don't give you any volume information, just leave it. Then your candlesticks volume will be 0.
adjustClock()
If you setup timelag correction, by enabling and setting samples count, you can pass your message timestamps to candles. Candles will build an average of these and your system clock, so the timestamp of the created candle will be more accorate.
Candlecollection.adjustClock(timestamp);timestampWill be the timestamp from your providers message.
getTimeDrift()
Gives you the average difference between exchange and local time. Depends on the timediff options you set. The value is in ms.
Candlecollection.getTimeDrift();Events
Events emitted from candles. For all events the current candle and the candle collection will be passed.
openwhen a new candle createdclosewhen a candle is closedcurrentwhen the current candle where updated
These events can be more specific by adding the timeframe and/or the productname.
open BTC-USDwhen a new candle of product 'BTC-USD' createdopen BTC-USD 1mwhen a new candle of product 'BTC-USD' with timeframe '1m' createdopen 1mwhen a new candle with timeframe '1m' created
This works also for close and current events to.
like:
Candlecollection.on('close BTC-USD 30s', (currentCandle, candles) => {
console.log(currentCandle);
});Properties
The candles instance provides you following properties.
Candles collects all candlesticks for you in a series. You can get it by using the product and timeframe as property name.
Candlecollection.series['BTC-USD'].timeframe['30s'].candlesCandlestick
This is an overview of the candlestick properties.
timestampTime when this candle started. Opening time.timeframeHow much time this candle burned.priceThe actual price.openOpening price.highHighest price in timeframe.lowLowest price in timeframe.closeClosing price.volumeVolume traded in this timeframe.sizeThe Candlesize.wick.topTop of the wick.wick.bottomBottom of the wick.wick.sizeSize of the wick.wick.ratioRation body to wick.closedIs this candle closed?marketMarket situation of this candle.