question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The CCXT fetch_balance is about 5/6 times slower than get_account on Binance

See original GitHub issue

MUST READ THIS BEFORE SUBMITTING ISSUES (read the links, then delete this message before submitting):

Make sure your local version of CCXT is up to date. Check by comparing the output of ccxt.version to https://github.com/ccxt/ccxt/blob/master/package.json#L3

  • OS: MacOS
  • Programming Language version: Python
  • CCXT version: 1.55.69
import ccxt
from binance.client import Client
import time

apiKey = "*****"
secret = "*****"

# binance-python client
client = Client(apiKey, secret, testnet=True)

# ccxt client
exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
    "apiKey": apiKey, 
    "secret": secret,
})
exchange.set_sandbox_mode(True)

start_time = time.time()
exchange.fetch_balance()
end_time = time.time()
print(f"ccxt: {end_time - start_time}")

start_time = time.time()
client.get_account()
end_time = time.time()
print(f"binance: {end_time - start_time}")
ccxt: 1.3862950801849365
binance: 0.24796199798583984

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kroitorcommented, Sep 5, 2021

@wkamer

Still a little slower, but around 500ms.

If you don’t want any delays, you can disable the rate limiter entirely, at your risk (since if you disable the rate limiter, your requests may be too aggressive for the exchange API and they might ban you temporarily for too many requests).

# -*- coding: utf-8 -*-

from pprint import pprint
import ccxt


print('CCXT Version:', ccxt.__version__)


exchange = ccxt.binance({
    'apiKey': 'YOUR_API_KEY',
    'secret': 'YOUR_SECRET',
    'enableRateLimit': False,  # <<<<<<<<<<<<<< disable rate limiter
})

markets = exchange.load_markets()

# exchange.verbose = True  # uncomment for debugging purposes if necessary

while True:
    try:
        print('--------------------------------------------------------------')
        previous_timestamp = exchange.milliseconds()
        balance = exchange.fetch_balance()
        current_timestamp = exchange.milliseconds()
        print(exchange.iso8601(current_timestamp), 'balance:')
        pprint(balance)
        print('Fetched in', current_timestamp - previous_timestamp, 'milliseconds')
        # change this delay as you like
        print('Sleeping for 5 seconds...')
        exchange.sleep(5000)  # sleep 5 seconds between the requests, for example
    except Exception as e:
        print(type(e).__name__, str(e))

How to handle this when I use the fetch_balance as an api endpoint? I do an instantiate every time I call the fetch_balance.

Reuse the exchange instance instead – create it once at the start of your script. Then do a fetch_balance call. Then leave your script running, and repeat the consequent fetch_balance calls on the already existing exchange instance, instead of exiting the script and restarting it again and again. The snippet in this post does exactly that.

0reactions
wkamercommented, Sep 5, 2021

Thanks, I tried the script your way and see it has good results by. Still a little slower, but around 500ms. I see that you refer to your wiki about instantiate it every time and with that it looks up the market every time? How to handle this when I use the fetch_balance as an api endpoint? I do an instantiate every time I call the fetch_balance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Binance: Timestamp for this request is not valid #300 - GitHub
That error on Binance means your system time is out of sync with their clock by more than 5 seconds... Check the UTC-alignment...
Read more >
Exchanges — ccxt 2.4.71 documentation
The ccxt library is a collection of available crypto exchanges or exchange classes. Each class implements the public and private API for a...
Read more >
How to use fetchBalance() parameters with ccxt - Stack Overflow
The params argument on fetchBalances() is for passing exchange specific data to the request. As each exchange offers different endpoints, ...
Read more >
how to get balance on binance (with python & ccxt) - YouTube
in this video i show you how to get your balance on binance or any other exchange using the ccxt library and pythonthis...
Read more >
ccxt-dev/ccxt - Gitter
I'd like to do a conditional check before running a particular method based on whether or not it is supported by that particular...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found