Cryptocurrency Trading Bot Part 1 - Setup And Connect To Binance

Introduction

In this post, I'll be going through the basics of the tools that we'll be using to build our cryptocurrency trading bot.

Step One - Download PyCharm and Create Working Directory

If you already have a Python IDE that you prefer to work, then by all means, use what is most comfortable to you. For the sake of these tutorials I'll be using PyCharm, which can be downloaded here (the community edition will work just fine). Once PyCharm is downloaded and opened up, create a new project anywhere you like. This will be where we store all python and text files needed to create the bot.

Step Two - Set up Binance Account and Get API Keys

Registering an account on Binance is easy, you simply click on this link, and sign up. Once you have an account, select the user icon shown below, and then press account.

clickhere.png

Once on the account page, find the box labeled API, and click enable.

Next, enter an API key label (this can be anything), click Create New Key.

Once you have your API Key and Secret Key, back them up in a secure location. What I did is create a file called credentials.txt in the working directory which we created in Step One, and pasted in the API Key on line one, and the secret key on line two. Later on in the post, I'll show you how to setup the Python-Binance API to use the keys stored in the credentials.txt file.

Step Three - Install the python-binance Client

The python-binance client by sammchardy will be our way of communicating with Binance from within Python. This Python Binance API is the most widely used and most well documented. The library can downloaded with pip using the command:

pip install python-binance

I ran into an issue on Windows 10 where the install would fail with exit code 1158, so if you run into this issue follow the steps in this post which should hopefully solve that issue.

Once the python-binance library is installed, we're ready to connect to our Binance account with Python.

Step Four - Connect to Binance

To connect to Binance we'll first need to create a python file in our working directory, I named mine trader.py. In this file paste in the following code:

from binance.client import Client

class Trader:
    def __init__(self, file):
        self.connect(file)

    """ Creates Binance client """
    def connect(self,file):
        lines = [line.rstrip('\n') for line in open(file)]
        key = lines[0]
        secret = lines[1]
        self.client = Client(key, secret)

    """ Gets all account balances """
    def getBalances(self):
        prices = self.client.get_withdraw_history()
        return prices

filename = 'credentials.txt'
trader = Trader(filename)
balances = trader.getBalances()
print(balances)

If you run the following python script, you should get a print out of your account balances. If you get this result, then congratulations, you've successfully connected to Binance via Python!

If you don't get the following result, there are a number of issues that you may have had. Some things to check for:

  • Ensure that Python 3.6 (or newer) is installed on your system
  • If the binance.client library is not found, retry the previous pip install and fix any errors that may arise until the library successfully downloads
  • If you receive an error relating to a Timestamp request error, check this page to figure out how to sync your system clock to match Binance

If you are having problems with this setup that you can't seem to fix, leave a comment on this post, and I'll try my best to help you resolve any issues.

At this point you should have complete access to the Binance API, I encourage you to check out the python-binance documentation and mess around with the commands that are available, because these commands will be the basis for making trades moving forward.

In the next post, we'll go over how to setup TA-LIB and begin generating some indicators based on historical price data.

Thanks for reading,
Andrew

Sort:  

Hi, thank for the guide!
But actually, after a bit of research, I found a cloud platform, trality.com, which has both Python editor and Exchanges' API integrated in one place. So the whole setup for bot creation and deployment is already fine-tuned. And it is free, which is just absolutely great!

This is written for dummies, great job! I like the idea to access the login data from a separate file and not hard code it in the script.

Curious to read part two.

I can't tell you how many times I've started tutorials, failed on the setup stage, and given up, hence the "written for dummies" feel to the post. Glad you liked it!

Congratulations @genesiscrypto! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post
You made your First Vote
You got a First Vote

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!

Nice initiative ! I too am fond of trading bots...
What's the trading strategy you will apply with this bot ? Will this journey will just be about coding a bot or will we have basktestings, discussion about strategies, etc. ?
Following you anyway, and looking forward to see where this goes...

I'm planning on trying out a bunch of different strategies with a very strong emphasis on backtesting. Ultimately I'd like to dive into some machine learning and use TensorFlow to tune the weight of different indicators in deciding on trades.

Great ! Looking forward to this then.
Do you plan on using only classical indicators (MACD, RSI, etc.) with the data available on the Binance API (the kline endpoint) or will you also use more original data like maybe Twitter with sentiment analysis, Reddit, orderbooks (you won't have the history) or trades (you can get the history but it's 500 by 500 so it's gonna take some time with the call restrictions).
I'm developing a bot to, that's why I'm interested in this.

Coin Marketplace

STEEM 0.27
TRX 0.21
JST 0.039
BTC 97039.33
ETH 3673.82
USDT 1.00
SBD 3.88