0% found this document useful (0 votes)
418 views

PyCryptoBot On AWS EC2. Run Your Python Crypto Bot(s) in The - by Michael Whittle - Coinmonks - Med

PyCryptoBot on AWS EC2.

Uploaded by

P-a des Mazis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
418 views

PyCryptoBot On AWS EC2. Run Your Python Crypto Bot(s) in The - by Michael Whittle - Coinmonks - Med

PyCryptoBot on AWS EC2.

Uploaded by

P-a des Mazis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Published in Coinmonks

Get started Sign In

You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
Search

Michael Whittle
May 25, 2021 · 14 min read · Listen

PyCryptoBot on AWS EC2 Michael Whittle


2.8K Followers
Run your python crypto bot(s) in the cloud… CCIE R&S #24223 | Network Architect | Security
Specialist | Software Developer | Blockchain
Developer | Data Scientist

Follow

More from Medium

Michael Whi… in Python in Plain Eng…

Howleto Know What


sh
to Trade

Mintoshi

The Complete Guide to Pionex


Dual Investment Bot — Pionex
Trading Bot Series

Alex Stakonski

Generate Financial Market Charts


with Python in 3 Minutes

Rahul Khanna in Nerd For Tech

How to Calculate Cross Rates in


Forex

Help Status Writers Blog Careers Privacy Terms About


Image by ParkMyCloud
Knowable

My Python crypto bot has really gone from strength to strength. As of today there
are 496 unique users using the bot and on average each user is running at least 2
bots according to Github.

I also setup a Telegram group chat for bot users to discuss crypto trading,
conCgurations, and support each other. It really has taken the load oE me and
the community really has been amazing. There are currently 215 people chatting
in the group and 36 are active in the chat at the moment.

The invite link to the main group chat is:


https://t.me/joinchat/09hYKfelbRY2MDNk

There is a breakout group for developers to discuss the code, pull requests, and
issues.

The invite link to the dev group chat is:


https://t.me/joinchat/3xFJO_rWLcoxOGFk

Quite often people ask if the bot can be run in the cloud either because they don’t
have a system they can leave on permanently or their Internet connection may be
poor or intermittent. I know some people have bought a Raspberry Pi and
installed it on there others have installed it in Amazon AWS EC2 or as a Docker
container in Amazon AWS ECS.

This article will explain how to run your PyCryptoBot(s) *FOR FREE* in Amazon
AWS EC2 for a year. Amazon AWS very kindly provides a “free tier” for personal
use, studying, etc. You just need to take care you don’t include non-free items by
accident but for the purpose of running a server instance in the cloud it’s free
(for a year). It’s probably worth mentioning that Amazon AWS ECS does not
have free tier options. If you are planning on running your PyCryptoBot(s) as
Docker containers you are better oE installing it locally or on an Amazon AWS
ECS instance.

I’m going to walk you through this step by step as easily as I can. It still requires
some technical competence but I’m hoping to make this as easy as possible for
you. If you get stuck on any parts or would like me to be explain anything in
more detail please do let me know on Telegram or in the comments.

I’m going to make the assumption you are familiar with PyCryptoBot already and
that you have an account either on Coinbase Pro or Binance and have API keys to
facilitate your access.

If you are completely new to this project I recommend the following as pre-
reading.

Python Crypto Bot (PyCryptoBot)


Bitcoin, Bitcoin Cash, Ethereum, Litecoin, Stellar on Coinbase Pro
medium.com

What’s new in PyCryptoBot 2?


Automated crypto trading on Coinbase Pro and Binance
medium.com

PyCryptoBot Results and Config


I wanted to share my bot results for the last 30 days and the config I’m
using. I get asked this question a lot in…
medium.com

PyCryptoBot with Telegram


I received a feature request in the PyCryptoBot Github repo to add
Telegram support to the crypto bot. I implemented…
medium.com

Success with PyCryptoBot


“Whooptydoo. But what does it all mean Basil?”
medium.com

Let’s get started…


The Crst step is to create yourself a free account if you don’t already have one.
You will require a valid email address and a credit/debit card. Yes, you still need
a card even though you will be only using the “free tier”. Because it’s only free
for the Crst year.

Screenshot by Author

Once you have logged in, the opening console will look something like this.
Amazon AWS is constantly improving and trying new interfaces but as of now
this is what it looks like.

Screenshot by Author

Search for “EC2” for “Elastic Compute Cloud” in the search bar at the top. If you
are unfamiliar with what this is, it is a virtual server in the cloud.

Screenshot by Author

