ccxt / ccxt

A JavaScript / TypeScript / Python / C# / PHP cryptocurrency trading API with support for more than 100 bitcoin/altcoin exchanges
https://docs.ccxt.com
MIT License
32.05k stars 7.4k forks source link

Binance: Exchange Error {"code":-3028,"msg":"Not a valid margin pair."} #8695

Open strikeout opened 3 years ago

strikeout commented 3 years ago

Hi ccxt team!

I am getting constant {"code":-3028,"msg":"Not a valid margin pair."} errors from Binance when executing market orders on them even tho I am using the 'margin' defaultType on initialization .

// init binance in margin mode
const binance = new ccxt.binance({
  options: {
    'defaultType': 'margin',
  },
})

// fetch markets, with explicit refresh and explicit type
const allMarkets = await this.exchange.loadMarkets(true,  {type: 'margin'})

// then I filter the markets to only give me margin=true pairs
const marginMarkets =  markets.filter((m) => {
      return m.active && m.margin && m.type === 'margin' && m.info.isMarginTradingAllowed
})

Even tho the filtered marginMarkets now only contains pairs with margin: true I still get the above mentioned error message from the exchange on some pairs.

Here is one of the pairs which are in marginMarkets but should not be as they dont seem to be supported. Also they do not show up in the Binance GET /sapi/v1/margin/allPairs listing.

Notice how it has "isMarginTradingAllowed": true, "margin": true and "type": "margin" set to indicate margin trading is allowed and possible.

// binance.markets["ENJ/BTC"]:

{
  "limits": {
    "amount": {
      "min": 1,
      "max": 90000000
    },
    "price": {
      "min": 1e-8,
      "max": 1000
    },
    "cost": {
      "min": 0.0001
    },
    "market": {
      "min": 0,
      "max": 418478.30902778
    }
  },
  "precision": {
    "base": 8,
    "quote": 8,
    "amount": 0,
    "price": 8
  },
  "tierBased": false,
  "percentage": true,
  "taker": 0.001,
  "maker": 0.001,
  "id": "ENJBTC",
  "lowercaseId": "enjbtc",
  "symbol": "ENJ/BTC",
  "base": "ENJ",
  "quote": "BTC",
  "baseId": "ENJ",
  "quoteId": "BTC",
  "info": {
    "symbol": "ENJBTC",
    "status": "TRADING",
    "baseAsset": "ENJ",
    "baseAssetPrecision": 8,
    "quoteAsset": "BTC",
    "quotePrecision": 8,
    "quoteAssetPrecision": 8,
    "baseCommissionPrecision": 8,
    "quoteCommissionPrecision": 8,
    "orderTypes": ["LIMIT", "LIMIT_MAKER", "MARKET", "STOP_LOSS_LIMIT", "TAKE_PROFIT_LIMIT"],
    "icebergAllowed": true,
    "ocoAllowed": true,
    "quoteOrderQtyMarketAllowed": true,
    "isSpotTradingAllowed": true,
    "isMarginTradingAllowed": true,
    "filters": [{
      "filterType": "PRICE_FILTER",
      "minPrice": "0.00000001",
      "maxPrice": "1000.00000000",
      "tickSize": "0.00000001"
    }, {
      "filterType": "PERCENT_PRICE",
      "multiplierUp": "5",
      "multiplierDown": "0.2",
      "avgPriceMins": 5
    }, {
      "filterType": "LOT_SIZE",
      "minQty": "1.00000000",
      "maxQty": "90000000.00000000",
      "stepSize": "1.00000000"
    }, {
      "filterType": "MIN_NOTIONAL",
      "minNotional": "0.00010000",
      "applyToMarket": true,
      "avgPriceMins": 5
    }, {
      "filterType": "ICEBERG_PARTS",
      "limit": 10
    }, {
      "filterType": "MARKET_LOT_SIZE",
      "minQty": "0.00000000",
      "maxQty": "418478.30902778",
      "stepSize": "0.00000000"
    }, {
      "filterType": "MAX_NUM_ORDERS",
      "maxNumOrders": 200
    }, {
      "filterType": "MAX_NUM_ALGO_ORDERS",
      "maxNumAlgoOrders": 5
    }],
    "permissions": ["SPOT", "MARGIN"]
  },
  "type": "margin",
  "spot": false,
  "margin": true,
  "future": false,
  "delivery": false,
  "active": true
}

