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

Minor fixes for Quadriga trading API #421

Merged
merged 22 commits into from
Jul 31, 2017
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
62d1d85
Merge remote-tracking branch 'carlos8f/master'
cmroche Jul 10, 2017
ad3d514
Merge remote-tracking branch 'carlos8f/master'
cmroche Jul 10, 2017
3cad6f3
Merge branch 'master' into carlos8f/master
cmroche Jul 15, 2017
b1fabd3
Merge remote-tracking branch 'carlos8f/master'
cmroche Jul 15, 2017
fc5a151
Quadriga CX Fixes
cmroche Jul 15, 2017
57c4ecf
Sometime opts.from is undefined, and we should return all results.
cmroche Jul 16, 2017
977aa14
Formatting
cmroche Jul 17, 2017
f3c084c
Merge remote-tracking branch 'carlos8f/master'
cmroche Jul 18, 2017
3412d99
Merge branch 'master' into qcx_fixes
cmroche Jul 19, 2017
d445811
Quadriga now supports trading LTC
cmroche Jul 19, 2017
f72cf73
Fixing QCX API handling of numbers
cmroche Jul 20, 2017
1d452e9
QCX fix incorrect error reporting
cmroche Jul 20, 2017
a5d336e
Merge remote-tracking branch 'carlos8f/master'
cmroche Jul 21, 2017
1594cc5
* Adding debug output to help troubleshoot any more issues
cmroche Jul 21, 2017
0cc21d8
Fix error with retry in getBalance
cmroche Jul 22, 2017
39e4b58
QCX - Fix look up order
cmroche Jul 27, 2017
0af5e38
Merge remote-tracking branch 'carlos8f/master'
cmroche Jul 27, 2017
365f08b
Merge branch 'master' into qcx_fixes
cmroche Jul 27, 2017
875aed0
Merge remote-tracking branch 'carlos8f/master'
cmroche Jul 29, 2017
7e59313
Merge branch 'master' into qcx_fixes
cmroche Jul 29, 2017
64ad897
Re-enabled taker order processing in buy/sell command
cmroche Jul 29, 2017
cf3919c
Merge remote-tracking branch 'origin/qcx_fixes' into qcx_fixes
cmroche Jul 29, 2017
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
82 changes: 51 additions & 31 deletions extensions/exchanges/quadriga/exchange.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
var QuadrigaCX = require('quadrigacx'),
path = require('path'),
minimist = require('minimist'),
moment = require('moment'),
colors = require('colors'),
n = require('numbro')

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

var shownWarnings = false

var public_client, authed_client

function publicClient() {
if (!public_client) public_client = new QuadrigaCX("1", "", "");
if (!public_client) public_client = new QuadrigaCX("1", "", "")
return public_client
}

Expand All @@ -21,7 +27,7 @@ module.exports = function container(get, set, clear) {
throw new Error('please configure your Quadriga credentials in ' + path.resolve(__dirname, 'conf.js'))
}