This is what the EC2 console looks like (as of today). Click on “Launch instance”
in orange below.

Screenshot by Author

You will notice I checked “Free tier only” below. It’s not mandatory but will
avoid you selecting paid services by accident. For the purpose of this tutorial I’m
going to spin up an “Ubuntu Server 20.04 LTS (HVM), SSD Volume Type” 64-
bit (x86) instance. Select this one.

Screenshot by Author

You will see the “Free tier eligible” option clearly highlighted in green below.
This is the one we will be using. The “t2.micro” instance which is more than
enough for what we need. One you have selected that one click on “Next:
ConNgure Instance Details”.

Screenshot by Author

You will leave most of the options below default with the exception of “Auto-
assign Public IP”. Make sure you select “Enable”. Please note that in the free tier
you will get a public IP for your server instance but it will change every time you
restart your system. You can assign what is called an Elastic IP but that is at an
additional cost. It’s not necessary for this but it’s worth mentioning.

Another really important part on this page is “User data” under “Advanced
Details”. This allows you to execute a whole series of commands automatically
after the instance is provisioned. This is deCnitely what we’ll want to do as it will
make our lives a whole lot easier here.

Please include the following script “As text” in the “User data” section.

#!/bin/bash

apt-get update -y
apt-get upgrade -y
apt-get install git -y
apt-get install ubuntu-advantage-tools -y

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py


python3 get-pip.py

su - ubuntu -c 'mkdir ~/PyCryptoBot'

# add one line like this for every bot


su - ubuntu -c 'git clone https://github.com/whittlem/pycryptobot
~/PyCryptoBot/BTC-GBP'

# just add this once for the first bot you add E.g. BTC-GBP
su - ubuntu -c 'python3 -m pip install -r ~/PyCryptoBot/BTC-
GBP/requirements.txt'

When you are Cnished, click “Next: Add Storage”.

Screenshot by Author

Nothing to do on this page, just click “Next: Add Tags”.

Screenshot by Author

Adding tags is optional but it’s usually good practice. I usually add “Name” as a
minimum. On my other projects I add a “Project” tag which is really handy when
it comes to billing itemisation. In this case I’m just adding a “Name” of
“PyCryptoBot”.

When you are done, click “Next: ConNgure Security Group”.

Screenshot by Author

You can optionally just accept the defaults here but I personally like to give my
access lists a proper name. I would recommend using “acl-svr-mgmt-in” for both
the “Security group name” and “Description”.

When you are done, click “Review and Launch”.

Screenshot by Author

The summary screen should look like this.

Click “Launch”.

Screenshot by Author

You now need to create your SSH keys to be able to access your system. Take care
here as you can’t change the keys once your system is deployed. If you make a
mistake you will not be able to access your instance and will have to start again.

“Create a new key pair” and give it a name. I called mine “AWS — Michael
Whittle” but you can call it what ever you like. Make sure you “Download Key
Pair”, this is really important and keep it extremely safe. If this falls into the
wrong hands, not only will they have full control of your system but your crypto
currency funds as well. This is no joke! This is your main protection running this
in the cloud so be careful.

In my case the private key that was downloaded is called “AWS-


MichaelWhittle.pem”. Yours should be something similar.

Once you are done, click “Launch Instance”.


Screenshot by Author

This is the example instance I created for this tutorial. You can see that the
“Instance state” is “Running”. You need to copy the “Public IPv4 address”
under “Details”. Not my one, yours. Just to conCrm you are looking in the correct
place below mine is (or was) 18.134.227.169.

Image by Author

The next part will be a little tricky to explain because it depends if you will be
accessing your instance from a Linux, macOS or Windows based system. If you
are using Linux or macOS it is pretty straight forward as you can use the .pem Cle
as is.

Linux and macOS

1. Go to a command line terminal

2. Make a directory called .ssh in your home directory if it doesn’t already exist
(“mkdir ~/.ssh”)

3. Make sure the .ssh directory has only read, write and execute permissions for
you only (“chmod 700 ~/.ssh”)

4. Copy your .pem Cle into your ~/.ssh directory. In my case “AWS-
MichaelWhittle.pem” (“cp AWS-MichaelWhittle.pem ~/.ssh”) — I am
making the assumption you are in the directory where your .pem Cle is
located. It’s likely to be your “Downloads” directory or where ever you saved
it.

5. Make sure the .pem Cle has the correct permissions. It should be read and
write to you only (“chmod 0600 ~/.ssh/AWS-MichaelWhittle.pem”)

6. Go into your .ssh directory (“cd ~/.ssh”)

