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

Oasis DeFi Platform 

What is Oasis?

Oasis is a platform for decentralized finance launched in November 2019 by MakerDAO.

You can use it to exchange token,borrow DAI (stablecoin soft-pegged to the US Dollar) and earn savings.


Secure protocol built on Ethereum

Oasis is built on top of audited and formally verified smart contracts created by Maker, the industry leader in secure decentralized finance.

Completely permissionless

You—and only you—control your assets. Oasis is a decentralized, non-custodial platform, accessible using an Ethereum-supported wallet.

“Decentralized Finance (DeFi) is the movement that leverages decentralized networks to transform old financial products into trustless and transparent protocols that run without intermediaries.”

My curiosity on crypto and blockchain environment has brought me to this point. Thanks to Hasheur and CryptoMatrix I’ve learn a lot about DeFi. I now understand better why this can be an huge revolution in finance sector.



Why I decided to use it?

ETH 2.0

Personally I was very happy with my app account with good staking benefit (up to 16%). Yet, I wanted to stack Ethereum to prepare myself to become a main validator with 32 ETH for Ethereum 2.0 and earn validations rewards.

Meanwhile I was accumulating ETH I was a bit disappointed with the staking reward I was getting (only 3% earning per year) which is still incredible compared to traditional saving account but far from other staking rewards (CRO 16% per year).


Afterward, I heard a lot about DeFi and I started to do some research about it. I found out it could resolve my problem by optimizing my earning.

I can still play the long term HOLD and getting profit from it. I looked on several solutions and I’ve chosen the one from the best organization in this area: MakerDAO. So I followed up and went on the platform linked to the organization: Oasis.




  • Buy Ethereum
  • Get Metamask
  • Transfer fund to Metamask
  • Borrow DAI
  • Reinvest your DAI
  • Pay your Debt



OASIS Tutorial


Buy Ethereum

First you need to buy Ethereum. I suggest you to buy on app which is a great application to enter the crypto world easily. You can buy crypto by your credit card or bank transfer (no transaction fee at this date) and you can download the app on this link. You will see how great is this app is with the review I’ve made on this article.

Get Metamask

Second Step will be to have a web wallet. The easiest way is to have Metamask a google chrome extension or directly on your Brave crypto wallet. Brave is an amazing web browser and you can find why on the article I’ve made here. You can download Brave on this link if you want to support the website. So now we need to transfer our fund to Metamask. Normally you can copy your crypto address on Metamask.





Transfer your fund to Metamask



Now you can go on your app: Crypto wallet -> ETH -> Transfer -> Withdraw.

Withdraw your ETH to your metamask address you’ve copy. Once done your should have your ETH on your Metamask!

OASIS Borrow

Go on OASIS Borrow to start and connect your Metamask. Now let’s start borrowing!


As you can see their is currently NO FEE on borrowing with ETH and BAT.  

So what are we doing right now? We are borrowing DAI (USD stablecoin) at 0% Fee

Yet as you can see you have something called liquidation ratio. If what the value you have in collateral is below this line you will lost the amount you borrowed plus liquidation fee 13%. We will make several scenario to see what can happen.

Let’s say I borrow 1000 DAI (1000$), put 10 ETH as collateral and have as above 150% of Liquidation Ratio.

Worst Scenario

In this bad case the price will go down and goes below your liquidation ratio. In this example ETH goes under 150$.

You will loose the amount you’ve borrowed (1000$).  You will have a 13% penalty because you went under liquidation ratio (130$).

In this case you will loose around  1130$ of ETH. Moreover your ETH have less value. You have to understand the risk of it as their is in any financial market.

Most Cases

In most cases you don’t put you at risk and are putting a good amount of collateral to be sure you are well above liquidation ratio.

In that scenario price almost didn’t move. So you had change your DAI to CRO to  stack them 1 year at 16%.

After this year you decide to pay your debt. So you pay back what you paid 1000$ in DAI which you’ve converted from CRO and your Ethereum and have earned at 16% during this time.

Best Scenario

Now let’s say that Ethereum price rise up. So you still have your collateral that has more value right now and you have earn CRO at 16% during one year.

You are winning a lot! Imagine that CRO also have a better price than where you made your deposit, then you had made a great investment.

You won from staking and from Ethereum price rising up! Eventually, you can easily paid your debt with the profits made.

So add value according to your economical situation. Play with finance will always be risky so be careful there is no guaranteed money.  Always look to be above the liquidation ratio and normally if the market stands or go higher you should make profit.

When you have set up your borrowing information and collateral you can generate the amount of DAI you want to have.


Vault DAI

In my scenario I have a collateralize ratio up to 280%. Right now ETH price is at 212$ so I’m well above my liquidation price which is at 112$ ETH.  

Reinvest my DAI

After generating my DAI I will go again on my app and look at my DAI deposit address.

So go on your wallet -> transfer -> deposit -> DAI. Copy the address and from OASIS in your Wallet Balance click on send in the DAI line. Then enter the amount you want to transfer and paste your DAI address.

Wait for the validation of your transaction (around 5 minutes) and you will have DAI in your app. 

Then you can stake at the high earning rate of

  • Keep it in DAI up to 10% (*if less than 500 MCO stacked)
  • Change it into CRO and earn up to 16% (*if less than 500 MCO stacked)

In the first case you will be sure to earn 10% of your earning. In the second one it is 16% but the CRO price can go down or up which can impact your return on investment!

Pay your Debt

When you decide that you want to close your smart contract loan you can easily do so by going into Paying back button in Oasis.

It will close your Smart contract and here it comes to an end!


Decentralize finance is a very interesting tool which has a lot of benefit. Let’s have a look at the advantages and disavantages.




You can earn from this platform:

  • stability fee low (even 0%)
  • Flexible reimbursement without time constraint
  • Decentralize leverage if Ether price rise up                   


No third party and you keep your privacy. You are not tracked. Sites is fully decentralized but teams works on security. It is a main subject very well handle with constant improvements and audits.

Moreover you don’t risk a centralize entity to stole and go away with your fund, it’s fully yours. Their can’t be any frozen account which is a big plus.





Financial risks from value as we saw earlier:

  • Stability fee (6% for USDC)

  • Liquidation threshold

  • Liquidation penalty                                        


If their is a hack their is not centralize company to help you (even if in reality MakerDAO foundation find way to help his users). 

Indeed, most of the time institution like banks are keeper of your funds and guarantee the security of it and provide you a support team for any problems.


Finally I gave you all my knowledge about OASIS and Decentralized Finance. It’s up to you to use it. I hope you have learn new things.

in my opinion, I have found a good answer at one of the issue I have. Now i have to make sure everything goes well until I have my 32 ETH!

ETH bullrun

Don’t hesitate to reach me if you have any question about this article. Have a good day!

Guillaume Niay