A Python 2/3 client for the Binance REST and WebSocket APIs https://www.binance.com/restapipub.html
python-binance-api return an object that parsed from json
for each method, set return_json=True
get the original API response.
pip install python-binance-api
- requests
- simplejson
- six
- websocket-client
- Events
from binance.client import BinanceRESTAPI, BinanceWebSocketAPI
rest_client = BinanceRESTAPI(api_key, secret_key)
ws_client = BinanceWebSocketAPI(api_key)
rest_client.ping()
rest_client.server_time().server_time
# return json
rest_client.server_time(return_json=True)["serverTime"]
depth = rest_client.depth("BNBBTC")
# total voulme
depth.get_depth_volume()
# best bid price
depth.get_bids_highest_price()
# best ask price
depth.get_asks_lowest_price()
rest_client.aggregate_trades(symbol="BNBBTC")
agg_trades = rest_client.aggregate_trades("BNBBTC")
for agg_trade in agg_trades:
print agg_trade.id, agg_trade.price, agg_trade.qty
rest_client.klines("BNBBTC", "1w")
klines = rest_client.klines(symbol="BNBBTC", interval="1w")
for kline in klines:
print kline.open_time, kline.close_time, kline.high, kline.volume
pre_day = rest_client.statistics_24hr("BNBBTC")
print pre_day.price_change_percent, pre_day.high_price
prices = rest_client.all_prices()
for price in prices:
print price.symbol, price.price
tickers = rest_client.all_book_tickers()
for ticker in tickers:
print ticker.symbol, ticker.bid.price, ticker.bid.qty
order = rest_client.new_order("SNMBTC", "SELL", "LIMIT", "GTC", 1, '1')
# order id
print order.id
rest_client.new_order_test(symbol="SNMBTC", side="SELL", type="LIMIT", time_in_force="GTC", quantity=1, price=1)
order = rest_client.query_order(symbol="SNMBTC", order_id=order.id, orig_client_order_id=order.client_order_id)
print order.status, order.type
order = rest_client.cancel_order("SNMBTC", order.id, order.client_order_id)
print order.id
orders = rest_client.current_open_orders("SNMBTC")
for order in orders:
print order.id, order.status, order.side
orders = rest_client.all_orders(symbol="SNMBTC")
for order in orders:
print order.id, order.status, order.side
account = rest_client.account()
# balance information
for balance in account.balances:
print balance.asset, balance.free, balance.locked
trades = rest_client.my_trades("BNBBTC")
for trade in trades:
print trade.id, trade.price, trade.qty
withdraw = rest_client.withdraw(asset="BNB", address="address", amount=0.1, name="test")
print withdraw.success
deposit_history = rest_client.deposit_history()
for deposit in deposit_history:
print deposit.asset, deposit.amount, deposit.insert_time, deposit.status
withdraw_history = rest_client.withdraw_history()
for withdraw in withdraw_history:
print withdraw.asset, withdraw.address, withdraw.amount, withdraw.apply_time, withdraw.status
stream = rest_client.start_user_data_stream()
print stream.listen_key
rest_client.keepalive_user_data_stream(stream.listen_key)
rest_client.close_user_data_stream(stream.listen_key)
from binance.models import DepthCache
# keep a depth cache
depth_cache = DepthCache(rest_client.depth("BNBBTC"))
def on_update(delta):
depth_cache.update(delta)
for bid in depth_cache.bids:
print bid.price, bid.qty
ws_client.depth("BNBBTC", callback=on_update)
def on_print(kline):
print kline.event_time, kline.open, kline.high, kline.is_final
ws_client.kline("ETHBTC", "1m", callback=on_print)
def on_print(trade):
print trade.symbol, trade.price, trade.qty
ws_client.aggregate_trade("ETHBTC", callback=on_print)
from binance.models import OutBoundAccountInfoEvent, ExecutionReportEvent
def on_print(data):
if data.event_type == OutBoundAccountInfoEvent.EVENT_TYPE:
for balance in data.balances:
print balance.asset, balance.free, balance.locked
elif data.event_type == ExecutionReportEvent.EVENT_TYPE:
print data.symbol, data.side, data.price, data.original_quantity
ws_client.user_data_keepalive(callback=on_print)
# return json also available for WebSocket
def on_print(data):
print data["e"]
ws_client.user_data_keepalive(callback=on_print, return_json=True)