authed_client = new QuadrigaCX(c.quadriga.client_id, c.quadriga.key, c.quadriga.secret);
authed_client = new QuadrigaCX(c.quadriga.client_id, c.quadriga.key, c.quadriga.secret)
}
return authed_client
}
Expand All @@ -33,7 +39,7 @@ module.exports = function container(get, set, clear) {
function retry(method, args, error) {
if (error.code === 200) {
console.error(('\QuadrigaCX API rate limit exceeded! unable to call ' + method + ', aborting').red)
return;
return
}

if (method !== 'getTrades') {
Expand All @@ -44,6 +50,10 @@ module.exports = function container(get, set, clear) {
}, 30000)
}

function debugOut(msg) {
if (so.debug) console.log(msg)
}

var orders = {}

var exchange = {
Expand All @@ -67,12 +77,12 @@ module.exports = function container(get, set, clear) {
client.api('transactions', args, function(err, body) {
if (!shownWarnings) {
console.log('please note: the quadriga api does not support backfilling.')
console.log('please note: periods should be set to 1h or less.');
shownWarnings = true;
console.log('please note: periods should be set to 1h or less.')
shownWarnings = true
}

if (err) return retry('getTrades', func_args, err)
if (body.error) return retry('getTrades', func_args, trades.error)
if (body.error) return retry('getTrades', func_args, body.error)

var trades = body.filter(t => {
return (typeof opts.from === 'undefined') ? true : (moment.unix(t.date).valueOf() > opts.from)
Expand All @@ -91,10 +101,11 @@ module.exports = function container(get, set, clear) {
},

getBalance: function(opts, cb) {
var func_args = [].slice.call(arguments)
var client = authedClient()
client.api('balance', function(err, wallet) {
if (err) return retry('getBalance', null, err)
if (wallet.error) return retry('getBalance', null, wallet.error)
if (err) return retry('getBalance', func_args, err)
if (wallet.error) return retry('getBalance', func_args, wallet.error)

var currency = opts.currency.toLowerCase()
var asset = opts.asset.toLowerCase()
Expand All @@ -104,11 +115,16 @@ module.exports = function container(get, set, clear) {
currency: 0
}

balance.currency = Number(wallet[currency + '_balance']);
balance.asset = Number(wallet[asset + '_balance']);
balance.currency = n(wallet[currency + '_balance']).format(0.00)
balance.asset = n(wallet[asset + '_balance']).format(0.00)

balance.currency_hold = n(wallet[currency + '_reserved']).format(0.00000000)
balance.asset_hold = n(wallet[asset + '_reserved']).format(0.00000000)

debugOut(`Balance/Reserve/Hold:`)
debugOut(` ${currency} (${wallet[currency + '_balance']}/${wallet[currency + '_reserved']}/${wallet[currency + '_available']})`)
debugOut(` ${asset} (${wallet[asset + '_balance']}/${wallet[asset + '_reserved']}/${wallet[asset + '_available']})`)

balance.currency_hold = Number(wallet[currency + '_reserved'])
balance.asset_hold = Number(wallet[asset + '_reserved'])
cb(null, balance)
})
},
Expand All @@ -126,8 +142,8 @@ module.exports = function container(get, set, clear) {
if (quote.error) return retry('getQuote', func_args, quote.error)

var r = {
bid: Number(quote.bid),
ask: Number(quote.ask)
bid: String(quote.bid),
ask: String(quote.ask)
}

cb(null, r)
Expand All @@ -140,6 +156,8 @@ module.exports = function container(get, set, clear) {
id: opts.order_id
}

debugOut(`Cancelling order ${opts.order_id}`)

var client = authedClient()
client.api('cancel_order', params, function(err, body) {
if (err) return retry('cancelOrder', func_args, err)
Expand All @@ -158,6 +176,8 @@ module.exports = function container(get, set, clear) {
params.price = n(opts.price).format('0.00')
}

debugOut(`Requesting ${opts.order_type} buy for ${opts.size} assets`)

var client = authedClient()
client.api('buy', params, function(err, body) {
var order = {
Expand All @@ -171,14 +191,11 @@ module.exports = function container(get, set, clear) {
}

if (err) return cb(err)
if (body.error) {
//console.log(`API Error: ${body.error.message}`);
return cb(body.error)
}
if (body.error) return cb(body.error)

if (opts.order_type === 'taker') {
order.status = 'done'
order.done_at = new Date().getTime();
order.done_at = new Date().getTime()

if (body.orders_matched) {
var asset_total = 0
Expand All @@ -197,6 +214,8 @@ module.exports = function container(get, set, clear) {
}
}

debugOut(` Purchase ID: ${body.id}`)

order.id = body.id
orders['~' + body.id] = order
cb(null, order)
Expand All @@ -213,6 +232,8 @@ module.exports = function container(get, set, clear) {
params.price = n(opts.price).format('0.00')
}

debugOut(`Requesting ${opts.order_type} sell for ${opts.size} assets`)

var client = authedClient()
client.api('sell', params, function(err, body) {
var order = {
Expand All @@ -226,22 +247,19 @@ module.exports = function container(get, set, clear) {
}

if (err) return cb(err)
if (body.error) {
//console.log(`API Error: ${body.error.message}`);
return cb(body.error)
}
if (body.error) return cb(body.error)

if (opts.order_type === 'taker') {
order.status = 'done'
order.done_at = new Date().getTime();
order.done_at = new Date().getTime()

if (body.orders_matched) {
var asset_total = 0
var price_total = 0.0
var order_count = body.orders_matched.length
for (var idx = 0; idx < order_count; idx++) {
asset_total = asset_total + Number(body.orders_matched[idx].amount)
price_total = price_total + (Number(body.orders_matched[idx].amount) * body.orders_matched[idx].price)
price_total = price_total + (Number(body.orders_matched[idx].amount) * Number(body.orders_matched[idx].price))
}

order.price = price_total / asset_total
Expand All @@ -252,6 +270,8 @@ module.exports = function container(get, set, clear) {
}
}

debugOut(` Sell ID: ${body.id}`)

order.id = body.id
orders['~' + body.id] = order
cb(null, order)
Expand All @@ -267,17 +287,17 @@ module.exports = function container(get, set, clear) {
var client = authedClient()
client.api('lookup_order', params, function(err, body) {
if (err) return cb(err)
if (body.error) {
//console.log(`API Error: ${body.error.message}`);
return cb(body.error)
}
if (body.error) return cb(body.error)

if (body.status === 2) {
if (body[0].status === 2) {
order.status = 'done'
order.done_at = new Date().getTime()
order.filled_size = Number(body.amount)
order.filled_size = Number(body[0].amount)
return cb(null, order)
}

debugOut(`Lookup order ${opts.order_id} status is ${body.status}`)

cb(null, order)
})
},
Expand Down