[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.
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 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
- A Raspberry Pi or just Linux box
- A Coinbase account with some funds in the EUR/USD Wallet
- PHP 5 or higher
- PHP Composer to install the libraries
- If you want reporting you'll need a rocket.chat or Slack webhook
Step 1: Installing the bot
As easy as it gets:
- Download the repo by using the command
git clone https://github.com/geek-at/phptrader.gitor download as ZIP file
- Go into the php trader directory
- Install the required composer files
- Rename example.config.inc.php to config.inc.php
Step 2: Enter Coinbase API key and secret
Getting your key is easy too:
- Go to https://www.coinbase.com/settings/api
- Click on "+ New API Key"
- You will then see the API key creation dialogue. Choose the Bitcoin/Ethereum and EUR/USD Wallet
- Select all permissions
- When you see the API key and secret, enter them in the
config.inc.phpfile of the PHPTrader bot under COINBASE_KEY and COINBASE_SECRET.
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
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.
- On your chat instance go to Administration -> Integrations
- Click on "New Integration", then "Incoming WebHook"
- Set it to enabled, choose a post channel or user
- Click "Save Changes"
- You will see now a "Webhook URL", copy it and put it in the ROCKETCHAT_WEBHOOK field in
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]
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
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