This archive contains the source code of "crypto-bot", which is a cryptocurrency trading bot (bot.py
) together with info-site (app.py
) created with Flask.
It is designed to work in 1h intervals (data gathering, making predictions, placing orders) on Binance, the biggest cryptocurrency exchange. It is configured to predict BTC, basing on hourly data of 12 selected cryptocurrencies. Its features:
- data gathering and trading using binance.com API
- neural network (LSTM) predictor trained on Google Colab
- automatic data backups using Git
- info-site with logs and stats using Flask
- implemented on Raspberry Pi Zero W (armv6l)
- Install Raspberry PI OS Lite (tested on 2020-02-14 release) and setup internet access. Make sure to have Python 3.7 and git installed. The default working directory is assumed to be
/home/pi/
. - Clone this repository
git clone https://github.com/kamsec/crypto-bot.git
- Install requirements
Linux:
cd crypto-bot
Windows:pip3 install -r requirements_rpi.txt
pip install -r requirements_win.txt
- Run
setup.py
with command:It will create the filespython3 setup.py
config.ini
,secrets.ini
,logfile.log
. - In order to check the balances and operate Binance account, bot needs to have access to API keys. In
secrets.ini
you will find the following content:In place of[DEFAULT] testnet_api_key = *** testnet_api_secret = *** mainnet_api_key = *** mainnet_api_secret = ***
***
placeholders provide your binance.com API keys (testnet or/and mainnet) without any quotation marks. Visit https://www.binance.com/en/support/faq/360002502072 for more information. [OPTIONAL] To enable orders, inconfig.ini
you can set:Inorders_allowed = True
config.ini
you can also change working binance.com enviroment betweentestnet
andmainnet
according to provided keys.enviroment = testnet # can be changed to mainnet
- Run the bot with:
If everything works correctly it will produce the output in logfile.log of the following format:
python3 bot.py
If an hour haven't passed from last record in2021-08-23 09:32:45 [INFO] Bot started 2021-08-23 09:32:47 [INFO] [BALANCE] 0.29 USD, 0.0010283 BTC 2021-08-23 09:32:47 [INFO] [PREDICTION] DOWN: 42%, UP: 58%
data/
folder, the messagewill appear and"[WARNING] [UPDATE] No new records available yet. Try again in: 0:47:50.464589"
[PREDICTION]
will not be made this hour, but this is correct. Bot will also createlogfile.csv
which is used only by Flask info-site (app.py
). - In order to make the bot working at the beginning of every hour (1st minute), add the cron job. Enter the command:
and at the end of the opened file add the following line:
crontab -e
1 * * * * cd /home/pi/crypto-bot && python3 /home/pi/crypto-bot/bot.py
- In order to create GitHub backups bot requires a remote repository and SSH key for authentication. Having remote repository defined, create new ssh key:
When the prompts appear, press enter three times so the file will be saved in default location
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
/home/pi/.ssh/
under default nameid_rsa
with no passphrase. Open the ssh agent with commandand add created keyeval "$(ssh-agent -s)"
Now add the SSH key to your account on GitHub, and create repository for this bot. For more information, see https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account. Inssh-add ~/.ssh/id_rsa
config.ini
set:backups_allowed = True
Instructions from this section will make bot.py
script running once in the first minute of every hour.
After running crypto-bot for the first time, you can view the results on Flask info-site, by running:
python3 app.py
and visiting the http://IP:5000/
in your browser (port is always 5000, but you need to check the IP assigned to your Raspberry Pi), for example http://192.168.0.107:5000/.
If you want to host the Flask info site on Raspbery Pi as service, you can use Gunicorn with nginx. To do this, take the folliwing steps:
- Install nginx.
sudo apt-get install nginx
- Create
crypto-bot
file in/etc/nginx/sites-available/
with the following comand:and write the following content (assumed localhost and port 5000):sudo nano /etc/nginx/sites-available/crypto-bot
server { listen 80; server_name 123.123.123.123; location / { proxy_pass http://127.0.0.1:5000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Scheme $scheme; } }
- Create
crypto-bot.service
file in/etc/systemd/system/
with the following comand:and write the following content (assumed localhost and port 5000):sudo nano /etc/systemd/system/crypto-bot.service
Add and start the service with commands:[Unit] Description=Gunicorn instance to serve crypto-bot After=network.target [Service] User=pi Group=pi WorkingDirectory=/home/pi/crypto-bot # Environment="PATH=/home/pi/crypto-bot/myprojectenv/bin" # if using virtual env ExecStart=/home/pi/.local/bin/gunicorn --bind 0.0.0.0:5000 --workers 3 wsgi:app Restart=always [Install] WantedBy=multi-user.target
sudo systemctl daemon-reload sudo systemctl enable crypto-bot.service sudo systemctl start crypto-bot.service
You can now visit information site on your RaspberryPi IP on port 5000, e.g. http://192.168.0.107:5000/, and this site will be available whenever Raspberry Pi is running and has internet connection. To access the site outside the local network, you should perform port forwarding in your router settings.
-
To control the configuration of the bot, you can edit constants in
config.ini
, for example:status_active = True # set to False to make the bot inactive orders_allowed = False # set to True to allow placing orders backups_allowed = False # set to True to allow GitHub backups enviroment = testnet # set to mainnet to change binance.com enviroment
-
To stop the bot completely, type:
crontab -e
and remove or comment the line that defines cron job:
1 * * * * cd /home/pi/crypto-bot && python3 /home/pi/crypto-bot/bot.py
-
To restart the bot (erase log history and reset balances), delete
logfile.log
,config.ini
,logfile.csv
(if exists) and run :python3 setup.py python3 bot.py
-
To control Flask info-site, you can use commands:
sudo systemctl stop crypto-bot.service # to stop the app sudo systemctl restart crypto-bot.service # to restart the app
-
To change the prediction model place new model in
models/model/
, and inconfig.cfg
set:model_path = models/model/***
and in
setup.py
set the variableMODEL_NAME = '***'
with
***
as new model name.
Training neural network models is performed manually on Google Colab, and files used for it are located in training-google-colab
directory.
Tools for adding new data pairs and running bot simulation (placing orders during using past data) are located in tools/
directory.
In main directory, file tests.py
contains some manual tests and allows to run&test individial functions from the project.
- Recent version of Firefox sometimes display parts of info-page incorrectly - just use Chrome in that case.
- If you encounter Binance error "Timestamp for this request is 1000ms ahead of the server's" you should sync your system time with Binance. More information: ccxt/ccxt#773