Make a Bitcoin/Ethereum trading bot

Nikos Titomichelakis
4 min readFeb 18, 2021

I always wanted to make a trading bot, to trade cryptocurrency while sleeping. After doing a little research I found that Binance’s API was great for doing exactly that. So, in the steps below I will demonstrate how to make a trading bot to trade whatever crypto you like, in whatever pair you like.

So let’s get started.

Getting an API Key from Binance

First things first, you need to go to you Binance Dashboard. From the dropdown menu select, Settings -> API Management

API Management section in Binance Dashboard

Give your API Key a name and hit Create API

API Key Creation

After you complete your 2-step authentication and entering your secret, you should be able to copy your API key. Keep it in a safe place for now.

Coding the bot

We are gonna use Node.js to create our bot. We will use the axios package to send requests to the Binance API. For this example, I will use the ETH/EUR pair. First, create a .env file to the root of your folder and enter the following:
API_KEY=
API_SECRET=

Enter the values of your previously created Binance API Key and the secret you used for it.

Then create a Js file. Name it as you like. Here is the actual code of the bot:

Review of the code

We have a run() function that starts when we run our bot. This function calls the tick() function every x milliseconds. In the example above I have set it up, every 6000ms, which equals to 1 min. We also set our binance client which connects us with the Binance API and will create our orders. The allocation, specifies the percentage of the available funds that we want to trade with. I have chosen 10%. The spread sets the percentage of the flunctation of the price after which an order is triggered. For example, if the ethereum price is at 1800$ and the spread is set to 0.01, an order will be triggered in +/- 18$ of the current price. When the tick() function is triggered, we do a for-loop to cancel every order which was previously active. We also need the current price of the crypto we are interested in. To do that, we will use the API of coingecko. We won’t use Binance cause we want a global average of the cryptos price. You can find the coingecko API here.

We are interested in the GET /simple/price endpoint. In the Ids section, input the cryptocurrency you are interested in trading. In the vs_currencies input the fiat you want to trade. In the example below we use ethereum with euro.

If you execute the request, Coingecko will give you a request url like the one below:

https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=eur

This is the url you need to change in the axios.get request (Line 17), if you want to trade in other pairs. After that, we set a bunch of variables for the trading prices for both buying and selling. We send a request to binance client, to create a sell order and a buy order and we wait.

A few things to keep in mind

First of all, don’t put money you cannot afford to lose in this bot. Trading is risky. I am not responsible for any money loss.
Secondly, depending on what crypto pair you choose, you might want to play with the settings of the bot and tweak some things, like the interval or the spread. For example, 0.01 change for ethereum is 18$ give or take at the moment. Which is more than ok to make money, as ethereum isn’t that volatile. But for bitcoin 0.01 is 500$ which is pretty low considering bitcoin can drop 1–2k within minutes, so your orders might get triggered a lot. Thus, take your time to see what best suits you.
Also, Binance has a limit of a buy order being at least 10$ so you must at least have 10$ in your account. I have made an if-statement to cancel the buy orders if the amount is insufficient so don’t worry about that part if you don’t care for buy orders simultaneously with sell orders :)

Final Thoughts

If anything was done correctly, your bot should work now. To run it, if you don’t have the packages needed installed you should first install axios, ccxt and dotenv. Do that by running:

npm install axios ccxt dotenv

After that you should be able to run your bot by running:

node [filename].js

The bot should start and immediately put a sell order:

bot sell order output

Also, the order should appear on Binance:

Binance Sell Order

And that was it. For the record, running the bot last night while sleeping made me 27 euros. It’s not a lot, but hey, it’s free money while sleeping.

P.S. If I helped you, consider donating some crypto love:

ETH: 0x1d75cE77D4020b23352E0C65638c39dFcbBDAf54
BTC: 1J9f4bNQcQ2p6cUUhdeTvYr2YfLyScAzus

--

--