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

Commit

Permalink
Fix binance product update (#1795)
Browse files Browse the repository at this point in the history
* Engine: Split code about check of minimal order size into a function

The function avoid code duplication and is more easily readable.

Small behavior change: the new function accept products with both `min_size` and `min_total`.
Previously, only the first one available was checked.

* Binance: rewrite update-products to fix bugs

Changes with the previous version:
- Binance filters are selected by type rather than by index. It fixes #1794
- No more useless removal of trailing zeros of 'increment' and 'asset_increment'
- Binance notional filter is set in `product.min_total` rather than roughly modifying `product.min_size`.
  It fixes #1793
  • Loading branch information
BAKFR authored and DeviaVir committed Dec 11, 2018
1 parent 9959059 commit d04c103
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
46 changes: 13 additions & 33 deletions extensions/exchanges/binance/update-products.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,24 @@ let ccxt = require('ccxt')
new ccxt.binance().fetch_markets().then(function(markets) {
var products = []

markets.forEach(function (market) {
var currStepSize = market.info.filters[0].tickSize
for (i = currStepSize.length - 1; i > 0; i--) {
if (currStepSize[i] === '0')
currStepSize = currStepSize.slice(0, i)
else
break;
}

var assetStepSize = market.info.filters[1].stepSize
for (i = assetStepSize.length - 1; i > 0; i--) {
if (assetStepSize[i] === '0')
assetStepSize = assetStepSize.slice(0, i)
else
break
}
var products = markets.map(function (market) {
const filters = market.info.filters
const price_filter = filters.find(f => f.filterType === 'PRICE_FILTER')
const lot_size_filter = filters.find(f => f.filterType === 'LOT_SIZE')
const notional_filter = filters.find(f => f.filterType === 'MIN_NOTIONAL')

// The MIN_NOTIONAL filter defines the minimum notional value allowed for an order on a symbol.
// An orders notional value is the price * quantity.
// In order to know the Value it is necessary to know the price which does not come on this JSon.
// But I have the maxPrice which seems to be: price * 10
var maxPrice = Number(market.info.filters[0].maxPrice);
var curPrice = maxPrice / 10;
var minNotional = Number(market.info.filters[2].minNotional);
var minNotionalQty = minNotional / curPrice;
var minQty = Number(market.info.filters[1].minQty);
var min_size = Math.max(minQty, minNotionalQty).toString();

products.push({
// NOTE: price_filter also contains minPrice and maxPrice
return {
id: market.id,
asset: market.base,
currency: market.quote,
min_size: min_size,
max_size: market.info.filters[1].maxQty,
increment: currStepSize,
asset_increment: assetStepSize,
min_size: lot_size_filter.minQty,
max_size: lot_size_filter.maxQty,
min_total: notional_filter.minNotional,
increment: price_filter.tickSize,
asset_increment: lot_size_filter.stepSize,
label: market.base + '/' + market.quote
})
}
})

var target = require('path').resolve(__dirname, 'products.json')
Expand Down
12 changes: 10 additions & 2 deletions lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ module.exports = function (s, conf) {
})
}

function isOrderTooSmall(product, quantity, price) {
if (product.min_size && Number(quantity) < Number(product.min_size))
return true
if (product.min_total && n(quantity).multiply(price).value() < Number(product.min_total))
return true
return false
}

// if s.signal
// 1. sync balance
// 2. get quote
Expand Down Expand Up @@ -395,7 +403,7 @@ module.exports = function (s, conf) {
size = n(trade_balance).subtract(expected_fee).divide(price).format(s.product.asset_increment ? s.product.asset_increment : '0.00000000')
}

if ((s.product.min_size && Number(size) >= Number(s.product.min_size)) || ('min_total' in s.product && s.product.min_total && n(size).multiply(price).value() >= Number(s.product.min_total))) {
if (!isOrderTooSmall(s.product, size, price)) {
if (s.product.max_size && Number(size) > Number(s.product.max_size)) {
size = s.product.max_size
}
Expand Down Expand Up @@ -446,7 +454,7 @@ module.exports = function (s, conf) {
}
size = n(s.balance.asset).multiply(sell_pct / 100).format(s.product.asset_increment ? s.product.asset_increment : '0.00000000')

if ((s.product.min_size && Number(size) >= Number(s.product.min_size)) || (s.product.min_total && n(size).multiply(price).value() >= Number(s.product.min_total))) {
if (!isOrderTooSmall(s.product, size, price)) {
if (s.product.max_size && Number(size) > Number(s.product.max_size)) {
size = s.product.max_size
}
Expand Down

0 comments on commit d04c103

Please sign in to comment.