7. And SSH to your new instance (“ssh -i AWS-MichaelWhittle.pem


[email protected]”) — please note that you will use your own .pem
Cle (not mine), the “ubuntu” user is the default user for Ubuntu systems in
EC2, and the IP address above was my tutorial IP. You need to use your own
Public IP for your instance here. Just as an aside, the default user for a Redhat
system in AWS is “ec2-user”. Not applicable here but worth knowing.

Please note that on your Crst SSH session to the instance it will ask you if you
want to add your source system to the known hosts. Make sure you answer “yes”
to this question.

The authenticity of host '<removed> (<removed>)' can't be established.


ECDSA key fingerprint is SHA256:<removed>.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
yes
Warning: Permanently added '<removed>' (ECDSA) to the list of known
hosts.

A successful login should look something like this…

% ssh -i AWS-MichaelWhittle.pem [email protected]


Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-1045-aws x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

System information as of Mon May 24 11:00:17 UTC 2021

System load: 0.15 Processes: 104


Usage of /: 18.6% of 7.69GB Users logged in: 0
Memory usage: 24% IPv4 address for eth0: 172.31.18.5
Swap usage: 0%

8 updates can be applied immediately.


7 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable

*** System restart required ***

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by


applicable law.

To run a command as administrator (user "root"), use "sudo <command>".


See "man sudo_root" for details.

ubuntu@ip-172-31-18-5:~$

You shouldn’t need to access root but if you do you can do it like this.

ubuntu@ip-172-31-18-5:~$ sudo su -
root@ip-172-31-18-5:~#

And “exit” to go back.

root@ip-172-31-18-5:~# exit
ubuntu@ip-172-31-18-5:~$

Windows

This part is going to be tricky to explain as there are many SSH clients for
Windows. I personally use “PuTTY” and “PuTTYgen” but it’s really up to you. The
problem is for “PuTTY” and I assume for most Windows clients it won’t know
what to do with a .pem Cle. It’s probably going to expect to you create a .ppk Cle
from the .pem Cle.

Please follow this guide by Amazon on how to do it:


https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html

If you don’t have a strong preference just stick with “PuTTY” and follow the
Amazon guide on how to access your system.

Confirming it’s all there…


If all has gone to plan you should have your Amazon AWS EC2 instance up and
running and you have an SSH connection to it. Let’s just check that their Crst or
only bot is ready to go.

ubuntu@ip-172-31-29-62:~$ ls -la
total 36
drwxr-xr-x 6 ubuntu ubuntu 4096 May 24 22:46 .
drwxr-xr-x 3 root root 4096 May 24 22:45 ..
-rw-r--r-- 1 ubuntu ubuntu 220 Feb 25 2020 .bash_logout
-rw-r--r-- 1 ubuntu ubuntu 3771 Feb 25 2020 .bashrc
drwxrwxr-x 3 ubuntu ubuntu 4096 May 24 22:47 .cache
drwx------ 4 ubuntu ubuntu 4096 May 24 22:46 .local
-rw-r--r-- 1 ubuntu ubuntu 807 Feb 25 2020 .profile
drwx------ 2 ubuntu ubuntu 4096 May 24 22:45 .ssh
drwxrwxr-x 3 ubuntu ubuntu 4096 May 24 22:46 PyCryptoBot

ubuntu@ip-172-31-29-62:~$ cd PyCryptoBot

ubuntu@ip-172-31-29-62:~/PyCryptoBot$ ls -la
total 12
drwxrwxr-x 3 ubuntu ubuntu 4096 May 24 22:46 .
drwxr-xr-x 6 ubuntu ubuntu 4096 May 24 22:46 ..
drwxrwxr-x 10 ubuntu ubuntu 4096 May 24 22:46 BTC-GBP

ubuntu@ip-172-31-29-62:~/PyCryptoBot$ cd BTC-GBP

ubuntu@ip-172-31-29-62:~/PyCryptoBot/BTC-GBP$ python3 --version


Python 3.8.5

ubuntu@ip-172-31-29-62:~/PyCryptoBot/BTC-GBP$ python3 -m pip --version


pip 21.1.2 from /usr/local/lib/python3.8/dist-packages/pip (python
3.8)

ubuntu@ip-172-31-29-62:~/PyCryptoBot/BTC-GBP$ python3 pycryptobot.py -


-sim fast
Invalid config.json: [Errno 2] No such file or directory:
'config.json'

For this tutorial I just added one bot for “BTC-GBP” but maybe you added
additional bots or used a completely diEerence market. Just use what ever you
have.

