Skip to main content

Binance API Library

Status Python

A python library that implements the Binance Exchange REST API and Web socket communication.

Installation

$ pip install solo-binance-api

You can uninstall the library anytime by running:

$ pip uninstall -y solo-binance-api

Getting Started

Various implemenation for methods has been developed, a Docs is in progress to better illustrate the functions made, below is is an initial setup you can add to monitor logs and initialize your connection.

import os
import logging

# Configuring logger
logger = logging.getLogger('algo-trading')
logger.setLevel(level=logging.INFO)

# Setting logger handler
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)

# Declaring binance account credentials
API_KEY = os.getenv('binance_api_key')
SECRET_KEY = os.getenv('binance_secret_key')

# Initializing REST and Socket clients
api_client = RequestClient(api_key=API_KEY, secret_key=SECRET_KEY, debug=False)
subscription_client = SubscriptionClient(
    api_key=API_KEY, secret_key=SECRET_KEY)

# Defining callback function and error function for socket subscriptions
def callback(data_type: 'SubscribeMessageType', event: 'any'):
    if data_type == SubscribeMessageType.RESPONSE:
        print('Event ID: ', event)
    elif data_type == SubscribeMessageType.PAYLOAD:
        print('=========== Subscription Payload Data ===========')
        PrintBasic.print_obj(event)

        # Uncomment below to stop subscribtion
        # subscription_client.unsubscribe_all()
    else:
        print('Unknown Data: ', event)


def error(e: 'BinanceApiException'):
    print(e.error_code + e.error_message)

Usage

# Example 1: Getting old trade lookup (REST)
old_trade = api_client.get_old_trade_lookup(symbol='BTCUSDT',
                                            limit=10)

print('======= Example 1: Old Trade Look up Data =======')
PrintMix.print_data(old_trade)
print('=================================================')


# Example 2: Getting CandlStick Data (REST)
candle_data = api_client.get_candlestick_data(symbol='BTCUSDT',
                                              interval=CandlestickInterval.MIN1,
                                              startTime=None,
                                              endTime=None,
                                              limit=10)

print('======= Example 2: Kline/Candlestick Data =======')
PrintMix.print_data(candle_data)
print('=================================================')


# Example 3: Getting Symbol Price Ticker Data (REST)
symbol_price = api_client.get_symbol_price_ticker(symbol='BTCUSDT')

print('====== Example 3: Symbol Price Ticker Data ======')
PrintMix.print_data(symbol_price)
print('=================================================')


# Example 4: Getting Order Data (REST)
get_order = api_client.get_order(symbol='BTCUSDT',
                                 orderId='some-order-id')

print('=========== Example 4: Get Order Data ===========')
# PrintMix.print_data(get_order)
print('=================================================')

# Example 5: Posting an Order to binance (REST)
post_order = api_client.post_test_order(symbol='BTCUSDT',
                                        side=OrderSide.BUY,
                                        ordertype=OrderType.MARKET,
                                        timeInForce=None,
                                        quantity=9,
                                        quoteOrderQty=None,
                                        price=None,
                                        newClientOrderId=None,
                                        stopPrice=None,
                                        icebergQty=None,
                                        newOrderRespType=OrderRespType.FULL)

print('=========== Example 5: Post Order Data ==========')
PrintMix.print_data(post_order)
print('=================================================')

# Example 6: Subscribe to Trade Stream
subscription_client.subscribe_trade_event(symbol='btcusdt',
                                          callback=callback,
                                          error_handler=error)

# Example 7: Subscribe to Symbol Book Ticker
subscription_client.subscribe_symbol_bookticker_event(symbol='btcusdt',
                                                      callback=callback,
                                                      error_handler=error)

# Example 8: Subscribe to Candle Stick
subscription_client.subscribe_candlestick_event(symbol='btcusdt',
                                                interval=CandlestickInterval.MIN1,
                                                callback=callback,
                                                error_handler=error)

Project Status

This project has great potential for improvements for the moment only the Binance Spot API is implemented based on their documentation, currently i wont be updating or modifying it due to time shortage but feel free to contribute!

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

License

Released under the MIT License

Authored and Maintained by Abdeen Mohamed

Metadata

Release files for solo-binance-api 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for solo-binance-api 0.1.2
File Size Uploaded Provenance Actions
solo-binance-api-0.1.2.tar.gz 32.8 kB Without provenance Details for solo-binance-api-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for solo-binance-api 0.1.2
File Interpreter ABI Platform Actions
solo_binance_api-0.1.2-py3-none-any.whl Python 3 none any Details for solo_binance_api-0.1.2-py3-none-any.whl

Total release size: 80.1 kB

Release files / solo-binance-api-0.1.2.tar.gz

Download URL solo-binance-api-0.1.2.tar.gz
Size 32.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3608f3d104d0ef5e5ad687b60ca2891ac10eb875d3e8156ff13d315ad7c2ad05
BLAKE2b-256 checksum
How to use checksums
e055da00e5714599572db0051af70b188776d6dc615b737b51312b79cc062a90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.5

Release files / solo_binance_api-0.1.2-py3-none-any.whl

Download URL solo_binance_api-0.1.2-py3-none-any.whl
Size 47.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eca844f25c4f1b49782e28a54342d163fb5a3abaee81dbcdabb9e85043ca4b17
BLAKE2b-256 checksum
How to use checksums
652cda281b6c726864ef483bd8d897dc983a1bc23a579a40941aca3265ac9365
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.5

Security for solo-binance-api 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page