Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge #25

Merged
merged 19 commits into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions core/markets/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ var gekkoEnv = util.gekkoEnv();
var adapter = config[config.adapter];
var daterange = config.importer.daterange;

var from = moment(daterange.from);
var from = moment.utc(daterange.from);

if(daterange.to) {
var to = moment(daterange.to);
var to = moment.utc(daterange.to);
} else {
var to = moment();
var to = moment().utc();
log.debug(
'No end date specified for importing, setting to',
to.format()
Expand Down Expand Up @@ -74,7 +74,7 @@ var Market = function() {
this.candleManager.on(
'candles',
this.pushCandles
);
);

Readable.call(this, {objectMode: true});

Expand Down Expand Up @@ -114,4 +114,4 @@ Market.prototype.processTrades = function(trades) {
setTimeout(this.get, 1000);
}

module.exports = Market;
module.exports = Market;
15 changes: 5 additions & 10 deletions core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,18 @@ var util = {
+ `\nNodejs version: ${process.version}`;
},
die: function(m, soft) {
if(_gekkoEnv === 'standalone' || !_gekkoEnv)
var log = console.log.bind(console);
else if(_gekkoEnv === 'child-process')
var log = m => process.send({type: 'error', error: m});

var instanceName;
if(_gekkoEnv === 'child-process') {
return process.send({type: 'error', error: '\n ERROR: ' + m + '\n'});
}

if(util.gekkoEnv() === 'standalone')
instanceName = 'Gekko';
else
instanceName = 'This Gekko instance';
var log = console.log.bind(console);

if(m) {
if(soft) {
log('\n ERROR: ' + m + '\n\n');
} else {
log(`\n${instanceName} encountered an error and can\'t continue`);
log(`\nGekko encountered an error and can\'t continue`);
log('\nError:\n');
log(m, '\n\n');
log('\nMeta debug info:\n');
Expand Down
30 changes: 29 additions & 1 deletion exchange/exchangeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,36 @@ const isValidOrder = ({api, market, amount, price}) => {
}
}


// https://gist.github.com/jiggzson/b5f489af9ad931e3d186
const scientificToDecimal = num => {
if(/\d+\.?\d*e[\+\-]*\d+/i.test(num)) {
const zero = '0';
const parts = String(num).toLowerCase().split('e'); // split into coeff and exponent
const e = parts.pop(); // store the exponential part
const l = Math.abs(e); // get the number of zeros
const sign = e/l;
const coeff_array = parts[0].split('.');
if(sign === -1) {
num = zero + '.' + new Array(l).join(zero) + coeff_array.join('');
} else {
const dec = coeff_array[1];
if(dec) {
l = l - dec.length;
}
num = coeff_array.join('') + new Array(l+1).join(zero);
}
} else {
// make sure we always cast to string
num = num + '';
}

return num;
}

module.exports = {
retry: retryInstance,
bindAll,
isValidOrder
isValidOrder,
scientificToDecimal
}
1 change: 0 additions & 1 deletion exchange/orders/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class BaseOrder extends EventEmitter {
this.api = api;

this.checkInterval = api.interval || 1500;

this.status = states.INITIALIZING;

this.completed = false;
Expand Down
2 changes: 1 addition & 1 deletion exchange/util/genMarketFiles/update-kraken.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let getMinTradeSize = asset => {
minTradeSize = '30'
break;
case 'XXLM':
minTradeSize = '300'
minTradeSize = '30'
break;
case 'XZEC':
minTradeSize = '0.03'
Expand Down
63 changes: 27 additions & 36 deletions exchange/wrappers/binance.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const _ = require('lodash');

const Errors = require('../exchangeErrors');
const marketData = require('./binance-markets.json');
const retry = require('../exchangeUtils').retry;
const exchangeUtils = require('../exchangeUtils');
const retry = exchangeUtils.retry;
const scientificToDecimal = exchangeUtils.scientificToDecimal;

const Binance = require('binance');

Expand Down Expand Up @@ -109,18 +111,19 @@ Trader.prototype.handleResponse = function(funcName, callback) {
}

if(funcName === 'cancelOrder' && error.message.includes('UNKNOWN_ORDER')) {
console.log(new Date, 'cancelOrder', 'UNKNOWN_ORDER');
// order got filled in full before it could be
// cancelled, meaning it was NOT cancelled.
return callback(false, {filled: true});
}

if(funcName === 'checkOrder' && error.message.includes('Order does not exist.')) {
// order got filled in full before it could be
// cancelled, meaning it was NOT cancelled.
return callback(false, {filled: true});
console.log(new Date, 'Binance doesnt know this order, retrying up to 10 times..');
error.retry = 10;
}

if(funcName === 'addOrder' && error.message.includes('Account has insufficient balance')) {
console.log(new Date, 'insufficientFunds');
error.type = 'insufficientFunds';
}

Expand Down Expand Up @@ -264,37 +267,11 @@ Trader.prototype.round = function(amount, tickSize) {
amount /= precision;

// https://gist.github.com/jiggzson/b5f489af9ad931e3d186
amount = this.scientificToDecimal(amount);
amount = scientificToDecimal(amount);

return amount;
};

// https://gist.github.com/jiggzson/b5f489af9ad931e3d186
Trader.prototype.scientificToDecimal = function(num) {
if(/\d+\.?\d*e[\+\-]*\d+/i.test(num)) {
const zero = '0';
const parts = String(num).toLowerCase().split('e'); // split into coeff and exponent
const e = parts.pop(); // store the exponential part
const l = Math.abs(e); // get the number of zeros
const sign = e/l;
const coeff_array = parts[0].split('.');
if(sign === -1) {
num = zero + '.' + new Array(l).join(zero) + coeff_array.join('');
} else {
const dec = coeff_array[1];
if(dec) {
l = l - dec.length;
}
num = coeff_array.join('') + new Array(l+1).join(zero);
}
} else {
// make sure we always cast to string
num = num + '';
}

return num;
}

Trader.prototype.roundAmount = function(amount) {
return this.round(amount, this.market.minimalOrder.amount);
}
Expand All @@ -308,7 +285,6 @@ Trader.prototype.isValidPrice = function(price) {
}

Trader.prototype.isValidLot = function(price, amount) {
console.log('isValidLot', this.market.minimalOrder.order, amount * price >= this.market.minimalOrder.order)
return amount * price >= this.market.minimalOrder.order;
}

Expand Down Expand Up @@ -357,13 +333,30 @@ Trader.prototype.getOrder = function(order, callback) {

const fees = {};

if(!data.length) {
return callback(new Error('Binance did not return any trades'));
}

const trades = _.filter(data, t => {
// note: the API returns a string after creating
return t.orderId == order;
});

if(!trades.length) {
return callback(new Error('Trades not found'));
console.log('cannot find trades!', { order, list: data.map(t => t.orderId).reverse() });

const reqData = {
symbol: this.pair,
orderId: order,
};

this.binance.queryOrder(reqData, (err, resp) => {
console.log('couldnt find any trade for order, here is order:', {err, resp});

callback(new Error('Trades not found'));
});

return;
}

_.each(trades, trade => {
Expand Down Expand Up @@ -405,7 +398,7 @@ Trader.prototype.getOrder = function(order, callback) {
const reqData = {
symbol: this.pair,
// if this order was not part of the last 500 trades we won't find it..
limit: 500,
limit: 1000,
};

const handler = cb => this.binance.myTrades(reqData, this.handleResponse('getOrder', cb));
Expand Down Expand Up @@ -471,8 +464,6 @@ Trader.prototype.cancelOrder = function(order, callback) {
this.oldOrder = order;

if(err) {
if(err.message.contains(''))

return callback(err);
}

Expand Down
7 changes: 6 additions & 1 deletion exchange/wrappers/bitfinex.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var Trader = function(config) {
this.pair = this.asset + this.currency;
this.bitfinex = new Bitfinex.RESTv1({apiKey: this.key, apiSecret: this.secret, transform: true});

this.interval = 2000;
this.interval = 4000;
}

const includes = (str, list) => {
Expand All @@ -49,13 +49,16 @@ const recoverableErrors = [

Trader.prototype.handleResponse = function(funcName, callback) {
return (error, data) => {

if(!error && _.isEmpty(data)) {
error = new Error('Empty response');
}

if(error) {
const message = error.message;

console.log('handleResponse', funcName, message);

// in case we just cancelled our balances might not have
// settled yet, retry.
if(
Expand Down Expand Up @@ -227,6 +230,8 @@ Trader.prototype.getOrder = function(order_id, callback) {
var amount = parseFloat(data.executed_amount);
var date = moment.unix(data.timestamp);

console.log('getOrder', data);

// TEMP: Thu May 31 14:49:34 CEST 2018
// the `past_trades` call is not returning
// any data.
Expand Down
6 changes: 3 additions & 3 deletions exchange/wrappers/kraken-markets.json
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@
],
"book": "XXLMXXBT",
"minimalOrder": {
"amount": "300",
"amount": "30",
"unit": "asset"
},
"pricePrecision": 8,
Expand All @@ -770,7 +770,7 @@
],
"book": "XXLMZEUR",
"minimalOrder": {
"amount": "300",
"amount": "30",
"unit": "asset"
},
"pricePrecision": 6,
Expand All @@ -787,7 +787,7 @@
],
"book": "XXLMZUSD",
"minimalOrder": {
"amount": "300",
"amount": "30",
"unit": "asset"
},
"pricePrecision": 6,
Expand Down
10 changes: 6 additions & 4 deletions exchange/wrappers/kraken.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const Kraken = require('kraken-api');
const moment = require('moment');
const _ = require('lodash');
const retry = require('../exchangeUtils').retry;
const exchangeUtils = require('../exchangeUtils');
const retry = exchangeUtils.retry;
const scientificToDecimal = exchangeUtils.scientificToDecimal;

const marketData = require('./kraken-markets.json');

Expand Down Expand Up @@ -111,7 +113,7 @@ Trader.prototype.handleResponse = function(funcName, callback, nonMutating, payl
}

// string vs float
if(o.descr.price != price) {
if(+o.descr.price != price) {
return false;
}

Expand Down Expand Up @@ -273,11 +275,11 @@ Trader.prototype.roundAmount = function(amount) {
};

Trader.prototype.roundPrice = function(amount) {
return _.round(amount, this.market.pricePrecision);
return scientificToDecimal(_.round(amount, this.market.pricePrecision));
};

Trader.prototype.addOrder = function(tradeType, amount, price, callback) {
price = this.roundAmount(price); // only round price, not amount
price = this.roundPrice(price); // only round price, not amount

const handle = (err, data) => {
if(err) {
Expand Down
3 changes: 2 additions & 1 deletion exchange/wrappers/poloniex.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const recoverableErrors = [
'Connection timed out. Please try again.',
// getaddrinfo EAI_AGAIN poloniex.com poloniex.com:443
'EAI_AGAIN',
'ENETUNREACH'
'ENETUNREACH',
'socket hang up'
];

// errors that might mean
Expand Down
3 changes: 3 additions & 0 deletions plugins/backtestResultExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ BacktestResultExporter.prototype.processPerformanceReport = function(performance

BacktestResultExporter.prototype.finalize = function(done) {
const backtest = {
market: config.watch,
tradingAdvisor: config.tradingAdvisor,
strategyParameters: config[config.tradingAdvisor.method],
performanceReport: this.performanceReport
};

Expand Down
2 changes: 1 addition & 1 deletion plugins/paperTrader/paperTrader.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ PaperTrader.prototype.createTrigger = function(advice) {
this.deferredEmit('triggerCreated', {
id: triggerId,
at: advice.date,
type: 'trialingStop',
type: 'trailingStop',
proprties: {
trail: trigger.trailValue,
initialPrice: this.price,
Expand Down
Loading