jaggedsoft / node-binance-api

Node Binance API is an asynchronous node.js library for the Binance API designed to be easy to use.
MIT License
1.58k stars 768 forks source link

Precision is over the maximum defined for this asset. #577

Open Naografix opened 3 years ago

Naografix commented 3 years ago

Hello

I'm tring in the Binance Future's testnet and I got an error precision every time... I don't understand how to fix it.

    let price = await GetLastPrice();    
    let stepSize = global.filters[pair].stepSize; //pair = BTCUSDT
    let precision = stepSize.toString().split(".")[1].length || 0;
    let amount = (positionSizeInUsdt / price); //positionSizeInUsdt = 10000
    amount = binance.roundStep(amount, stepSize);
    console.log("Buy MARKET " + pair + " quantity: " + amount.toFixed(precision)+ " - stepSize: " + stepSize + " - precision: " + precision + " at: " + price);
    let buyOrder = await binance.futuresMarketBuy(pair, amount.toFixed(precision), {newOrderRespType: 'RESULT'});
    console.log(buyOrder);

And I got this error:

Buy MARKET BTCUSDT quantity: 0.268118 - stepSize: 0.00000100 - precision: 8 at: 37296.94000000 [Object: null prototype] { code: -1111, msg: 'Precision is over the maximum defined for this asset.' }

What is wrong with my code?

Thanks!

aquilatrindade commented 3 years ago

I fixed the stepSize to BTCUSDT with precision 6 and not 8. I'd the same problem.

37296.94000000 <= here you have 8

gavcat commented 3 years ago

after precision convert to string

and if a precision = 6 or 8 and price 1.23450000 you must erace last zero to 1.2345

you look at minPrice in PRICE_FILTER?

hronnie commented 3 years ago

I also struggled with this problem and I figured out that for futures the precision is smaller than for spot trading. E.g. for BTCUSDT it's 3 but you can check in the application for the specific pair (just try to buy in futures and below the scroll it will show the amount where you can figure out the precision).

hungbang commented 2 years ago

I also struggled with this problem and I figured out that for futures the precision is smaller than for spot trading. E.g. for BTCUSDT it's 3 but you can check in the application for the specific pair (just try to buy in futures and below the scroll it will show the amount where you can figure out the precision).

you've saved my time. is there any API from Binance to check that?

hungbang commented 2 years ago

Oh I found that, we can use get exchange info API to see quantityPrecision

vdtuan commented 2 years ago

Hi I am facing a very strange bug today, can anyone help me? I am calling this function futuresMarketSell to maker a sell order for BTCUSDT, qty: 0.00091 but I got this error: Precision is over the maximum defined for this asset not sure what is wrong here!

jamestangeres commented 1 year ago

futuresMarketBuy

Did you solve your issues? I need your help. Got this issue:

Object: null prototype] {
  code: -1111,
  msg: 'Precision is over the maximum defined for this asset.'
}

using this line of code:

quantity = parseFloat(20 / 0.02396).toFixed(5);

binance.futuresMarketBuy('GALAUSDT', quantity)
  .then(resp => console.info(resp))
  .catch(error => console.error(error));

Thank you for the help.

hronnie commented 1 year ago

Hey,

