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

How to learn or calculate weight of an api request #866

Open cuneytkilic opened 1 year ago

cuneytkilic commented 1 year ago

I am trying to coding a buy/sell strategy but i struggle with "max 2400 request" error. Basically, my program buys a coin and create limit order and then checks that order is new, filled, cancelled or expired. Program working fine about 8-12 hours. After that gives a max request error. I dont know, how to control, calculate or learn weight of a request.

Also i wonder that the following 2 request code example has the same weight of request or not ?

1-)

await binance.futuresOrderStatus( coin_name, {orderId: coin_orderId} )
.then(json => { console.log(new Date().toLocaleTimeString() + " - order status:  " + json.status) })
.catch(err => console.log( new Date().toLocaleTimeString() + " -53err- " +  err));

2-)

await binance.futuresOrderStatus( coin_name, {orderId: coin_orderId} )
.then(json => {

for(let i=0; i<9999; i++){
    console.log(i + " - " + json.status)
}

console.log(new Date().toLocaleTimeString() + " - futuresOrderStatus() " + coin_name + " - coin_orderId: " + coin_orderId + " - STATUS: " + json.status)

  if(json.status=='FILLED'){
      console.log(new Date().toLocaleTimeString() + " - " + coin_name + " - order successfull.")
      limitten_alindi = true;
  }else if(json.status == 'CANCELED'){
      console.log(new Date().toLocaleTimeString() + " - " + coin_name + " - order cancelled.")
      iptal_edildi = true;
  }
}).catch(err => console.log( new Date().toLocaleTimeString() + " -53err- " +  err));
GTedZ commented 1 year ago

In the response headers, there is a parameter X-MBX-USED-WEIGHT-(intervalNum)(intervalLetter) which contains how much weight you used. Now for spot it's usually 1200, for Futures its 2400, so you can easily calculate how much weight you have left by doing 2400 - X-MBX-USED-WEIGHT-...-... from the headers

and then you can add a timer that resets the usedWeight to 0 or reset your "speedLimiter" every beginning of the minute.

But what is best for you is to instead learn how to use userData streams, listen to the ORDER_TRADE_UPDATE event which sends you the status of the orders in your account

If you want easier documentation, I suggest using my library binance-lib once v3.0.0 comes out with the new IntelliSense documentation (its coming out in a couple of days)