Comparing version 2.0.0 to 2.1.0
{ | ||
"name": "candles", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Generate candlesticks from your tick data in realtime.", | ||
@@ -13,4 +13,8 @@ "main": "index.js", | ||
"candlestick", | ||
"realtime", | ||
"ohlcv", | ||
"technical-analysis" | ||
"ohlc", | ||
"time series", | ||
"trading", | ||
"stock" | ||
], | ||
@@ -17,0 +21,0 @@ "author": "Flippo24", |
# 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. | ||
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](https://github.com/swimclan/gdax-candles). | ||
This project started as an fork of [gdax-candles](https://github.com/swimclan/gdax-candles). | ||
During use, it turned out that there was a need to use it on other exchanges. So it changed to candles. | ||
@@ -75,2 +75,3 @@ | ||
market: 'bullish' } | ||
*/ | ||
``` | ||
@@ -96,4 +97,4 @@ ### Constructor | ||
* `enabled` Enabling the correction of timelag. | ||
* `fixed` for a fixed lag between your provider and you local system set fixed to true. Otherwise the lagtime will be calculated be the given message timestamp, you have to pass. | ||
* `value` only when fixed lag selected, the lag in ms has to be specified. | ||
* `fixed` For 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. | ||
* `value` Only when fixed lag selected, the lag in ms has to be specified. | ||
* `samples` For the calculated lagtime an average of x samples will be calculated. | ||
@@ -110,14 +111,35 @@ | ||
const timeframe = ['30s', '1m', '2m', '5m', '15m', '30m', '1h', '3h', '6h', '12h', '24h']; | ||
Candlecollection.addProduct(product, timeframe); | ||
Candlecollection.addProduct(product, timeframe, serieslength); | ||
``` | ||
* `product` You 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. | ||
* `timeframe` Setup 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. | ||
* `timeframe` Setup 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. | ||
* `serieslength` [optional] 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. | ||
```js | ||
Candlecollection.addProduct('ETH-USD', '30s'); | ||
Candlecollection.addProduct('BTC-USD', '1m'); | ||
Candlecollection.addProduct('BTC-USD', '1m', 60); | ||
``` | ||
#### removeProduct() | ||
Removing products is as easy as adding, just provide the product(s) and timeframe(s). | ||
```js | ||
// 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); | ||
``` | ||
* `product` You have to set the same name(s), you use for adding your product(s). | ||
* `timeframe` same as above. | ||
```js | ||
// removing one timeframe in a product | ||
Candlecollection.removeProduct('ETH-USD', '30s'); | ||
Candlecollection.removeProduct('BTC-USD', '1m'); | ||
``` | ||
#### SetSeriesPrice() | ||
@@ -134,3 +156,3 @@ | ||
* `product` The name you set in addProduct() before. | ||
* `product` The name you set in addProduct() before. | ||
* `price` The actual price you got from provider. | ||
@@ -141,3 +163,3 @@ * `size` This is an optional parameter. If your provider don't give you any volume information, just leave it. Then your candlesticks volume will be 0. | ||
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 more accorate. | ||
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. | ||
@@ -150,5 +172,13 @@ ```js | ||
#### getTimeDrift() | ||
Gives you the average difference between exchange and local time. Depends on the timediff options you set. The value is in ms. | ||
```js | ||
Candlecollection.getTimeDrift(); | ||
``` | ||
### Events | ||
Events emitted from candles. For all events the current candle will be passed. | ||
Events emitted from candles. For all events the current candle and the candle collection will be passed. | ||
@@ -165,3 +195,3 @@ * `open` when a new candle created | ||
This works for close and current events to. | ||
This works also for close and current events to. | ||
@@ -171,4 +201,4 @@ like: | ||
```js | ||
Candlecollection.on('close BTC-USD 30s', candle => { | ||
console.log(candle); | ||
Candlecollection.on('close BTC-USD 30s', currentCandle, candles => { | ||
console.log(currentCandle); | ||
}); | ||
@@ -181,3 +211,3 @@ ``` | ||
Candles collects all candlesticks for you in a series. You can get it by using the product and timeframe as propertienames. | ||
Candles collects all candlesticks for you in a series. You can get it by using the product and timeframe as property name. | ||
@@ -184,0 +214,0 @@ ```js |
@@ -17,7 +17,7 @@ const Clock = require('./Clock'); | ||
}; | ||
this.clock = new Clock.getInstance(); | ||
this.clock = new Clock; | ||
this.clock.setOptions(this.options.timediff) | ||
} | ||
addProduct(product, timeframe) { | ||
addProduct(product, timeframe, serieslength=0) { | ||
if (!product) { | ||
@@ -31,27 +31,28 @@ throw 'product is missing' | ||
product.forEach(product => { | ||
this.addTimeframe(product, timeframe); | ||
this.addTimeframe(product, timeframe, serieslength); | ||
}) | ||
} else { | ||
this.addTimeframe(product, timeframe); | ||
this.addTimeframe(product, timeframe, serieslength); | ||
} | ||
}; | ||
addTimeframe(product, timeframe) { | ||
addTimeframe(product, timeframe, serieslength) { | ||
if (Array.isArray(timeframe)) { | ||
timeframe.forEach(timeframe => { | ||
this.addSeries(product, timeframe); | ||
this.addSeries(product, timeframe, serieslength); | ||
}) | ||
} else { | ||
this.addSeries(product, timeframe); | ||
this.addSeries(product, timeframe, serieslength); | ||
} | ||
} | ||
addSeries(product, timeframe) { | ||
if (!this.clock || !this.clock.resolutions[timeframe]) { | ||
this.clock = new Clock.getInstance(timeframe).start(); | ||
addSeries(product, timeframe, serieslength) { | ||
if (!this.clock.resolutions[timeframe]) { | ||
this.clock.addResolution(timeframe); | ||
this.clock.start(); | ||
} | ||
var series = new Series(product, timeframe); | ||
var series = new Series(product, timeframe, serieslength); | ||
this.clock.on('tick '+ timeframe, series.onSeriesClockTick.bind(series)); | ||
series.on('open',this.seriesOpen.bind(this)); | ||
series.on('close',this.seriesClose.bind(this)); | ||
series.on('close',this.seriesClose.bind(this)); | ||
if (!this.series[product]) { | ||
@@ -66,6 +67,27 @@ this.series[product] = {}; | ||
getTimeDrift() { | ||
return this.clock.timediff.drift | ||
} | ||
removeProduct(product, timeframe) { | ||
if (this.series[product].timeframe[timeframe]) { | ||
this.clock.off('tick '+ timeframe, this.series[product].timeframe[timeframe].onSeriesClockTick.bind(this.series[product].timeframe[timeframe])); | ||
this.series[product].timeframe[timeframe].off('open',this.seriesOpen.bind(this)); | ||
this.series[product].timeframe[timeframe].off('close',this.seriesOpen.bind(this)); | ||
delete this.series[product].timeframe[timeframe]; | ||
} | ||
if (this.series[product].timeframe.lenght == 0) { | ||
delete this.series[product]; | ||
} | ||
} | ||
SetSeriesPrice(product, price, size=0) { | ||
Object.keys(this.series[product].timeframe).forEach(timeframe => { | ||
if (!this.series[product].timeframe[timeframe]) { | ||
return; | ||
} | ||
this.series[product].timeframe[timeframe].price = Number.parseFloat(price); | ||
this.series[product].timeframe[timeframe].currentCandle.updatePrice(Number.parseFloat(price), Number.parseFloat(size).toFixed(8)); | ||
this.emit('current ' + this.series[product].product + ' ' + this.series[product].timeframe[timeframe].timeframe, this.series[product].timeframe[timeframe].currentCandle); | ||
this.emit('current ' + this.series[product].product, this.series[product].timeframe[timeframe].currentCandle); | ||
this.emit('current ' + this.series[product].timeframe[timeframe].timeframe, this.series[product].timeframe[timeframe].currentCandle); | ||
@@ -76,14 +98,14 @@ this.emit('current', this.series[product].timeframe[timeframe].currentCandle); | ||
seriesClose(currentCandle) { | ||
this.emit('close ' + currentCandle.product, currentCandle); | ||
this.emit('close ' + currentCandle.product + ' ' + currentCandle.timeframe, currentCandle); | ||
this.emit('close ' + currentCandle.timeframe, currentCandle); | ||
this.emit('close', currentCandle); | ||
seriesClose(currentCandle, candles) { | ||
this.emit('close ' + currentCandle.product, currentCandle, candles); | ||
this.emit('close ' + currentCandle.product + ' ' + currentCandle.timeframe, currentCandle, candles); | ||
this.emit('close ' + currentCandle.timeframe, currentCandle, candles); | ||
this.emit('close', currentCandle, candles); | ||
} | ||
seriesOpen(currentCandle) { | ||
this.emit('open ' + currentCandle.product, currentCandle); | ||
this.emit('open ' + currentCandle.product + ' ' + currentCandle.timeframe, currentCandle); | ||
this.emit('open ' + currentCandle.timeframe, currentCandle); | ||
this.emit('open', currentCandle); | ||
seriesOpen(currentCandle, candles) { | ||
this.emit('open ' + currentCandle.product, currentCandle, candles); | ||
this.emit('open ' + currentCandle.product + ' ' + currentCandle.timeframe, currentCandle, candles); | ||
this.emit('open ' + currentCandle.timeframe, currentCandle, candles); | ||
this.emit('open', currentCandle, candles); | ||
} | ||
@@ -90,0 +112,0 @@ |
@@ -49,3 +49,3 @@ class Candlestick { | ||
this.wick.size = Math.abs(Number.parseFloat(this.wick.top) - Number.parseFloat(this.wick.bottom)).toFixed(8); | ||
if (Number.parseFloat(this.size) > 0) { | ||
if (Number.parseFloat(this.size)> 0) { | ||
this.wick.ratio = (Number.parseFloat(this.wick.size) / Number.parseFloat(this.size)).toFixed(8); | ||
@@ -52,0 +52,0 @@ } else { |
@@ -1,2 +0,2 @@ | ||
const EventEmitter = require('events') | ||
const EventEmitter = require('events') | ||
const { timeframeToSec, nextTime } = require('./utils'); | ||
@@ -56,7 +56,7 @@ const {average} = require('./utils'); | ||
this.timediff.fixed = options.fixed; | ||
this.timediff.value = options.value; | ||
this.timediff.value = options.value; | ||
this.timediff.samples = options.samples; | ||
if (this.timediff.enabled && this.timediff.fixed) { | ||
this.timediff.drift = this.timediff.value; | ||
} | ||
} | ||
} | ||
@@ -72,14 +72,2 @@ | ||
module.exports = Clock; | ||
let instance; | ||
module.exports.getInstance = function(resolution) { | ||
if (!instance) { | ||
instance = new Clock(); | ||
} | ||
if (resolution) { | ||
instance.addResolution(resolution); | ||
} | ||
return instance; | ||
} | ||
module.exports = Clock; |
@@ -6,6 +6,7 @@ const Candlestick = require('./Candlestick'); | ||
class Series extends EventEmitter { | ||
constructor(product, timeframe) { | ||
constructor(product, timeframe, serieslength) { | ||
super(); | ||
this.product = product; | ||
this.timeframe = timeframe; | ||
this.length = serieslength; | ||
this.nexttime = new Date(); | ||
@@ -20,3 +21,3 @@ this.candles = []; | ||
this.currentCandle = new Candlestick(this.lastClose || this.price, this.product, this.timeframe, this.nexttime); | ||
this.emit('open', this.currentCandle); | ||
this.emit('open', this.currentCandle, this.candles); | ||
} | ||
@@ -29,3 +30,6 @@ | ||
this.candles.push(this.currentCandle) | ||
this.emit('close', this.currentCandle); | ||
if(this.candles.length > this.length) { | ||
this.candles.shift() | ||
} | ||
this.emit('close', this.currentCandle, this.candles); | ||
} | ||
@@ -32,0 +36,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
55467
228
0
294