Crypto trading bot on Raspberry Pi 3 using ProfitTrailer and Bittrex

Neha M
Chatbots Life
Published in
9 min readJan 5, 2018

--

Firstly, let me quell any misconceptions about this article making you rich overnight — I am not a trader and this article is not about trading but simply about setting up your own trading bot on a pi. My friend wanted to run a trading bot on Bittrex for crypto using ProfitTrailer and I wanted another pi project :)

Top 3 Bot Tutorials

1. How to Make a Facebook Messenger Chatbot in 1Hr

2. Introduction to API.AI

3. AWS setup for Deep Learning

You will need…

  • Rasberry Pi 3 (with its power adaptor)
  • Larger than 8GB micro SD card for the pi
  • Internet connection (This tutorial is for a WiFi setup on the pi)
  • A laptop or computer that has a memory card reader (or an external memory card reader). I’m using an Ubuntu machine so the tutorial is based on that.
  • A license from ProfitTrailer
  • A verified account on Bittrex

Topics being covered…

  1. Setting up Raspbian (a Raspberry Pi OS) on the SD card. During this tutorial I was using Raspbian Stretch.
  2. Setting up WiFi headlessly for the pi
  3. Enabling SSH headlessly
  4. Using SSH to connect to the pi
  5. Securing your pi
  6. Setting up ProfitTrailer
  7. Run PT in the background

Setting up Rasbian on the SD card

Before connecting the raspberry pi to any power source, we must install an operating system on the pi — Raspbian. For this you will need a computer or laptop that has a memory card reader or you will need to connect an external memory card reader.

Note: For this tutorial, I used the Lite version of Raspbian as most of the setup is via the terminal but you can also download the full version which comes with a GUI and then use a terminal for the instructions.

Download a Raspbian image

Install etcher.io

It will help you install Raspbian on the SD card.

Flash SD card

  • Insert SD card into reader
  • Format SD card using Fat32 format (this step is optional)
  • Launch etcher on your computer
  • Select the Raspbian image you just downloaded
  • Etcher should automatically find your SD card
  • And flash your SD card

Need more HELP?
If you need further assistance in installing Raspbian on the pi, you can use the NOOBS installer. More information is available here — https://www.raspberrypi.org/documentation/installation/installing-images/README.md

Setting up WiFi headlessly for the pi

To avoid connecting the pi with peripherals (monitor, keyboard, mouse), I decided to set up the WiFi headlessly. So essentially, we will need to add a config file to the boot partition of the SD card that was just flashed with the Raspbian image using your computer.

Note: If you are directly connecting the pi via an ethernet cable, you can skip this section.

Find your network

First, we need to find the details (ESSID and Encryption) of your WiFi network. This will be very similar to the name of your WiFi.

  1. Enter the following in the terminal of the computer you are using (once again, assuming you are using a Linux based machine):
sudo iwlist wlan0 scanning | egrep 'Cell |Encryption|ESSID'

Note: This is a modified version of sudo iwlist wlan0 scan which has a lot of additional information. We only need the ESSID.

2. Note down the ESSID and make sure you have your WiFi password handy.

Adding network config to your pi

When adding the WiFi password, we need to create an encrypted form so that a spy can’t simply find your password in plain text,

  1. Enter the following in the terminal to get the encrypted PSK:
wpa_passphrase "MyWiFiESSID" "MyWiFiPassword" >> wpa_supplicant.conf

Two things are happening here:

wpa_passphrase "MyWiFiESSID" "MyWiFiPassword" will create a network profile with your password replaced by a token so that it is not saved as plain text.

>> wpa_supplicant.conf will add the network profile to your configuration file.

2. Now we need to remove the plain text password that also gets copied into the config file. In the terminal:

sudo nano wpa_supplicant.conf

3. Your WiFi profile will look like this:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
ssid="MyWiFiESSID"
#psk="MyWiFiPassword"
psk=131e1e221f6e06e3911a2d11ff2fac9182665c004de85300f9cac208a6a80531
}

4. Remove #psk="MyWiFiPassword" and add the line ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev, if it’s missing.

5. Save it.

6. Copy wpa_supplicant.conf to the boot partition of your flashed SD card.

More info can be found here — https://howchoo.com/g/ndy1zte2yjn/how-to-set-up-wifi-on-your-raspberry-pi-without-ethernet and https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md

Enabling SSH headlessly

Now that the WiFi configuration is set up, we still need a way to access the pi over the network to avoid connecting the peripherals — so we need to enable SSH before we actually start up the raspberry pi.

Create a file called ssh with no extension and put it in the boot partition of the SD card — same place as the wpa_supplicant.conf file.

More info can be found here — https://www.raspberrypi.org/documentation/remote-access/ssh/README.md

Using SSH to connect to the pi

Now you are ready to start up the pi so connect the power source — that should start it up. It took a couple of restarts for it to connect to the WiFi and have the SSH running.

A really cool thing with the new Raspbian is that you don’t need to fix the IP address anymore (like we needed to before) — all you need to use is raspberrypi.local as the hostname.

Open up your terminal,

ssh pi@raspberrypi.local

I used a Linux machine to connect to my pi, but for Windows users, you will need to install an additional tool — Putty, in order to SSH into another computer.

OR

Enter, if you know the IP address of the pi.

ssh pi@<ip address>

The first time you connect, it will ask you if you trust this fingerprint. Select ‘Yes’.

You will need to enter your pi’s password, which should be raspberry unless you changed it.

If you connect successfully, your terminal’s prompt will change to pi@raspberrypi

Note: For more information on setting up SSH — https://www.raspberrypi.org/documentation/remote-access/ssh/README.md

