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

Update/binance fee tiers #2344

Merged
merged 2 commits into from
Jul 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
47 changes: 37 additions & 10 deletions exchange/wrappers/binance.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ const Trader = function(config) {
return market.pair[0] === this.currency && market.pair[1] === this.asset
});

// Note non standard func:
//
// On binance we might pay fees in BNB
// if we do we CANNOT calculate feePercent
// since we don't track BNB price (when we
// are not trading on a BNB market).
//
// Though we can deduce feePercent based
// on user fee tracked through `this.getFee`.
// Set default here, overwrite in getFee.
this.fee = 0.1 / 100;

this.binance = new Binance.BinanceRest({
key: this.key,
secret: this.secret,
Expand Down Expand Up @@ -160,10 +172,25 @@ Trader.prototype.getPortfolio = function(callback) {
retry(undefined, fetch, setBalance);
};

// This uses the base maker fee (0.1%), and does not account for BNB discounts
Trader.prototype.getFee = function(callback) {
const makerFee = 0.1;
callback(undefined, makerFee / 100);

// binance does NOT tell us whether the user is using BNB to pay
// for fees, which means a discount (effectively lower fees)
const handle = (err, data) => {
if(err) {
return callback(err);
}

const basepoints = data.makerCommission;

// note non standard func, see constructor
this.fee = basepoints / 100;

callback(undefined, basepoints / 100);
}

const fetch = cb => this.binance.account({}, this.handleResponse('getFee', cb));
retry(undefined, fetch, handle);
};

Trader.prototype.getTicker = function(callback) {
Expand Down Expand Up @@ -323,23 +350,23 @@ Trader.prototype.getOrder = function(order, callback) {
let feePercent;
if(_.keys(fees).length === 1) {
if(fees.BNB && this.asset !== 'BNB' && this.currency !== 'BNB') {
// we paid fees in BNB, right now that means the fee is always 5 basepoints.
// we cannot calculate since we do not have the BNB rate.
feePercent = 0.05;
// we paid fees in BNB, right now that means the fee is always 75%
// of base fee. We cannot calculate since we do not have the BNB rate.
feePercent = this.fee * 0.75;
} else {
if(fees[this.asset]) {
feePercent = fees[this.asset] / amount * 100;
} else if(fees.currency) {
feePercent = fees[this.currency] / price / amount * 100;
} else {
// assume base fee of 10 basepoints
feePercent = 0.1;
// use user fee of 10 basepoints
feePercent = this.fee;
}
}
} else {
// we paid fees in multiple currencies?
// assume base fee of 10 basepoints
feePercent = 0.1;
// assume user fee
feePercent = this.fee;
}

callback(undefined, { price, amount, date, fees, feePercent });
Expand Down
14 changes: 12 additions & 2 deletions plugins/trader/trader.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Trader = function(next) {
}

this.sync(() => {
this.setBalance();
log.info('\t', 'Portfolio:');
log.info('\t\t', this.portfolio.currency, this.brokerConfig.currency);
log.info('\t\t', this.portfolio.asset, this.brokerConfig.asset);
Expand Down Expand Up @@ -130,7 +131,16 @@ Trader.prototype.processCandle = function(candle, done) {
}

Trader.prototype.processAdvice = function(advice) {
const direction = advice.recommendation === 'long' ? 'buy' : 'sell';
let direction;

if(advice.recommendation === 'long') {
direction = 'buy';
} else if(advice.recommendation === 'short') {
direction = 'sell';
} else {
log.error('ignoring advice in unknown direction');
return;
}

const id = 'trade-' + (++this.propogatedTrades);

Expand Down Expand Up @@ -213,7 +223,7 @@ Trader.prototype.createOrder = function(side, amount, advice, id) {
return this.deferredEmit('tradeAborted', {
id,
adviceId: advice.id,
action: direction,
action: side,
portfolio: this.portfolio,
balance: this.balance,
reason: check.reason
Expand Down