How to Build a Cryptocurrency Auto-Trader Bot with PHP? 💰
This tutorial will walk you through the full process of building a bitcoin bot with PHP - from setup, on to your first execution of an automated trade, and beyond.
I should not need to tell you but, a couple of months ago you could buy the cryptocurrency Ether for $11, it rapidly went up to $43 (I bought in between those prices) and has now gone to over $335 as of June 2017. Those kinds of gains are nearly unbelievable to a traditional investor and yet these are across the board in this space.
Excited yet? So here is a scenario:
You made a ton of money on cryptocurrencies and have some concerns about shuffling it through your bank because of potential capital gains tax issues. There are places that have a solution for you if you want to be able to use this money for other investments. These places won’t make you photograph your license and send it in, just use an email and they provide you with a BTC deposit wallet, demo accounts, APIs, then when you are ready, you send money in and it’s ‘go time’, you can trade everything from treasury bonds to Forex using Cryptocurrencies as your base monetary instrument.
But, you say, I am a coder who likes to automate things, surely we can fire up some BTCbot and we can have it just do the work for us, it will make us millions in our sleep, right?
I don’t want to write a bot and publish it with a single strategy and just say “here, use this”, I don’t think that is helpful to anyone, I would rather give you the tools and show you how to write strategies yourself, show you how to set up data collection for the strategies and how to implement them in a trading system and see the results.
Also, I don’t want to create this in a new or arcane language, I want this written in PHP which the biggest number of people are familiar with and in a framework (Laravel - here's a great premium course for sale, and a bunch of free articles if you're not familiar with it) that is simple to use but powerful enough to let you can create what you need. If you think PHP is just for web pages, read on, this should surprise you.
I like to build systems. I have been working on this post for a while and it represents a good deal of non-derivative custom work. If you have read some of my other tutorials you know that I like to write tutorials that “I wish that I had found instead of having to to write”, so you are in for a thorough read, with a lot of copy-paste style recipes.
Let’s get started.
Steps we are going to take:
- Get boilerplate/framework installed.
- Walk through the core parts of the system, see what is where.
- Install and configure the software we need.
- Account creation at the brokerages we will be using, setting up the API keys for the scripts.
- Run tests and examples.
- Set up websocket streams to get data.
- Finding strategies for our automated agents.
- Deep dive into Indicators and Candles available to us.
- Coding up our first agent.
- Testing the agent.
- A few closing words about the risks you are taking.
Get boilerplate/framework installed (Bowhead)
You can find the repository for the Bowhead boilerplate at it’s Github repository. It's a full application already, but we'll be using its functionality to get the stuff in this post done.
It is recommended you use the extremely Laravel-friendly Homestead Improved Vagrant box for a good, isolated development environment you can get started with in under 5 minutes. If you're unfamiliar with Vagrant, here's an excellent re-introduction, and if you'd like to dig deeper, this premium book will teach you amazing things.
git clone https://github.com/joeldg/bowhead.git cd bowhead composer install cp .env-example .env sudo pecl install trader echo "extension=trader.so" | sudo tee /etc/php/7.1/mods-available/trader.ini sudo phpenmod trader
Now let's explain the the current folder structure of the app.
This is where all our console commands are located.
BitfinexWebsocketCommand.php- Stream market data from Bitfinex
CoinbaseWebsocketCommand.php- Stream market data from GDAX
ExampleForexStrategyCommand.php- Forex example strategy
ExampleStrategyCommand.php- Our example of a strategy
ExampleUsageCommand.php- Basic usage examples
GetHistoricalCommand.php- Pull in historic data from broker
OandaStreamCommand.php- Stream market data from Oanda
Is where all the utility classes that are available are found.
Bitfinex.php- Bitfinex API wrapper
BrokersUtil.php- Utilities for various brokers
Candles.php- All 60 TALib candle methods wrapped
Coinbase.php- GDAX API wrapper
Console.php- Console color, tables and progress
Indicators.php- 21 TALib indicators and moving averages.
Oanda.php- Oanda API wrapper
OneBroker.php- 1Broker API wrapper
Other.php- possible indicators, not implemented yet
testStrategy.php- Here is your test strategy
Whaleclub.php- Whaleclub API wrapper
Extras and some testing data, these scripts are SKLearn price forecasting scripts taken from a study on beer consumption I thought was really useful, these might be used for market price predictions.
close_prediction.py- SKLearn script to predict a closing price
ohlc-btc.csv- Sample CSV data, if needed
open_prediction.py- SKLearn script to predict an opening price - a python script in the root dir called ‘streaming.py’ which is part of the Oanda streaming command.
If you execute
php artisan, you should see something like the following, the part you are interested in is below.
Redis and MySQL
Redis really does not need any tweaking out of the box, it's installed and ready if you're using Homestead Improved.
MySQL will need a database and a few tables. Change the credentials in the
.env file (create it from
.env.example if it doesn't exist).
DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret
Let's add the DB dump into MySQL:
mysql -u homestead -psecret < app\Script\DBdump.sql
Open up the database in a tool like Sequel Pro and you will the sample data in the bowhead_ohlc (open, high, low, close) table.
API accounts we need in order to set up automated trading
Full disclosure: Where possible, I have set up bonuses for you on these links, all sites below offer free accounts which do not require ‘verification’ and do not require a deposit. The links are referral links which also bring me some perks if you sign up.
1) Whaleclub is the main site we want to trade on for this tutorial. They key their market data off of the Bitfinex websocket and match with Oanda streaming data for Forex. This site allows you to trade many instruments and commodities with BTC at up to 20x leverage, Forex up to 222x as well as providing BTC-based binary options. They have a simple, easy to understand interface and an excellent API. The API key is found by clicking on your name in the upper-right, and clicking on API. (use DEMO API key to start)
2) 1Broker the secondary site we want to trade on, they are similar to other BTC-based market makers and have a ‘trader follow’ system as well that is fairly interesting, particularly to get people following ‘you’. The API key is found on the right, just under the email icon, there is a small box with what looks like sliders on it, then click on Access & API Management.
4) Coinbase/GDAX is what used to be called ‘Coinbase Exchange’ and is now called GDAX. I have been automated-trading there since they first opened. The API key is found at the far upper-right, then click on API and create your keys.
5) Bitfinex - you need an account here with an API key so we can get Cryptocurrency quotes. API keys are found under ‘Account’ then click on API.
6) Poloniex is like Bitfinex but supports many alt-coins. API keys are found under Settings - API Keys.
7) TradingView is not mandatory, but you will want an account there because all the indicators bowhead uses can be viewed on charts to help you build your strategies.
The reasoning behind this combination is that the Whaleclub and 1Broker APIs are rate limited, WC only allows 60 requests per minute, if we want to make sure we have streaming real-time data to work with we need to stream from a BTC brokerage. Same with Forex.
Definitely look around on these sites and see what they have to offer, I’ve been around the block with a lot of brokers and market maker sites and for BTC, these are all good as of June 2017. For Forex, Oanda is great, but for the purposes here of trading using BTC we just need their streaming Forex data.
Once you get the API keys for these sites, you will want to put them in your
Continue reading %How to Build a Cryptocurrency Auto-Trader Bot with PHP? 💰%