Binance Trading Bot in Python

In this article we will see how to build a trading bot linked to a cryptomarket. In this example we will make a Binance Trading Bot in Python. It can be reuse your own way on other exchange, for stocks as your convenience. Reminder that it’s not a financial advisement and investing is at your own risks.

Now let’s get into the detail and see what we require to create our little robot.






Binance API

First you need to have a Binance account, if you don’t create one by clicking here.

Afterward click on your profile and go in API Management. Create your API key and save your public API key and secret API key somewhere safe. If you want more detail on this operation Binance has made a tutorial here.

Verify that trading is enabled and deposit money in the asset you want to trade otherwise it will be difficult for our robot to trade! 

Don’t forget to put some security in your account like 2 FA, login with SMS code etc…


If you don’t have python please install it. Don’t forget to add the python.exe path into your path environment variable if it wasn’t automatically do.

When it’s installed, we need to setup libraries we will use for our script. Please open your cmd and install:

pip3 install python-binance 
pip3 install ta

The first library will allow us to do action in Binance and the second will provide financial technical analysis tool to help our robot make decision.



Python Script Details



Access Binance Account

Now that we have everything set we can start coding!

Let’s retrieve our account data by accessing Binance website. Include your own public API KEY and Secret API KEY.

We will look for our wallet quantity amount on interested pair. In this example it will be USDT/ETH. 

Financial Analysis

I’m not a Financial expert but I have some basic knowledges. If you would like to add information on the financial part, fell free to share in the comment section.

In my example I have used the basic yet strong Moving Average Convergence Divergence (MACD) and Related Strength Index (RSI) on 4 Hours time periods.

We will use MACD to check if we are in a Bullish or Bearish market and according to the case we will adjust our action.

RSI is a great indicator to see if on a certain time period the asset is oversold or overbought.

My following Bot action has been setup after analyzing pattern on March period on this asset. Moreover, I have set a stop loss to prevent having massive losses.


Bot Actions



After my analysis on last month market and my knowledge on indicators I went up with following action for our trading bot.

I used MACD Trend, RSI variation to forecast Buy/Sell action. In that case I also cancel or put Stop loss according to the action we are doing (Stop Loss if Buying and cancel them if Selling). Feel Free to adjust your quantity, add your own indicators. Above is just an example from my perspective but you can adjust it to yours. 

You can have a look at the full code here on my Github.



Script Scheduling





Now that our script is ready to be use we don’t want to have to run it manually every 4 Hours. So let’s schedule it.

To begin, we need to create a bat file to enable the launching of the script by the scheduler.

To do so open your cmd and type:

where python

Press enter and copy the resulting folder. Now open a bloc-note and insert:

"pathopython.exe" "C:\"

Save this bloc-note as a .bat file.

Now Open task scheduler in windows as administrator.

  • In action select the .bat file you created.
  • In Trigger set start one time and set repeat the task every 4 Hours for an indeterminate period of time.
  • For the rest set option at your convenience according to your system (all rights on, launching even in sleep mode etc…).

Check your Binance account to look at what did your bot. Look at your orders historic and keep recording your trades!

Linux Debian

I did it on my side on Raspbian to let my Raspberry Pi run script automatically. I think a raspberry is the perfect tool for such daily script execution. 

To do so install all packages with pip3 in your cmd if not already done.

Afterward transfer your python file into your raspberry.

Launch your cmd and write:

crontab -e

Go at the end of the script and erase “#” and mark:

0 */4 * * Pythonpath >> path/log.txt

Now your program will launch your python file automatically every four hours.

If you want to have a look at the log you will have a log file on your desktop.





Let’s Trade




I hope it can help few people to start understanding Finance, Coding and IT in general. On my side the system is set on a Raspberry Pi. Don’t hesitate to write about possible improvement or your feedback in the commentary section.

I will be waiting for long-term results and see if this is a good bot strategy or not!

Bibliography  :

Binance Trading Bot in Python Made by Guillaume Niay