Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #186 from 4miners/1.1.1
Browse files Browse the repository at this point in the history
Backport #185
  • Loading branch information
karmacoma authored Jun 7, 2017
2 parents f471a53 + 4eea902 commit 56a9729
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 76 deletions.
3 changes: 0 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@ module.exports = function (grunt) {
}
});

// Making grunt default to force in order not to break the project.
grunt.option('force', true);

// Default task(s).
grunt.registerTask('default', ['watch']);

Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"cacheTTL" : 20,
"enableCandles" : true,
"updateCandlesInterval" : 30000,
"candlesBuildTimeframe" : 2592000,
"enableOrders" : true,
"updateOrdersInterval" : 15000
}
124 changes: 75 additions & 49 deletions lib/candles/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ function AbstractCandles (client) {
this.name = 'exchange';
this.key = this.name + 'Candles';
this.url = '';
this.start = 0;
this.start = null;
this.end = null;
this.last = null;

this.response = {
Expand All @@ -29,19 +30,19 @@ function AbstractCandles (client) {
this.duration = 'minute';
this.durations = ['minute', 'hour', 'day'];

this.retrieveTrades = function (start, cb) {
if (!start) { start = self.start || 0; }

this.retrieveTrades = function (start, end, cb) {
var found = false,
results = [];

console.log('Candles:', 'Retrieving trades from', self.name + '...');

async.doUntil(
function (next) {
console.log ('Candles: Start: ' + (start ? new Date(start*1000).toISOString() : 'N/A') + ' End: ' + (end ? new Date(end*1000).toISOString() : 'N/A'));

request.get({
url : self.url + start,
json : true
url: self.url + (start ? '&start=' + start : '') + (end ? '&end=' + end : ''),
json: true
}, function (err, resp, body) {
if (err || resp.statusCode !== 200) {
return next(err || 'Response was unsuccessful');
Expand All @@ -59,9 +60,9 @@ function AbstractCandles (client) {
}

if (self.validTrades(results, data)) {
console.log('Candles:', start.toString(), '=> Found', data.length.toString(), 'trades');
console.log('Candles:', (start ? start.toString() : 'N/A'), 'to', (end ? end.toString() : 'N/A'), '=> Found', data.length.toString(), 'trades');
results = self.acceptTrades(results, data);
start = self.nextStart(data);
end = self.nextEnd(data);
return next();
} else {
found = true;
Expand All @@ -83,8 +84,8 @@ function AbstractCandles (client) {
);
};

this.nextStart = function (data) {
return 0;
this.nextEnd = function (data) {
return null;
};

this.rejectTrades = function (data) {
Expand Down Expand Up @@ -151,7 +152,7 @@ function AbstractCandles (client) {
btcVolume : _.reduce(period, function (memo, t) { return (memo + parseFloat(t[self.candle.amount]) * parseFloat(t[self.candle.price])); }, 0.0).toFixed(8),
firstTrade : _.first(period)[self.candle.id],
lastTrade : _.last(period)[self.candle.id],
nextStart : self.nextStart(period),
nextEnd : self.nextEnd(period),
numTrades : _.size(period)
};
});
Expand Down Expand Up @@ -201,39 +202,53 @@ function AbstractCandles (client) {
};

this.buildCandles = function (cb) {
async.eachSeries(self.durations, function (duration, callback) {
self.duration = duration;
_buildCandles(callback);
}, function (err, results) {
async.waterfall([
function (waterCb) {
return self.retrieveTrades(self.start, self.end, waterCb);
},
function (trades, waterCb) {
async.eachSeries(self.durations, function (duration, eachCb) {
self.duration = duration;
return _buildCandles(trades, eachCb);
}, function (err, results) {
if (err) {
return waterCb(err);
} else {
return waterCb(null);
}
});
}
],
function (err, results) {
if (err) {
return cb(err);
} else {
return cb(null);
return cb(null, results);
}
});
};

var _buildCandles = function (cb) {
var _buildCandles = function (trades, cb) {
console.log('Candles:', 'Building', self.duration, 'candles for', self.name + '...');

async.waterfall([
function (callback) {
function (waterCb) {
client.DEL(self.candleKey(), function (err, res) {
if (err) {
return callback(err);
return waterCb(err);
} else {
self.retrieveTrades(null, callback);
return waterCb();
}
});
},
function (results, callback) {
self.groupTrades(results, callback);
function (waterCb) {
return self.groupTrades(trades, waterCb);
},
function (results, callback) {
self.sumTrades(results, callback);
function (results, waterCb) {
return self.sumTrades(results, waterCb);
},
function (results, callback) {
self.saveCandles(results, callback);
function (results, waterCb) {
return self.saveCandles(results, waterCb);
}
],
function (err, results) {
Expand All @@ -246,21 +261,6 @@ function AbstractCandles (client) {
};

this.updateCandles = function (cb) {
async.eachSeries(self.durations, function (duration, callback) {
self.duration = duration;
_updateCandles(callback);
}, function (err, results) {
if (err) {
return cb(err);
} else {
return cb(null);
}
});
};

var _updateCandles = function (cb) {
console.log('Candles:', 'Updating', self.duration, 'candles for', self.name + '...');

async.waterfall([
function (callback) {
client.LRANGE(self.candleKey(), -1, -1, function (err, reply) {
Expand All @@ -274,17 +274,43 @@ function AbstractCandles (client) {
}
});
},
function (reply, callback) {
return self.retrieveTrades(reply.nextStart, callback);
function (last, waterCb) {
return self.retrieveTrades(last.nextEnd, null, waterCb);
},
function (results, callback) {
return self.groupTrades(results, callback);
function (trades, waterCb) {
async.eachSeries(self.durations, function (duration, eachCb) {
self.duration = duration;
return _updateCandles(trades, eachCb);
}, function (err, results) {
if (err) {
return waterCb(err);
} else {
return waterCb(null);
}
});
}
],
function (err, results) {
if (err) {
return cb(err);
} else {
return cb(null);
}
});
};

var _updateCandles = function (trades, cb) {
console.log('Candles:', 'Updating', self.duration, 'candles for', self.name + '...');

async.waterfall([
function (waterCb) {
return self.groupTrades(trades, waterCb);
},
function (results, callback) {
return self.sumTrades(results, callback);
function (results, waterCb) {
return self.sumTrades(results, waterCb);
},
function (results, callback) {
return self.saveCandles(results, callback);
function (results, waterCb) {
return self.saveCandles(results, waterCb);
}
],
function (err, results) {
Expand Down
4 changes: 0 additions & 4 deletions lib/candles/bittrex.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ function BittrexCandles (client) {
amount : 'Quantity'
};

this.nextStart = function (data) {
return moment(_.last(data).date).add(1, 's').unix();
};

this.acceptTrades = function (results, data) {
return results.concat(data.reverse());
};
Expand Down
13 changes: 8 additions & 5 deletions lib/candles/poloniex.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ var AbstractCandles = require('./abstract'),
_ = require('underscore'),
util = require('util');

function PoloniexCandles (client) {
function PoloniexCandles (client, params) {
var self = this;

AbstractCandles.apply(this, arguments);

var now = Math.floor(Date.now() / 1000);
this.start = params && params.candlesBuildTimeframe ? (now - params.candlesBuildTimeframe) : null;
this.end = now; // Current unix timestamp (in sec)

this.name = 'poloniex';
this.key = this.name + 'Candles';
this.url = 'https://poloniex.com/public?command=returnTradeHistory&currencyPair=BTC_LSK&start=';
this.start = 1464126157; // 2016-05-24 00:00:00 GMT
this.url = 'https://poloniex.com/public?command=returnTradeHistory&currencyPair=BTC_LSK';

this.response = {
error : 'error',
Expand All @@ -27,8 +30,8 @@ function PoloniexCandles (client) {
amount : 'amount'
};

this.nextStart = function (data) {
return moment(_.last(data).date).add(1, 's').unix();
this.nextEnd = function (data) {
return moment(_.first(data).date).subtract(1, 's').unix();
};

this.acceptTrades = function (results, data) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"license": "MIT",
"main": "app.js",
"scripts": {
"test": "./node_modules/.bin/mocha --reporter spec"
"test": "./node_modules/.bin/mocha --reporter spec --timeout 250s"
},
"dependencies": {
"async": "^2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion tasks/candles.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function (grunt) {

async.series([
function (callback) {
var poloniex = new candles.poloniex(client);
var poloniex = new candles.poloniex(client, config);

poloniex.buildCandles(function (err, res) {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion test/api/candles.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Candles API', function() {
'btcVolume',
'firstTrade',
'lastTrade',
'nextStart',
'nextEnd',
'numTrades'
);
}
Expand Down
1 change: 1 addition & 0 deletions test/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"cacheTTL" : 20,
"enableCandles" : true,
"updateCandlesInterval" : 30000,
"candlesBuildTimeframe" : 2592000,
"enableOrders" : true,
"updateOrdersInterval" : 15000
}
30 changes: 18 additions & 12 deletions test/config_lisk.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

{
"port": 7000,
"address": "0.0.0.0",
"version": "0.5.1a",
"minVersion": "~0.5.0",
"version": "0.8.2b",
"minVersion": ">=0.5.0",
"fileLogLevel": "info",
"logFileName": "logs/lisk.log",
"consoleLogLevel": "info",
Expand All @@ -23,8 +22,10 @@
]
},
"api": {
"enabled": true,
"access": {
"whiteList": []
"public": false,
"whiteList": ["127.0.0.1"]
},
"options": {
"limits": {
Expand All @@ -36,29 +37,32 @@
}
},
"peers": {
"enabled": true,
"list": [
{
"ip": "13.69.159.242",
"ip": "94.237.29.221",
"port": 7000
},
{
"ip": "40.68.34.176",
"ip": "83.136.254.104",
"port": 7000
},
{
"ip": "52.165.40.188",
"ip": "209.50.48.177",
"port": 7000
},
{
"ip": "13.82.31.30",
"ip": "94.237.64.33",
"port": 7000
},
{
"ip": "13.91.61.2",
"ip": "94.237.30.23",
"port": 7000
}
],
"blackList": [],
"access": {
"blackList": []
},
"options": {
"limits": {
"max": 0,
Expand All @@ -76,6 +80,9 @@
"releaseLimit": 25,
"relayLimit": 2
},
"transactions": {
"maxTxsPerQueue": 1000
},
"forging": {
"force": false,
"secret": [],
Expand All @@ -100,9 +107,8 @@
},
"dapp": {
"masterrequired": true,
"masterpassword": "Bdp2EtpCzLvj",
"masterpassword": "",
"autoexec": []
},
"nethash": "da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba"
}

0 comments on commit 56a9729

Please sign in to comment.