As you can see above the bot is complaining that no conCguration exists yet (i.e.
no conCg.json). You can initialise a conCguration easily like this. This is just an
example but please follow the prompts which will create your “conNg.json” for
you.

ubuntu@ip-172-31-29-62:~/PyCryptoBot/BTC-GBP$ python3 pycryptobot.py -


-init
*** config.json Configuration Builder ***

Do you have API keys for the Coinbase Pro exchange (1=yes,
2=no:default)? 1

What is your Coinbase Pro API Key? 00000000000000000000000000000000


What is your Coinbase Pro API Secret?
0000/0000000000/000000000000000000000000000000000000000000000000000000
0000/00000000000==
What is your Coinbase Pro API Passphrase? 00000000000

What is your Coinbase Pro base currency (what you are buying) E.g.
BTC? BTC
What is your Coinbase Pro quote currency (what you are buying with)
E.g. GBP? GBP

Do you want to smart switch between 1 hour and 15 minute intervals


(1=yes:default, 2=no)? 1

Do you want to start live trading? (1=live, 2=test:default)? 1

Do you have API keys for the Binance exchange (1=yes, 2=no:default)? 2

Do you have a Telegram Token and Client ID (1=yes, 2=no:default)? 2

Do you want to ever sell at a loss even to minimise losses (1:yes,


2=no:default)? 2
Do you want to sell at the next resistance? (1:yes:default, 2=no)? 1
Do you only want to trade in a bull market SMA50 > SMA200? (1:yes,
2=no:default)? 2
Do you want to avoid buying when the price is too high?
(1:yes:default, 2=no)? 1
Do you want to disable the On-Balance Volume (OBV) technical indicator
on buys? (1:yes:default, 2=no)? 2
Do you want to disable the Elder-Ray Index on buys? (1:yes:default,
2=no)? 2
Do you want to disable saving the CSV tracker on buy and sell events?
(1:yes:default, 2=no)? 1
Do you want to disable writing to the log file? (1:yes, 2=no:default)?
1
Do you want the bot to auto restart itself on failure? (1:yes:default,
2=no)? 1

config.json saved!

After you have done this you should be able to run a simulation to conCrm it all
works.

ubuntu@ip-172-31-29-62:~/PyCryptoBot/BTC-GBP$ python3 pycryptobot.py -


-sim fast

It may not be obvious yet but once you have started your bot it will only be
running while you have your SSH connection open. This kind of defeats the
purpose. The way you get around this is to use a really handy application called
“screen”. It will allow you to run your bot(s) and then “detach” allowing you to
close your SSH session and it will keep running. When you log into your system
again you will “re-attach” to your “screen” and you are ready to go again.

Persisting your session with “screen”


You will already have “screen” installed on your Ubuntu system.

ubuntu@ip-172-31-29-62:~/PyCryptoBot$ screen --version


Screen version 4.08.00 (GNU) 05-Feb-20

The way you access screen is to just run “screen” but let’s Crst take a look to see if
there are any screens running (there shouldn’t be if this is a fresh install).

ubuntu@ip-172-31-29-62:~/PyCryptoBot$ screen -ls


No Sockets found in /run/screen/S-ubuntu.

I usually run my screens with “screen -R” which re-attaches to an existing


detached screen or starts a new screen. You can run “screen” on its own but this
will create a new screen each time.

ubuntu@ip-172-31-29-62:~/PyCryptoBot$ screen -R

You may notice once “screen” is loaded that it doesn’t look any diEerent to your
existing terminal.

I’ve created a very basic conCg.json Cle without valid API keys. If I run my bot
now it looks like this.

ubuntu@ip-172-31-29-62:~/PyCryptoBot/BTC-GBP$ git pull; python3


pycryptobot.py
Already up to date.
Exception: GET (401) https://api.pro.coinbase.com/orders?status=done -
Invalid API Key

If the keys were valid the bot would start at this point. Now how do you de-attach
to allow this to keep running even after you log oE. Please “Ctrl+a, then d”. This
will detach from the session and take you back to your original terminal screen.

If you want to conCrm the (Detached) screen is running you can run “screen -ls”.

ubuntu@ip-172-31-29-62:~/PyCryptoBot$ screen -ls


There is a screen on:
17770.pts-0.ip-172-31-29-62 (05/25/21 11:43:38) (Detached)
1 Socket in /run/screen/S-ubuntu.

To re-attach to your screen just run “screen -R” again and you will be back into
your screen. Just out of interest you can also provide the screen ID to attach to a
speciCc screen if you have more than one running and if you break something
you can type “screen -D” to power detach and start over.

