It has been a while since I posted anything here. I’d been in many new things to fight with. Most important one was moving into Netherlands. Which definitely also has been the most precious one for my life.
Okay, now back to topic. While I have been checking everyday Binance for my cryptocurrency assets, I’ve seen that I was missing some patterns when I was making day-to-day trading. We all have been there, checking the last status of our crypto assets, sliding that screen every second to refresh pairs… But even if I do it so precise, I wasn’t able to make it working when I am asleep (since cryptocurrencies are never-sleeping market) or I am with friends, drinking. So in short; I needed some automation to check prices on Binance, make sell/buy action when necessary, and for last -not least anyhow- notify me when a movement occurs.
But how? I have some minor programming skills, but nothing so deep to cover an API connection to Binance or install it to Raspberry Pi. So it became a personal project for myself. Eventually everything should be working one by one. That’s why this is just a high level explanation, obviously requires detailed work.
- Python Knowledge (even a minor knowledge can work)
- Python Repositories
- PyCharm (IDE)
- Pypi (to be installed on Raspberry Pi)
- Raspberry Pi (obviously) (or a cloud service like cloudocean)
- Linux Lite distribution for Raspberry Pi.
- Binance API keys (key with trading capabilities)
Actually Raspberry Pi is just a little computer to be configured for little projects like IoT, home automation. It consists USB slots, ethernet port, mini-hdmi etc… So it becomes perfect solution for low performance server topics (like ours).
You probably can obtain one from a local retailer website in your region. But for any case, I drop Amazon U.S. Link below.
So this is just the board. You may find yourself looking for enclosing -with cooler on it- but it’s not mandatory for our rig. Also this approach doesn’t have any kind of disk/drive on it. So you’ll require 32gb micro-sd card minimum. Maybe a lesser drive can work but I didn’t check it, DYOR for this.
Alternatively, you can get a cloud server subscription service (like cloudocean), which can benefit a faster installation process (installation of Linux OS etc.). However I didn’t want to share my Binance access keys on a could service (even though it’s a secure platform), ended up with Raspberry Pi 4 model B.
So this is the most tricky part. I’ve got to admit if you are not programming savvy person, this may take some time to handle. Even though python is one of the simplest programming languages.
While I was jumping into this non-sense, I had no idea how python was working so it’ll be depending how motivated you are. I developed on Python 3.6, so considering you’ll also be new, just download PyCharm free-COMMUNITY version.
For Turkish readers, I’ve used a tutorial on YouTube, Burak has explained whole concept pretty clearly. For English users, this link has taken millions of view, good to check out I guess.
First things first
Go to Binance, first we need to get the API keys so that we can connect there by our upcoming code.
It’ll ask you multiple security passwords in the way you are securing your account. Then give you 2 specific codes api key and api secret. Choose spot buy/sell since we’ll be doing trade within Binance. If you are thinking of moving assets between different accounts/exchanges/wallets, you need to also check withdraw/deposit (which is dangerous if someone reaches your keys). Now secure these two codes, you’ll not be able to access these two once again. You need to delete the old ones and create new ones FYI.
Now open up PyCharm and create file named apikeys.py enter your keys into that file with the following way below.
api_key = ‘I03wksdaEaTWRQXmYVDiCkfdslkjwermcxnv324DFGBVCXCVCVfGWTLMKkDJ5Jj’
api_secret = ‘iDwNnIS8I3048lnvckxilxzcgASsdfknvcxkvjıp0565t56DoHij5H4’
Save it wherever you want. We’ll use this file later.
So we’ve installed PyCharm perfect. But we still need libraries to be able to work with Binance and Telegram (I also used apscheduler for repetitive checks).
To reach these libraries, we’ll need PyPi library to begin with. If you’ve installed python you’ll already have this library included, but next time when we’d like to install to raspberry we’ll make a manual installation on it.
In the end, after we pop-up PyCharm window and create a project, there will be a terminal tab waiting for us at the bottom.
If you’ve found the terminal, we have 3 installations waiting for us. To the terminal window, enter these three individual codes.
If you are on windows swap “python3” with “py”.
python3 -m pip install python-binance
python3 -m pip install apscheduler
python3 -m pip install telegram_send
It also must be installing pre-requisites for these modules.
So let me explain you what these modules do…
python-binance is giving us python functions that itself handling all API connection details. It has all the functions that API offers us. You can check the functions from it’s own documentation link. Basically you can check your account balance, create spot buy/sell order, create margin buy/sell order, get history prices etc…
apscheduler is giving us the option to run a code block in a period, creating a scheduled loop. But it has more than it looks, follow the documentation link for further usage.
telegram_send I believe the name is pretty self-explanatory… Helps you to send messages to an account. For proper installation, at telegram app you need to find @botfather from telegram users. send “/newbot” message to it. Then follow the instructions. In the end you’ll have token to access.
As the document link describes; after the install via terminal, it’ll ask you to run “telegram-send –configure” code for configuration. During config, you need to enter the token the @botfather provided to you. Now you have a connection to send messages from code… Well done!
Now it’s time to code. This is the part your imagination/experience will be rewarding (or not i dunno). You need to implement your own buy/sell patterns into the code via python-binance library brings to you.
Installing Linux to Raspberry Pi
Since you’ve got the Raspberry Pi, you need to have an SD card to fulfill the OS requirements. Connect the micro-sd to your computer. Download the Raspberry Pi Imager so that we can have a operating system working. I am running a Linux on it.
This will bring you a complete command-line experience. So now digging into Linux coding a bit. After the installation has been successfully done, just plug it into the Raspberry Pi. The system will boot-up almost immediately. Will ask a password to be defined. Later on we’ll use that password to access the OS from somewhere else.
sudo apt-get update
sudo apt-get upgrade
These will make your Linux to get aligned with the recent distributions.
Now to be able to use “pip” command to install libraries, let’s go ahead and install it. As PyPi’s documentation link shows, you can directly install from command line with the codes below.
Now you can install all the libraries in the previous lines I have mentioned, just in the same way we did in the terminal in PyCharm.
So in the end, add your apikeys.py and coding you’ve made, to the drive and plug the raspberry pi to internet. Run your python file via “python3 mypythoncode.py”.
Now you are good to go. Your bot is running. Nice job.