Building an AI-Powered Trading Bot on AWS Ubuntu Server: A Step-by-Step Guide
Introduction
In this tutorial, we will guide you through the process of “How to setup freqtrade AI crypto trading bot” on an Ubuntu virtual server hosted on AWS, you can even setup on your localhost also. From securing your server to configuring the trading bot software, this comprehensive guide covers every step needed to get your trading bot up and running.
Warning
Before we begin, it’s crucial to understand the risks associated with automated trading. Make sure to use proper risk management strategies and thoroughly test your trading algorithms in a safe environment before deploying them in a live market.
PART 1: Creating and Securing a Virtual Cloud Server
1.1 Logging in on the Server and Service Management(Skip below if you are using local machine/computer)
To start, we’ll log in to the server and perform essential service management tasks.
Log in to the server
# ssh -i "your-key.pem" ubuntu@your-server-ip
Check which services are running
# service --status-all
Check load, resources & running processes
# top
Changing the server hostname
# cp /etc/hostname{,.org}
# echo "gtbot1" > /etc/hostname
#Reboot the server
1.2 Add Normal User and Disable Root
Create a new user and disable the root user for enhanced security.
Add a new user
# sudo adduser gotraddy
Give the new user sudo permission
# sudo usermod -a -G sudo gotraddy
Disable root login
# sudo usermod -L root
Enable root login
# sudo usermod -L root
To verify if the root user has been disabled
# sudo cat /etc/shadow |grep -i root
(there should a "!" mark in front)
1.3 Configuring the SSH Server with Keypair
Enhance SSH security by configuring key-based authentication.
Now generate a new key pair & download .pem file to your local machine
Then on your local machine run below command and provide the path of downloaded .pem file from AWS, refer youtube video for reference and then copy the extracted public key
# ssh-keygen -y
Now as we already have created the user "gotraddy" in step 1.2, so login with the new user & run below command
# sudo su - gotraddy
# mkdir .ssh
# chmod 700 .ssh
# touch .ssh/authorized_keys
# chmod 600 .ssh/authorized_keys
# cat >> .ssh/authorized_keys
# Now paste the copied commands from the your local machine to here and hit enter and ctrl+d
# Try to login to new user from your local machine
ssh -i "awskey.pem" gotraddy@your-aws-server-ip
1.4 Server hardening with SSHD_CONFIG file
Now we will make few parameter changes in “/etc/sshd/sshd_config” file
Basically we will achieve below steps
- Prevent root login.
- Restrict server access to the specified user only.
- Prohibit the use of password-based authentication.
- Set a limit on the number of authentication retries.
- Utilize exclusively the recently generated keyfile.
- Consider employing a less common port for the SSH service.
- Turn off X11 and TCP forwarding, if currently enabled.
- Opt for SSH protocol version 2.
# sudo cp /etc/ssh/sshd_config{,.orig}
# vim /etc/ssh/sshd_config
Include /etc/ssh/sshd_config.d/*.conf
Protocol 2
Port 1091
AddressFamily inet
PermitRootLogin no
AllowUsers gotraddy
PasswordAuthentication no
PermitEmptyPasswords no
KbdInteractiveAuthentication no
UsePAM no
X11Forwarding no
AllowTcpForwarding no
PrintMotd no
AcceptEnv LANG LC_*
MaxAuthTries 3
Banner /etc/motd
1.5 Install and Configure a Firewall
Set up a firewall to control incoming and outgoing traffic.
Install UFW
# sudo ufw status numbered
If no output then install otherwise you can skip next 2 commands
# sudo apt-get update
# sudo apt-get install ufw
First add/configure SSH port before activating the firewall
# sudo ufw allow 1091/tcp
# sudo ufw limit 1091/tcp
# sudo ufw logging medium
# sudo ufw status numbered
Configure UFW(Uncomplicated Firewall)
# sudo ufw enable
# sudo ufw status numbered
Restart the SSH service
# sudo systemctl restart sshd
# sudo systemctl status sshd
To verify port listening on 1091
# sudo ss -atpu
1.6 Enabling Automatic Updates
Keep your system up-to-date automatically.
Install unattended-upgrades
# sudo apt-get install unattended-upgrades
1.7 Configuring Time Server
Ensure accurate time synchronization on your server.
Install NTP (Network Time Protocol) (Run if NTP is not installed)
# sudo apt-get install ntp
# sudo timedatectl list-timezones |grep -i singapore
# sudo timedatectl set-timezone Asia/Singapore
# sudo apt install systemd-timesyncd
# sudo systemctl status systemd-timesyncd.service
# sudo timedatectl timesync-status
# sudo cp /etc/systemd/timesyncd.conf{,.org}
# sudo vim /etc/systemd/timesyncd.conf
PART 2: Installing Trading Bot Software
2.1 Installing Docker and Docker Compose
Set up Docker and Docker Compose for easy deployment.
Install Docker and Docker Compose
# sudo apt-get install docker docker-compose
2.2 Downloading Freqtrade Docker Image and Creating File Structure
Pull the Freqtrade Docker image and create necessary file structures.
# mkdir /var/www/ft_userdata
# cd /var/www/ft_userdata
Download the docker-compose file from the repository
# curl https://raw.githubusercontent.com/freqtrade/freqtrade/stable/docker-compose.yml -o docker-compose.yml
Pull the freqtrade image
# docker compose pull
Create user directory structure
# docker-compose run –rm freqtrade create-userdir –userdir user_data
Create configuration – Requires answering interactive questions
# docker-compose run –rm freqtrade new-config –config user_data/config.json
2.3 Checking the Docker Container with Multiple Management Commands
Explore Docker container management commands.
Check container status
# docker ps -a
Start/Stop container
# docker start/stop container-name
2.4 Updating the Docker Bot Image File
Keep your Freqtrade Docker image up-to-date.
Update Docker image
# docker pull freqtradeorg/freqtrade:latest
PART 3: Trading Bot Configuration
3.1 Changing the Config for Spot or Futures Trading
Modify Freqtrade configurations based on your trading preferences.
Edit Freqtrade config file
# vi user_data/config.json
3.2 Other Configuration Considerations
Explore additional configuration options.
Edit other config files
# vi user_data/your-custom-config-file.json
3.3 Using a Separate Private Config File
Enhance security by using a separate private config file.
Create and edit private config file
# vi user_data/private-config.json
3.4 Testing Docker command
Starting docker container in normal and detached mode
# sudo docker-compose up
# sudo docker-compose up -d
3.5 Enable the Bot Web Interface
Activate the Freqtrade web interface for convenient monitoring.
I have explained bot web interface in video kindly refer to that
# Make sure in "docker-compose.yml", you have added below line
ports:
- "127.0.0.1:8080:8080"
# Add/modify below line in "config.json" file
"api_server": {
"enabled": false,
"listen_ip_address": "127.0.0.1",
"listen_port": 8080,
"username": "",
"password": ""
3.6 Enable and Use Telegram for Signaling and Management
Integrate Telegram for notifications and management.
I have explained telegram interface in video kindly refer to that
Edit config.json and add/modify below line
# vim user_data/config.json
"telegram": {
"enabled": false,
"token": "",
"chat_id": ""
PART 4: Downloading Data, Backtest Past Performance, Hyperoptimizing the Bot, and Visualizing Results
Explore the process of preparing data, backtesting, optimizing, and plotting results.
Run Freqtrade commands
#cd user_data
#vim config_json
Add "BTC/USDT" in pair whitelist
Volume pairlist to static pairlist
"heartbeat_interval: 5"
#sudo docker-compose run --rm freqtrade download-data --config user_data/config.json --days 30 -t 5m
#sudo docker-compose --rm freqtrade backtesting --config user_data/config.json --strategy SampleStrategy
#sudo docker-compose run --rm freqtrade hyperopt --hyperopt-loss SharpeHyperOptLossDaily --spaces roi stoploss trailing --strategy SampleStrategy --config user_data/config.json -e 10
PART 5: Introduction to Developing Your Own Trading Strategy
Dive into the basics of developing trading strategies.
# Explore strategy development
#sudo vim user_data/strategies/your-strategy-file.py
PART 6: Final Words
Congratulations! You’ve successfully set up and configured an AI-powered trading bot on your Ubuntu server hosted on AWS. Remember to continuously monitor and refine your strategies for optimal performance.