So this is great and all for one bot but how do you run multiple bots….

In a screen you can spawn additional screens by typing “Ctrl+a, then n”. It may
be obvious by now but “Ctrl+a” tells screen that your next key will be an action.
“d” to “detach”, “c” for “create screen”, etc.

Once you have your new screen within “screen” you can spawn you next bot. To
toggle back and forward between your screens you can type “Ctrl+a, then n” to
go to the next screen and “Ctrl+a, then p” to go back to the previous screen. And
as I have mentioned to detach just “Ctrl+a, then d”. “screen” is pretty great in
that if you lose connection while in a screen the screen keeps running.

And that’s it really. You now have one or more bots running securely in the cloud.

Thinking forward
For those helpful project contributors if you would like a hobby project what may
be an interesting addition is a secure API to drive the bot. The bot already has
Telegram functionality to keep you updated with what the bot(s) are doing but
maybe an API to start, stop, restart the bot(s) would be pretty handy especially
with a cloud implementation like this.

And finally…
This is my Crst fully open source project and I really have been pleasantly
surprised how helpful the community has been. Loads of developers are now
helping me develop new functionality, improve the bot, Cx issues, and just
support each other. I really appreciate it so thanks :)

Michael Whittle
If you enjoyed this, please follow me on Medium

Interested in collaborating? Let’s connect on LinkedIn

393 4

393 4

Sign up for Coinmonks


By Coinmonks

A newsletter that brings you day's best crypto news, Technical analysis, Discount Offers,
and MEMEs directly in your inbox, by CoinCodeCap.com Take a look.

Your email Get this newsletter

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information
about our privacy practices.

More from Coinmonks Follow

Coinmonks is a non-profit Crypto Educational Publication. Follow us on Twitter


@coinmonks and Our other project —  https://coincodecap.com , Email  — 
[email protected]

Gaurav Agrawal · May 25, 2021

Top 6 Bitcoin Debit Card | Best Bitcoin Credit Cards


in 2021
If you are a new user or plan to buy a new bitcoin card, then you are at the
right place. This article will review the diEerent types of Bitcoin debit car…
compare their features, security, advantages, etc., of these top six Bitcoin
debit cards.
Bitcoin Summary
15 min read (TL;DR) Like traditional Visa / Mastercard…

Share your ideas with millions of readers. Write on Medium

Michael Michailidis · May 25, 2021

Starting with Polkadot Development (Part III)


In the last part of our tutorial, we saw how to create addresses and what
that means for the Polkadot network. We also transferred some funds
through the Polkadot.js user interface and saw this same amount appear…
when we queried the address through our Node.js script. …
Polkadot 5 min read

NoAutoPilot · May 25, 2021

3 Tips That Will Make You Money During Any


Crypto Crash
Do you have the crypto crash blues? Don’t sweat it. The vast majority of
people are probably in the same boat, having seen their crypto portfolios…
decimated during the last few days of the crypto qash crash. I mean, we
watched
Crypto Bitcoin drop from a high of over 45k to…
3 min read

Ben Lilly · May 25, 2021

Crypto’s No Man’s Land


First its bad. Then its worse. And then before you know it, its testing your
very beliefs. This was likely the way many experienced the bitcoin and the
cryptocurrency market last week. First we got what everyone seems to be…
calling the Elon selloE, which was bad. Then as we…
Blockchain 6 min read

Zane Blanton · May 24, 2021

Cryptocurrency Investment for 401k Guys


I’ve become a crazy crypto guy during the COVID-19 lockdown. With
nothing to do or buy and plentiful stimulus checks, one year later my crypto
investment is up x4 (a couple of days ago it was x6). Being a generous…
person, I wanted to spread the Good Word about crypto…
Cryptocurrency 7 min read

Read more from Coinmonks

Recommended from Medium

Ryan Caruso bitdealer

NuCypher Community Update The emergence of a new star in


#11 — Berlin and Beyond the world of cryptocurrency.

Huobi Global in Huobi Group IZUCHUKWU ERNEST

Tutorial: How to Post an VIRGO AND WHAT YOU NEED


Advertisement on the Huobi TO KNOW
Global Mobile App?

岐部 匠 TheBizz in The Social Interest

Analysis of price in September: Explode Your Reach!


BTC, ETH, XRP, BCH, LTC, BNB,
EOS, BSV, XMR, XLM

Elizabeth Udoma Jonald Fyookball

InsureDAO is a peer to pool SPV as Implemented Today is


insurance market protocol on EXACTLY As Described In the
Ethereum. Bitcoin Whitepaper

You might also like