P2p binance api python

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

a simple connector to Binance Public API

License

binance/binance-connector-python

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

* update user agent * update test case

Git stats

Files

Failed to load latest commit information.

README.md

Binance Public API Connector Python

This is a lightweight library that works as a connector to Binance public API

  • Supported APIs:
    • /api/*
    • /sapi/*
    • Spot Websocket Market Stream
    • Spot User Data Stream
    • Spot WebSocket API
    pip install binance-connector
    from binance.spot import Spot client = Spot() # Get server timestamp print(client.time()) # Get klines of BTCUSDT at 1m interval print(client.klines("BTCUSDT", "1m")) # Get last 10 klines of BNBUSDT at 1h interval print(client.klines("BNBUSDT", "1h", limit=10)) # API key/secret are required for user data endpoints client = Spot(api_key='', api_secret='') # Get account and balance information print(client.account()) # Post a new order params = < 'symbol': 'BTCUSDT', 'side': 'SELL', 'type': 'LIMIT', 'timeInForce': 'GTC', 'quantity': 0.002, 'price': 9500 > response = client.new_order(**params) print(response)

    Please find examples folder to check for more endpoints.

    • In order to set your API and Secret Key for use of the examples, create a file examples/config.ini with your keys.
    • Eg:
    # examples/config.ini P2p binance api python api_key=abc123456 api_secret=cba654321

    Binance supports HMAC, RSA and ED25519 API authentication.

    # HMAC: pass API key and secret client = Client(api_key, api_secret) print(client.account()) # RSA Keys client = Client(api_key=api_key, private_key=private_key) print(client.account()) # ED25519 Keys api_key = "" private_key = "./private_key.pem" private_key_pass = "" with open(private_key, 'rb') as f: private_key = f.read() spot_client = Client(api_key=api_key, private_key=private_key, private_key_pass=private_key_pass) # Encrypted RSA Key client = Client(api_key=api_key, private_key=private_key, private_key_pass='password') print(client.account())

    Please find examples/spot/wallet/account_snapshot.py for more details on ED25519. Please find examples/spot/trade/get_account.py for more details on RSA.

    Spot Testnet is available, it can be used to test /api/* endpoints.

    from binance.spot import Spot as Client client = Client(base_url='https://testnet.binance.vision') print(client.time())

    If base_url is not provided, it defaults to api.binance.com .
    It’s recommended to pass in the base_url parameter, even in production as Binance provides alternative URLs in case of performance issues:

    PEP8 suggests lowercase with words separated by underscores, but for this connector, the methods’ optional parameters should follow their exact naming as in the API documentation.

    # Recognised parameter name response = client.cancel_oco_order('BTCUSDT', orderListId=1) # Unrecognised parameter name response = client.cancel_oco_order('BTCUSDT', order_list_id=1)

    Additional parameter recvWindow is available for endpoints requiring signature.
    It defaults to 5000 (milliseconds) and can be any value lower than 60000 (milliseconds). Anything beyond the limit will result in an error response from Binance server.

    from binance.spot import Spot as Client client = Client(api_key, api_secret) response = client.get_order('BTCUSDT', orderId=11, recvWindow=10000)

    timeout is available to be assigned with the number of seconds you find most appropriate to wait for a server response.
    Please remember the value as it won’t be shown in error message no bytes have been received on the underlying socket for timeout seconds.
    By default, timeout is None. Hence, requests do not time out.

    from binance.spot import Spot as Client client= Client(timeout=1)
    from binance.spot import Spot as Client proxies = < 'https': 'http://1.2.3.4:8080' > client= Client(proxies=proxies)

    The Binance API server provides weight usages in the headers of each response. You can display them by initializing the client with show_limit_usage=True :

    from binance.spot import Spot as Client client = Client(show_limit_usage=True) print(client.time())
    'data': 'serverTime': 1587990847650>, 'limit_usage': 'x-mbx-used-weight': '31', 'x-mbx-used-weight-1m': '31'>>

    You can also display full response metadata to help in debugging:

    client = Client(show_header=True) print(client.time())
    'data': 'serverTime': 1587990847650>, 'header': 'Context-Type': 'application/json;charset=utf-8', . >>

    If ClientError is received, it’ll display full response meta information.

    Setting the log level to DEBUG will log the request URL, payload and response text.

    There are 2 types of error returned from the library:

Оцените статью