If you have problems using raspberrypi.local you will need to find the pi’s address. There are many ways to do this and some of the methods can be found here — https://www.raspberrypi.org/documentation/remote-access/ip-address.md

Securing your pi

I’m not a security expert but considering you are setting up a crypto bot trader on this pi, you would want to take at least some basic precautions. For this tutorial, I’m just covering key pair login for SSH since the steps are slightly more complex than the other methods (see note below).

Note: There are many additional ways to secure the pi but it’s beyond the scope of this tutorial so you can find some options here — https://www.raspberrypi.org/documentation/configuration/security.md

Using key-based authentication

Very naively, a key pair is created — a private and public key. The public key is stored on your pi and the private key on the computer you will be using to SSH into the pi. When the pi recognises your private key with the help of your public one on it, it lets you login without the need of entering your pi’s password over the network.

  1. Generate a key pair on your computer,
ssh-keygen -t ed25519

I’ve used ed25519 which is a newer and better algorithm to generate the keys.

2. Use the default for the file names and preferably enter a passphrase, although this is optional.

3. Once you’ve generated the keys, they should have been automatically saved in ~/.ssh and if you followed the defaults then the two filenames will be id_ed25519 and id_ed25519.pub . The .pub file is your public key and the one we will copy onto the pi. The other is your private key — NEVER share it with anyone or over the network or email or take a screenshot etc. etc.

4. Copy the contents of your .pub file into your pi’s ~/.ssh/authorized_keys file. If the authorized_keys file isn’t created, create it using,

touch authorized_keys

Then open the file using nano and copy your public key in.

5. Now restart your ssh connection to your pi, and this time you won’t need to enter the pi’s password but simply the key’s passphrase (if you had set it).

6. If you use multiple computers to connect to your pi, you will have to generate a separate set of key pairs.

7. Let’s disable password login so only the computers with an SSH key pair can connect to your pi.

sudo nano /etc/ssh/sshd_config

8. Change the following settings to ‘no’

ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no

9. Make sure to reboot and login with your key pair :)

More details on this process can be found here — https://www.ssh.com/ssh/keygen/

Enable password for sudo

By default, the pi doesn’t ask you to enter the password whenever you run a command with sudo. This is very good practice for any Linux based computer. So first let’s change the password to something other than raspberry (worldwide password for all pi’s) and then we enable it for sudo.

  1. Enter passwd and follow the instructions to change your password
  2. Open this file using nano sudo nano /etc/sudoers.d/010_pi-nopasswd
  3. And change your pi user setting to this pi ALL=(ALL) PASSWD: ALL Basically, you will be changing NOPASSWD to PASSWD .

Now every time you need to run a superuser command, it will ask for a password. This is important especially since new software can’t be installed without this password.

Let’s update the system,

sudo apt-get update
sudo apt-get upgrade

Mnemonic: I always get confused as to whether it’s update first or upgrade, so here’s a mnemonic to remember the order. Look at the third letter of each word (basically both start with ‘up’ so we can remove those): ‘d’ and ‘g’. d comes before g in the alphabet, so update comes first and then upgrade .

Setting up ProfitTrailer

In order to run ProfitTrailer you will need to first purchase a license from their website https://profittrailer.com/. During checkout you have to provide your Bittrex API key (not the secret key) to be activated with the license. Once activated ProfitTrailer can be installed on the pi. See below to generate the API keys on Bittrex.

Create API keys on Bittrex

For anyone who already has an account on Bittrex I would suggest making a second account exclusively for PT: this will save your bot from getting confused by any manual trades you may be doing.

You would have created at least one key to buy the ProfitTrailer license (you will need a second one as well), but in case you haven’t then read on.

  1. Login to your Bittrex account (make sure you activate two factor authentication)
  2. Click Settings (top right)
  3. Select API keys from the right menu
  4. And add a new key — the key’s secret will be shown only at the time you create it, so make sure you copy it down (preferably on some paper, for the extra paranoid :) )
  5. Add another key as the bot requires two
  6. The API keys can be set in the file application.propertieshttps://wiki.profittrailer.io/doku.php/application.properties

Installing ProfitTrailer

Now all we need to do is download ProfitTrailer onto the pi. The zip of the latest release can be found here — https://github.com/taniman/profit-trailer/releases

Copy the link of the zip (not the link of the site) into your home directory,

wget https://github.com/taniman/profit-trailer/releases/download/v1.2.5.5/ProfitTrailer.zip

Then we need to unzip,

unzip ProfitTrailer.zip

There should be a ProfitTrailer directory created.

Note: All the various settings for the bot can be found onProfitTrailer’s Wiki pages — https://wiki.profittrailer.io/doku.php/start

Running PT in the background

We decided to use pm2 to have it run headlessly on the pi.

  1. Install npm and pm2,
sudo apt-get install nodejs npm
sudo npm install pm2 -g

2. Go into the PT directory and run pm2,

pm2 start pm2-ProfitTrailer.json

More info on the various pm2 aspects for PT here — https://wiki.profittrailer.io/lib/exe/fetch.php/installing_profit_trailer_on_a_vps.pdf

3. Once PT has started, you can view your dashboard at raspberrypi.local:8081/login or <pi-ip-address>:8081/login

Hope this guide was useful and if there any comments/edits/problems/questions, please do ask away :) All feedback is welcome.

References

I’ve added most of the references in the relevant sections but PT also has their own pi tutorial —
https://wiki.profittrailer.io/doku.php/raspberry_pi_guide

The wiki also has extensive information on all the various settings and and capabilities of PT. And if all else fails their Telegram channel is an amazing community ready to help at any time —

--

--