How to build an Automatic Cryptocurrency Portfolio Rebalancing Tool (3/3)

Use AWS(Amazon Web Services) EC2 to run python script 24/7.

Chris Kang
Coinmonks
Published in
6 min readJul 29, 2021

--

Image by author

This tutorial will not only help our project but also could help with many other projects that you might be interested in. It will allow you to run python script on a cloud computer, which means this computer will never turn off (unless the Amazon server goes down).

Now it is time for us to end our journey, and let the program trade according to our set rule.

STEP 1: Create an Amazon Web Services account.

STEP 2: Search EC2 in the services search bar at the top of the website.

STEP 3: Click launch instance and configure settings.

We will use Ubuntu Server 20.04 LTS (HVM), SSD Volume Type 64-bit(x86), which is eligible for the free tier.

Image by author
Image by Author

Next, you need to choose the type of your computer. We will use t2.micro because it is free, and our script is not computationally heavy. Therefore, we don’t need fancy CPUs and memory. Then click next.

Image by author

You can ignore the rest of the settings and click review and launch.

When you launch, you will encounter a message about the key pair. Click the drop-down menu and click Create a new key pair and type any name you like. Then you will be downloading a .pem file, which is important. Store the file safely in a separate directory.

Image by author

STEP 4: Access your cloud computer and move the codes from the local computer to the cloud computer

Give AWS some time to finish initializing your instance. Then right click your instance and click Connect.

Image by author

Open your terminal and use the cd command to locate the folder that contains the key file. If you are using a mac, you can simply drag and drop the folder to the terminal application in the dock. Then copy and paste #3 in the instruction into the terminal. Then do the same for Example. (I have put a box around the important commands. Also, the name of the .pem file will be different from mine). The terminal will ask you whether you want to continue although the authenticity of the host can’t be established. Type yes.

Image by author

Once you follow all the steps above, you will see the image below. Then you are in your cloud computer!

Image by author

STEP 5: Transfer all the necessary files to your cloud computer.

We will use FileZilla to do this step. Once you have FileZilla set up, it is so simple to manage your files on your cloud computer. Download FileZilla from the link below.

Once you’ve downloaded it, you will be on this screen.

Image by author

First, locate the folder that contains our python scripts. Then we have to add the key file to FileZilla by following steps (FileZilla needs the key file to access your cloud computer).

Settings → Connection → SFTP → Add key file → OK (make sure you click ok)

Now we need to connect FileZilla to our cloud computer. Click the icon at the top left corner, then you need to type in some of the configurations.

Protocol: SFTP

Host: Public IPv4 DNS of your instance → you can find this out by clicking your instance in the Instances menu in AWS.

User: ubuntu

Image by author

When you click connect, you will see this:

Image by author

Then you can simply drag our scripts to the left! (You should make a folder in your cloud computer first)

STEP 6: Run the python script on your cloud computer.

Connect to your cloud computer again in the terminal. Then follow the steps below:

a) Download necessary packages on your cloud computer by the following command. (run line by line in order)

sudo apt update
sudo apt install python3-pip
pip install pandas
pip install python-binance
pip install python-telegram-bot
pip install dataframe-image
pip install lxml

b) type ls → to see the folder you have created, which now contains all the necessary python scripts for our bot.

ls → terminal command to see what files are there in a directory

c) type cd XXXX → XXXX is the name of the folder

cd ____ → terminal command to access certain directory

Below is the example.

ubuntu@ip-172-31-5-28:~$ ls
Demo
ubuntu@ip-172-31-5-28:~$ cd Demo
ubuntu@ip-172-31-5-28:~/Demo$

d) type the following to run your script in the background (replace the brackets with the name of your telegram bot python script). This command allows you to exit the computer while running your script 24/7.

nohup python3 (TELEGRAM SCRIPT.py) > output.log &

Now you can exit your computer simply by typing:

exit

You're done!

Just a few additional commands

  • Command to see if your script is running in the background.
ps ax | grep .py
  • Command to kill your script. (PID refers to the number on the left when you run the above command)
kill -9 PID

CONGRATULATIONS! You have now deployed your code on a cloud server! Now you can use your telegram bot without turning your computer on 24/7! Enjoy!

source: https://giphy.com/gifs/olympics-shaun-the-sheep-aardman-xT8qBepJQzUjXpeWU8/links

Final Comments

The goal of this tutorial series was to make an automatic crypto portfolio rebalancing tool. Now you can use telegram to monitor your portfolio, and the telegram bot will rebalance it for you if your pre-set condition is met.

There is no right answer for how you want to allocate your assets. Therefore, you should do your own research. You should study which coin you want to invest in and how much weight you are going to allocate for that crypto. But, I believe portfolio rebalancing is a good long-term investment strategy in general because it lets you buy low sell high.

Thank you very much for reading this series, and I hope this tutorial has made your life easier. If you have any questions or problems with anything, please don’t hesitate to contact me in the response section.

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also Read

--

--

Chris Kang
Coinmonks

Crypto Enthusiast | Quant Trading | Solidity | DeFi | Trading System | Blockchain