Making a simple Raspberry Pi Bitcoin/Ethereum trading bot

with rocket.chat and Slack status reporting

[update] I have extended the bot to be able to trade Ethereum as well!

I recently got my Raspberry Pi Zero Wifi and I tell you this thing is a game changer. I have used RasPis before but since this device has a much smaller footprint, uses less energy, is cheaper and has wifi on board, it's perfect for low profile or IoT projects.

Raspberry Pi Zero W (c)RASPBERRY PI FOUNDATION

I wanted it to do things without having to pay a lot of attention to it and the first thing that came to my mind was:

Why not create a simple trading bot that can trade Bitcoin and Ethereum automatically.

The trading bot in action

The idea is simple: The Raspberry Pi Zero uses the Coinbase API to check the prices periodically and buys and sells Bitcoin, whenever it makes sense. You should be able to give it a budget and see what it can do with it.

This bot is meant to be a lurker that runs for weeks or months and waits for an awesome bitcoin price to buy and later sell. It's up to you how you want to use it!

What the bot should be able to do:

What the bot will not do

For control reasons I want to be informed whenever BTC/ETH were sold or bought. For this I use a simple webhook for my favorite (and selfhosted) chat platform rocket.chat. But it also works with a webhook from Slack!

This is the PHPTrader bot


What we'll need to get it running

  1. A Raspberry Pi or just Linux box
  2. A Coinbase account with some funds in the EUR/USD Wallet
  3. PHP 5 or higher
  4. PHP Composer to install the libraries
  5. If you want reporting you'll need a rocket.chat or Slack webhook

Step 1: Installing the bot

As easy as it gets:

  1. Download the repo by using the command git clone https://github.com/geek-at/phptrader.git or download as ZIP file
  2. Go into the php trader directory cd phptrader
  3. Install the required composer files composer install
  4. Rename example.config.inc.php to config.inc.php

Composer installing the coinbase library

Step 2: Enter Coinbase API key and secret

Getting your key is easy too:

Coinbase API page (c)Coinbase.com

  1. Go to https://www.coinbase.com/settings/api
  2. Click on "+ New API Key"
  3. You will then see the API key creation dialogue. Choose the Bitcoin/Ethereum and EUR/USD Wallet
  4. Select all permissions
  5. Confirm
  6. When you see the API key and secret, enter them in the config.inc.php file of the PHPTrader bot under COINBASE_KEY and COINBASE_SECRET.

Coinbase API Key details (c)Coinbase.com

Warning: Keep your key and secret safe! When an attacker gains access to them it's like handing them over your house key.

(Optional) Step 3: Set up a rocket.chat or Slack webhook

For Slack use this tutorial to get the webhook. and fill it in the ROCKETCHAT_WEBHOOK field in your config.inc.php

Rocket.chat:

Logging into rocket.chat
Note: You need to be an admin to setup a webhook for rocket.chat. Either host your own instance (it's very easy with Docker) or ask an admin you trust to do it for you.

  1. On your chat instance go to Administration -> Integrations
  2. Click on "New Integration", then "Incoming WebHook"
  3. Set it to enabled, choose a post channel or user
  4. Click "Save Changes"
  5. You will see now a "Webhook URL", copy it and put it in the ROCKETCHAT_WEBHOOK field in config.inc.php

Final step: Tell to bot when to buy and sell

Using the following commands, the bot will create a transacitons.json file where the amount in EUR, BTC, start price and sell price will be saved so we can track them over restarts.

Examples:

After setup you can start the watchdog: The heart of the bot is an infinite loop that checks periodically (every 10 seconds) for price changes. You can start it yourself by using the command php trader.php watchdog or use the start.sh script which will put the process in background and log to /var/log/phptrader.log

./start.sh

or

nohup php trader.php watchdog >> /var/log/phptrader.log 2> /var/log/phptrader.err &

You can watch the log file so you can see what the bot is doing at the moment

Bot working like charm

Raspberry Pi watching your coins for you

Tags: bitcoin raspberry pi rocket.chat slack trading bot iot php coinbase ethereum

Comment using SSH! Info
ssh feb62@ssh.blog.haschek.at

Comments


Get new posts by email
(~ one email every couple of months & no spam)