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

Commit

Permalink
Multiple Kraken API fixes/improvements (#367)
Browse files Browse the repository at this point in the history
* Kraken API:
- Fix cancelOrders (#303)
- Fix rejected trades on post only errors
- Improve detection of canceled trades
- Maybe fix sell orders executed multiple times (#339)
- Added more verbose debug logging
- Adjust timeouts for retry

* Replaced a tab with spaces
  • Loading branch information
crubb authored and DeviaVir committed Jul 10, 2017
1 parent c0c79bc commit 2c9add7
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions extensions/exchanges/kraken/exchange.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
var KrakenClient = require('kraken-api'),
path = require('path'),
minimist = require('minimist'),
moment = require('moment'),
n = require('numbro'),
colors = require('colors')

module.exports = function container(get, set, clear) {
var c = get('conf')
var s = {options: minimist(process.argv)}
var so = s.options

var public_client, authed_client
var recoverableErrors = new RegExp(/(ESOCKETTIMEDOUT|ETIMEDOUT|ECONNRESET|ECONNREFUSED|ENOTFOUND|API:Invalid nonce|API:Rate limit exceeded)/)
// var recoverableErrors = new RegExp(/(ESOCKETTIMEDOUT|ETIMEDOUT|ECONNRESET|ECONNREFUSED|ENOTFOUND|API:Invalid nonce|API:Rate limit exceeded|between Cloudflare and the origin web server)/)
var recoverableErrors = new RegExp(/(ESOCKETTIMEDOUT|ETIMEDOUT|ECONNRESET|ECONNREFUSED|ENOTFOUND|API:Invalid nonce|between Cloudflare and the origin web server)/)
var silencedRecoverableErrors = new RegExp(/(ESOCKETTIMEDOUT|ETIMEDOUT)/)

function publicClient() {
Expand Down Expand Up @@ -36,12 +40,17 @@ module.exports = function container(get, set, clear) {
if (error.message.match(/API:Rate limit exceeded/)) {
var timeout = 10000
} else {
var timeout = 2500
var timeout = 150
}

// silence common timeout errors
if (!error.message.match(recoverableErrors)) {
console.warn(('\nKraken API warning - unable to call ' + method + ' (' + error + '), retrying in ' + timeout / 1000 + 's').yellow)
if (so.debug || !error.message.match(silencedRecoverableErrors)) {
if (error.message.match(/between Cloudflare and the origin web server/)) {
errorMsg = 'Connection between Cloudflare CDN and api.kraken.com failed'
} else {
errorMsg = error
}
console.warn(('\nKraken API warning - unable to call ' + method + ' (' + errorMsg + '), retrying in ' + timeout / 1000 + 's').yellow)
}
setTimeout(function () {
exchange[method].apply(exchange, args)
Expand Down Expand Up @@ -123,12 +132,12 @@ module.exports = function container(get, set, clear) {
return cb(data.error.join(','))
}
if (data.result[opts.currency]) {
balance.currency = n(data.result[opts.currency]).format('0.00000000'),
balance.currency_hold = 0
balance.currency = n(data.result[opts.currency]).format('0.00000000')
balance.currency_hold = 0
}
if (data.result[opts.asset]) {
balance.asset = n(data.result[opts.asset]).format('0.00000000'),
balance.asset_hold = 0
balance.asset = n(data.result[opts.asset]).format('0.00000000')
balance.asset_hold = 0
}
cb(null, balance)
})
Expand Down Expand Up @@ -176,7 +185,11 @@ module.exports = function container(get, set, clear) {
if (data.error.length) {
return cb(data.error.join(','))
}
cb(null)
if (so.debug) {
console.log("cancelOrder")
console.log(data)
}
cb(error)
})
},

Expand All @@ -196,6 +209,10 @@ module.exports = function container(get, set, clear) {
if ('price' in opts) {
params.price = opts.price
}
if (so.debug) {
console.log("trade")
console.log(params)
}
client.api('AddOrder', params, function (error, data) {
if (error && error.message.match(recoverableErrors)) {
return retry('trade', args, error)
Expand All @@ -214,6 +231,15 @@ module.exports = function container(get, set, clear) {
order.post_only = !!opts.post_only
}

if (so.debug) {
console.log("Data")
console.log(data)
console.log("Order")
console.log(order)
console.log("Error")
console.log(error)
}

if (error) {
if (error.message.match(/Order:Insufficient funds$/)) {
order.status = 'rejected'
Expand Down Expand Up @@ -267,22 +293,27 @@ module.exports = function container(get, set, clear) {
return cb(data.error.join(','))
}
var orderData = data.result[params.txid]
if (so.debug) {
console.log("QueryOrders")
console.log(orderData)
}

if (!orderData) {
return cb('Order not found')
}

if (orderData.status === 'canceled' && orderData.reason === 'Post only order') {
order.status = 'rejected'
order.status = 'done'
order.reject_reason = 'post only'
order.done_at = new Date().getTime()
order.filled_size = n(orderData.vol_exec).format('0.00000000')
return cb(null, order)
}

if (orderData.status === 'closed') {
if (orderData.status === 'closed' || (orderData.status === 'canceled' && orderData.reason === 'User canceled')) {
order.status = 'done'
order.done_at = new Date().getTime()
order.filled_size = parseFloat(orderData.vol) - parseFloat(orderData.vol_exec)
order.filled_size = n(orderData.vol_exec).format('0.00000000')
return cb(null, order)
}

Expand Down

0 comments on commit 2c9add7

Please sign in to comment.