Executing a type MARKET trade on this pair results in the error mentioned above. The code works perfectly in all spot markets, so price and amount rounding issues are not the issue.

binance.createOrder(
          order.symbol, //  "ENJ/BTC"
          order.type, // "MARKET"
          order.side, // "buy"
          order.amount, // 5
          undefined, 
          {
             sideEffectType: 'MARGIN_BUY' // enable auto-borrow
          } 
        )

Does this problem arise from binance API delivering wrong results, by CCXT providing wrong data for market pairs or from my approach in getting margin-only markets? (I know, I could use the implicit method for sapi/v1/margin/allPairs but I'd rather use the unified call - which should yield the same data)

kroitor commented 3 years ago

@strikeout thx for your report, we will investigate and will be back to you asap.

strikeout commented 3 years ago

Small update:

I've checked the binance Website and API directly and it seems that, at least the above mentioned pair ENJ/BTC, does in fact allow margin trading, but only on isolated margin. I have not found any indication or differentiator via the API market data endpoint about this tho.

kroitor commented 3 years ago

@strikeout thanks for pointing that out, it really helps save the time. Apparently, we can only throw an exception from the exchange at this point...

strikeout commented 3 years ago

Can we maybe introduce a new property (array) or two (bools), like I did, to the market to indicate which "type" of margin is supported?

I'm now doing the following locally:

// fetch isolated margin pairs, sets .marginIso = true 
        let isos = await this.exchange.sapiGetMarginIsolatedAllPairs();
        isos = isos.forEach((m) => {
          const mm = this.exchange.markets_by_id[m.symbol];
          this.exchange.markets[mm.symbol].marginIso = true
        });
// fetch cross margin pairs, sets .marginCross = true 
        let cross = await this.exchange.sapiGetMarginAllPairs();
        cross = cross.forEach((m) => {
          const mm = this.exchange.markets_by_id[m.symbol];
          this.exchange.markets[mm.symbol].marginCross = true
        });
kroitor commented 3 years ago

@strikeout yes, that is possible, let me think a bit on how we can better unify this across the exchanges, i will be back to you shortly.

Piercarlomaia commented 3 years ago

I am facing the same issue, can't distinguish between altcoins with leverage

kroitor commented 3 years ago

@Piercarlomaia have you set your defaultType to 'margin'? Can you paste your versions, your code and your request+response output in verbose mode? https://github.com/ccxt/ccxt/wiki/FAQ#what-is-required-to-get-help

stormx5 commented 3 years ago

it's probably that the pair does not support cross margin, and is only tradable in isolated margin. Sadly been looking at ccxt and binanace API's to find out if the pair is cross margin or isolated with no luck.

ttodua commented 11 months ago

there are official api endpoints for them, i.e.: A) Get all marginable assets - https://binance-docs.github.io/apidocs/spot/en/#get-all-margin-assets-market_data , using ccxt you can call: data = binance.sapiGetMarginAllAssets(); B) Get all cross-margin pairs: https://binance-docs.github.io/apidocs/spot/en/#get-all-cross-margin-pairs-market_data , using ccxt you can call: data = binance.sapiGetMarginAllPairs(); C) Get all isoslated-margin pairs: https://binance-docs.github.io/apidocs/spot/en/#get-all-isolated-margin-symbol-user_data , using ccxt you can call: data = binance.sapiGetMarginIsolatedAllPairs();

let us know if you have any questions