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

Commit

Permalink
CEXIO use "my" fees (#967)
Browse files Browse the repository at this point in the history
* CEXIO use "my" fees

Get (and use) personalized fees on CEXIO as returned by the API at https://cex.io/api/get_myfee for live and paper trading.

The fees get recalculated (by CEX) and refreshed (by us) every day at midnight (GMT)

The timer uses UTC so this works regardless of the bots local timezone.

* Check for dynamicFees property instead of exchange name

Less hackish
  • Loading branch information
defkev authored and DeviaVir committed Jan 3, 2018
1 parent 0c7c2c2 commit 561eb54
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
44 changes: 44 additions & 0 deletions extensions/exchanges/cexio/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,31 @@ module.exports = function container (get, set, clear) {
}, 10000)
}

function refreshFees(args) {
var skew = 5000 // in ms
var now = new Date();
var nowUTC = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds())
var midnightUTC = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()).setHours(24,0,0,0)
var countdown = midnightUTC - nowUTC + skew
if (so.debug) {
var hours = parseInt((countdown/(1000*60*60))%24)
var minutes = parseInt((countdown/(1000*60))%60)
var seconds = parseInt((countdown/1000)%60)
console.log('\nRefreshing fees in ' + hours + ' hours ' + minutes + ' minutes ' + seconds + ' seconds')
}
setTimeout(function() {
exchange['setFees'].apply(exchange, args)
}, countdown)
}

var orders = {}
var exchange = {
name: 'cexio',
historyScan: 'forward',
backfillRateLimit: 0,
makerFee: 0.16,
takerFee: 0.25,
dynamicFees: true,

getProducts: function () {
return require('./products.json')
Expand Down Expand Up @@ -182,6 +200,32 @@ module.exports = function container (get, set, clear) {
})
},

setFees: function(opts) {
var func_args = [].slice.call(arguments)
var client = authedClient()
client.get_my_fee(function (err, body) {
if (err || (typeof body === 'string' && body.match(/error/))) {
if (so.debug) {
console.log(('\nsetFees ' + body + ' - using fixed fees!').red)
}
return retry('setFees', func_args)
} else {
var pair = opts.asset + ':' + opts.currency
var makerFee = (parseFloat(body[pair].buyMaker) + parseFloat(body[pair].sellMaker)) / 2
var takerFee = (parseFloat(body[pair].buy) + parseFloat(body[pair].sell)) / 2
if (exchange.makerFee != makerFee) {
if (so.debug) console.log('\nMaker fee changed: ' + exchange.makerFee + '% -> ' + makerFee + '%')
exchange.makerFee = makerFee
}
if (exchange.takerFee != takerFee) {
if (so.debug) console.log('\nTaker fee changed: ' + exchange.takerFee + '% -> ' + takerFee + '%')
exchange.takerFee = takerFee
}
}
return refreshFees(func_args)
})
},

// return the property used for range querying.
getCursor: function (trade) {
return trade.trade_id
Expand Down
3 changes: 3 additions & 0 deletions lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ module.exports = function container (get, set, clear) {
console.error('error: could not find product "' + s.product_id + '"')
process.exit(1)
}
if ((so.mode === 'live' || so.mode === 'paper') && s.exchange.dynamicFees) {
s.exchange.setFees({asset: s.asset, currency: s.currency})
}
if (so.mode === 'sim' || so.mode === 'paper') {
s.balance = {asset: so.asset_capital, currency: so.currency_capital}
}
Expand Down

0 comments on commit 561eb54

Please sign in to comment.