Making a simple Raspberry Pi Bitcoin/Ethereum trading bot

with rocket.chat and Slack status reporting

Posted by Christian Haschek on 2017-04-17
Never miss a post by liking this blog on Facebook

[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:

  • I want you to buy 50€ worth of Bitcoin and sell when the 50€ became 52€
  • I want you to buy 100€ worth of Bitcoin when the Bitcoin price hits 1000€/BTC and sell when the 100€ are worth 150€
  • Buy low, sell high
  • Automatically buy bitcoin after the next bubble pops

What the bot will not do

  • Make market analysis and detect bubbles and plunges
  • Make you rich
  • Do high volume trades. This bot is a slow lurker investor

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/chrisiaut/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.

  • php trader.php buy [amount in EUR] [sell when price increases by EUR]
  • php trader.php sell [amount in EUR] [sell when this BTC/ETH price is reached]
  • php trader.php order [amount in EUR] [sell when price increases by EUR] [buy at BTC/ETH price]

Examples:

  • Buy 10 EUR in BTC/ETH and sell when they're worth 12 EUR: php trader.php buy 10 2

  • Add sell order. Sell 100 EUR/USD when BTC/ETH price reaches 2000: php trader.php sell 100 2000

  • Add buy order for 100 EUR when 1 BTC/ETH is worth 1000 EUR and sell when the 100 EUR are worth 110 EUR: php trader.php order 100 10 1000

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
9.684

There are no ads on this (https enforced) blog. Please help me to keep it that way
1ChrisHMgr4DvEVXzAv1vamkviZNLPS7yx
0x1337C2F18e54d72d696005d030B8eF168a4C0d95