Quickstart for Binance with Python

Posted By : Anoop Sharma | 04-May-2020

In this article, we will explore the Python-Binance library. We will figure out how to get to a wide range of data with respect to your record, and furthermore how to make HTTP POST/GET requests to perform exchanges and different activities. 

The main thing you are going to need to do is login to your Binance account. In the drop down menu for your profile tab, pick the API Management choice. Create your API key (public/private) and record them some place. These ought to be kept in a protected spot since any individual who approaches these keys could access your binance account.

Project Setup

  • Create a virtual environement in python. 
  • Open any Code editor and activate your environment.
  • open terminal and install binance library for python.
pip install python-binance
  • Now open a file (let's say index.py ) and perfrom the given below operations.

from binance.client import Client
#Add API key in the given place
public_key = ''
private_key = ''
client = (Client(public_key, private_key)

 

  • Now to verify whether we have made a connection with binance marketplace via API, we call a method called get_all_tickers(). This method fetches the last current price for each trading pair on the binance platform. Code would look like this 

_tickers = client.get_all_tickers()
print(_tickers)

 

  • The output for this. if your Keys were correct and you were able to make connections with the binance servers would looks like this.

[{'symbol': 'ETHBTC', 'price': '0.02393200'}, 
{'symbol': 'LTCBTC', 'price': '0.00535500'}, 
{'symbol': 'BNBBTC', 'price': '0.00194130'}, 
{'symbol': 'NEOBTC', 'price': '0.00102800'}, 
{'symbol': 'QTUMETH', 'price': '0.00745500'}, 
{'symbol': 'EOSETH', 'price': '0.01358500'}, 
{'symbol': 'SNTETH', 'price': '0.00009823'}.......]

 

Let's make Trade!!

Binance API provides test methods to verify whether your code is working or not. It provides two methods TEST_BUY and TEST_SELL method.

Binance Buy method - 

Test buy method look like this:

 

client.create_test_order(
    symbol=pair,
    side=Client.SIDE_BUY,
    type=Client.ORDER_TYPE_MARKET,
    quantity=amount,
    recvWindow=10000)

 

In the above method, the inputs that we need to perform this operation are

  • Symbol - Trading pair on which you want to perform operation. For eg: BNBBTC
  • side - What type of trade you want to do. That is Buy or Sell.
  • type - Order can be of different types. For eg : Market, Limit, Stop Loss Limit etc
  • quantity - The amount that you want to buy or sell.
  • recvWindow - It tells the time till which API to be active.

Binance Sell Method - 

Binance Sell method looks like this.

client.create_test_order(
    symbol=pair,
    side=Client.SIDE_SELL,
    type=Client.ORDER_TYPE_MARKET,
    quantity=amount,
    recvWindow=10000)

 

Conclusion

Binance Library for python provides  different and many more methods. They are very useful if you want to make any application which works on binance Platofrm.

Related Tags

About Author

Author Image
Anoop Sharma

Anoop is a Python developer, who has worked on Python Framework Django and is keen to increase his skillset in the field. He has a zest for learning and is capable of handling challenges. He is a team player and has good enthusiasm.

Request for Proposal

Name is required

Comment is required

Sending message..