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.5k stars 7.47k forks source link

BinanceUs - rate limits issue in websocket subscriptions #23525

Open ttodua opened 3 weeks ago

ttodua commented 3 weeks ago

Operating System

n/a

Programming Languages

No response

CCXT Version

n/a

Description

the below code is just copied from another topic where user had other issue. however, this below code immediately throws binanceus NetworkError: connection closed by remote server, closing code 1008

Code


async function main() {
    let exchangeConns = []
    exchangeConns.push(await excInit('coinbaseexchange'))
    exchangeConns.push(await excInit('binanceus'))

    //kinda pseudocode, but the idea is that all markets are added from both exchanges. 
    let exchangeMarkets = []
    exchangeMarkets.push (await exchangeConns[0].fetch_markets())
    exchangeMarkets.push (await exchangeConns[1].fetch_markets())

    //iterate through all of the markets for both exchanges and and start the tradeWS function for each market
    for (let i = 0; i < exchangeConns.length; i ++){
        for (let j = 0; j < exchangeMarkets[i].length; j++){
            if (exchangeMarkets [i][j]){
                tradesWS(exchangeConns[i], exchangeMarkets[i][j].symbol)       
            }
        }
    }
}

async function tradesWS(exchange:any, symbol:string) {
    const limit = 1
    while (true) {
        try {
        //returns an array of trades
            const data = await exchange.watchTrades (symbol, limit)
            for (let i = 0; i < data.length; i++){
                console.log(data[i])                                    
            }   
        } catch (e) {
            console.log(exchange.id, e); break;
        }
    }
}

main()
ttodua commented 3 weeks ago

@pcriadoperez you might have more context on these, you worked in the past with similar issue

pcriadoperez commented 2 weeks ago

@ttodua , correct this is due to hitting the rate limit of binance. The user can either introduce some sleeps between calls or can also try this PR that looks to create a rateLimit within ws (https://github.com/ccxt/ccxt/pull/23168)