Yes I did. The trick is that you have to use the price precision from the symbol information. And please make sure that you are using the price precision for the price and quantity precision for quantity. Here is my method, maybe it helps (I'm getting the precision numbers from a mapping but you can get it from the exchange info but I didn't want to always get it since it's quite huge):

/**

On Thu, 6 Jul 2023 at 18:38, jamestangeres @.***> wrote:

futuresMarketBuy

Did you solve your issues? I need your help. Got this issue:

Object: null prototype] { code: -1111, msg: 'Precision is over the maximum defined for this asset.' }

using this line of code:

quantity = parseFloat(20 / 0.02396).toFixed(5);

binance.futuresMarketBuy('GALAUSDT', quantity) .then(resp => console.info(resp)) .catch(error => console.error(error));

Thank you for the help.

— Reply to this email directly, view it on GitHub https://github.com/jaggedsoft/node-binance-api/issues/577#issuecomment-1623984357, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7FQDVN4R5HHBWCCGC3EC3XO3SZ5ANCNFSM4XFJUVGQ . You are receiving this because you commented.Message ID: @.***>

jamestangeres commented 1 year ago

Hey, Yes I did. The trick is that you have to use the price precision from the symbol information. And please make sure that you are using the price precision for the price and quantity precision for quantity. Here is my method, maybe it helps (I'm getting the precision numbers from a mapping but you can get it from the exchange info but I didn't want to always get it since it's quite huge): / Formats number to precision (Binance only accepts numbers with correct precision by the symbol) / public formatNumberToPrecision(symbol: string, numberToPrecision: number, precisionType: PRECISION_TYPE): string { functions.logger.debug(Entering into formatNumberToPrecision with Symbol: ${symbol}, Number to precision: ${numberToPrecision}, Precision type: ${precisionType}); numberToPrecision = Math.abs(numberToPrecision); const precision = FUTURES_PRICE_PRECISION_MAPPING.get(symbol); let precisionValue: number; switch (precisionType) { case (PRECISION_TYPE.QUANTITY_PRECISION): { precisionValue = precision.quantityPrecision; break; } case (PRECISION_TYPE.PRICE_PRECISION): { precisionValue = precision.pricePrecision === 0 ? 0 : precision.pricePrecision - 1; } } const pow = Math.pow(10, precisionValue); const truncated = Math.trunc(numberToPrecision pow); return (truncated/pow).toFixed(precisionValue); } On Thu, 6 Jul 2023 at 18:38, jamestangeres @.> wrote: futuresMarketBuy Did you solve your issues? I need your help. Got this issue: Object: null prototype] { code: -1111, msg: 'Precision is over the maximum defined for this asset.' } using this line of code: quantity = parseFloat(20 / 0.02396).toFixed(5); binance.futuresMarketBuy('GALAUSDT', quantity) .then(resp => console.info(resp)) .catch(error => console.error(error)); Thank you for the help. — Reply to this email directly, view it on GitHub <#577 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7FQDVN4R5HHBWCCGC3EC3XO3SZ5ANCNFSM4XFJUVGQ . You are receiving this because you commented.Message ID: @.***>

Thank you for your reply. I appreciates it.. I'm able to buy using futuresMarketBuy.

I have questions, on my test account I have 25 usdt. I tried to use 20 usdt and it getting an msg of 'Margin is insufficient.' so I reduce it to 15 and I able to execute to the futuresBuyMarket.

Why using 20 usdt is margin insufficient?

Here's my code snippet for your reference:

Can you help me review my code if its correct? Thanks again


const usdtAccount = 15; // assuming the current balance of USDT is 25
const currentPrice = 12.629; // assuming the current price of AVAXUSDT is 12.629

const pricePrecision = formatNumberToPrecision(symbol, currentPrice, 'PRICE_PRECISION');
const qtyPrecision = formatNumberToPrecision(symbol, usdtAccount, 'QUANTITY_PRECISION');

const quantity = (qtyPrecision / pricePrecision).toFixed(0); // AVAX quantityPrecision: 0

console.log('quantity: ' + quantity);

binance.futuresMarketBuy(symbol, quantity)
  .then(resp => console.info(resp))
  .catch(error => console.error(error));
hronnie commented 1 year ago

No worries. I'm not a support guy 😉

On 2023. Jul 7., Fri at 13:08, jamestangeres @.***> wrote:

Hey, Yes I did. The trick is that you have to use the price precision from the symbol information. And please make sure that you are using the price precision for the price and quantity precision for quantity. Here is my method, maybe it helps (I'm getting the precision numbers from a mapping but you can get it from the exchange info but I didn't want to always get it since it's quite huge): /* Formats number to precision (Binance only accepts numbers with correct precision by the symbol)

/ public formatNumberToPrecision(symbol: string, numberToPrecision: number, precisionType: PRECISION_TYPE): string { functions.logger.debug(Entering into formatNumberToPrecision with Symbol: ${symbol}, Number to precision: ${numberToPrecision}, Precision type: ${precisionType}); numberToPrecision = Math.abs(numberToPrecision); const precision = FUTURES_PRICE_PRECISION_MAPPING.get(symbol); let precisionValue: number; switch (precisionType) { case (PRECISION_TYPE.QUANTITY_PRECISION): { precisionValue = precision.quantityPrecision; break; } case (PRECISION_TYPE.PRICE_PRECISION): { precisionValue = precision.pricePrecision === 0 ? 0 : precision.pricePrecision - 1; } } const pow = Math.pow(10, precisionValue); const truncated = Math.trunc(numberToPrecision pow); return (truncated/pow).toFixed(precisionValue); } … <#m6062578737899020505> On Thu, 6 Jul 2023 at 18:38, jamestangeres @.> wrote: futuresMarketBuy Did you solve your issues? I need your help. Got this issue: Object: null prototype] { code: -1111, msg: 'Precision is over the maximum defined for this asset.' } using this line of code: quantity = parseFloat(20 / 0.02396).toFixed(5); binance.futuresMarketBuy('GALAUSDT', quantity) .then(resp => console.info http://console.info(resp)) .catch(error => console.error(error)); Thank you for the help. — Reply to this email directly, view it on GitHub <#577 (comment) https://github.com/jaggedsoft/node-binance-api/issues/577#issuecomment-1623984357>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7FQDVN4R5HHBWCCGC3EC3XO3SZ5ANCNFSM4XFJUVGQ https://github.com/notifications/unsubscribe-auth/AA7FQDVN4R5HHBWCCGC3EC3XO3SZ5ANCNFSM4XFJUVGQ . You are receiving this because you commented.Message ID: @.>

Thank you for your reply. I appreciates it.. I'm able to buy using futuresMarketBuy.

I have questions, on my test account I have 25 usdt. I tried to use 20 usdt and it getting an msg of 'Margin is insufficient.' so I reduce it to 15 and I able to execute to the futuresBuyMarket.

Why using 20 usdt is margin insufficient?

Here's my code snippet for your reference:

Can you help me review my code if its correct? Thanks again

const usdtAccount = 15; // assuming the current balance of USDT is 15 const currentPrice = 12.629; // assuming the current price of AVAXUSDT is 12.629

const pricePrecision = formatNumberToPrecision(symbol, currentPrice, 'PRICE_PRECISION'); const qtyPrecision = formatNumberToPrecision(symbol, usdtAccount, 'QUANTITY_PRECISION');

const quantity = (qtyPrecision / pricePrecision).toFixed();

console.log('quantity: ' + quantity);

binance.futuresMarketBuy(symbol, quantity) .then(resp => console.info(resp)) .catch(error => console.error(error));

— Reply to this email directly, view it on GitHub https://github.com/jaggedsoft/node-binance-api/issues/577#issuecomment-1625249967, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7FQDXNPNL7F63EP434QALXO7U35ANCNFSM4XFJUVGQ . You are receiving this because you commented.Message ID: @.***>