Setting up vpn on lightsail for receiving tradingview signals and trade automatically on Binance

So to give you a bit of background why I decided to write this post.

Few years ago I used tradingview webhook signals that my api hosted on aws as simple lambda. Setup was quite easy. TradingView -> ProxyApi -> Binance.

I wrote a very simple trading strategy on 5M timeframe BTC/USDT pair that uses Stochastic algorithm 9 3 3 and 60 1 1, if both Stoch are below 20 we buy if above 80 we sell (no shorts, just long positions). This strategy using backtesting on tradingview gave me satisfying results.

Zoom image will be displayed

The above results are around 1 month time frame. Jun 30–5 Aug 2025

Capital per trade 5000 usdt.

Please note this is not any financial advice, this entry is purely about technology to automate the trades.

Unfortunately one day it stopped working, as Binance api introduced IP restrictions.

I had few options.

  1. Purchase VM on azure or aws — costs ~ 200USD/month that would give me ability to assign static IP and make sure it works 24/7 as my trades were executed when I was asleep during Asia and Australia working hours
  2. Run my own computer 24/7 — that would requires more maintenance from me and costs of electricity also wouldn’t help to keep this infrastrcture under reasonable budget
  3. Use aws Lightsail — try with the cheapest possible option 5$/month, first 90 days to test out for free

I decided to give option 3 a go.

Architecture diagram of the solution

Zoom image will be displayed

So I already had my python script that I turned into webhook to receive messages from my proxy api.

On the lightsail I had to attach static Ip to my instance and also enable Firewall for TCP port 8000 and 5000—that would be the port that I used in my python flask and also later on Gunicorn (on that later)

First we need to create virtual environment for python so we can use pip installation without issues.

sudo apt update sudo apt 
install python3-venv -y
python3 -m venv webhook-env

Let’s activate virtual environment

source webhook-env/bin/activate

Now we can install necessary packages for my python script to run on new vpn
flask for webhook
ccxt for binance and other exchanges if you want to use different one, should be supported

pip install flask
pip install ccxt

Upload the python into our vpn

Access the server via ftp (FileZilla for instance) and upload trading script python simple-trading-stoch-webhook.py

Now we want to keep it running 24/7 so let’s run the script using screen

sudo apt install screen -y
screen -S webhook
source webhook-env/bin/activate
python simple-trading-stoch-webhook.py

Now I could finish there, but the problem seems to appear when aws restarts lightsail and then I have to manually run python command to activate my webhook python script.

So my next goal was to make sure my webhook runs 24/7 and survives automatic restarts, services reboots. Using Gunicorn I manage to make it happen.

So I had to remove this line of code from python so it won’t run in developer mode anymore. You can now remove port 5000 from Firewall settings on lightsail as we won’t need it anymore.

if name == "main":
app.run(host='0.0.0.0', port=5000)

Then I had to create new service inside systemd folder

I used sudo nano to create file on the server but you can just transfer the file using filezila or any other ftp client.

sudo nano /etc/systemd/system/webhook.service

File content

[Unit] Description=Gunicorn service for Flask Webhook
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu
Environment="PATH=/home/ubuntu/webhook-env/bin"
ExecStart=/home/ubuntu/webhook-env/bin/gunicorn -w 3-b 0.0.0.0:8000 simple-trading-stoch-webhook:app
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Depending on which instance of vm you are using, server might run out of memory when running 3 workers so simply reduce to 2 or 1.

I had issues sporadically with 3 workers so I reduced to 1.

ExecStart=/home/ubuntu/webhook-env/bin/gunicorn -w 1 -b 0.0.0.0:8000 simple-trading-stoch-webhook:app

Now let’s enable the service

sudo systemctl daemon-reload
sudo systemctl start webhook
sudo systemctl enable webhook

Let’s check if it’s running

sudo systemctl status webhook

Once everything is correctly configured we should be able to see the service running on our lightsail instance.

Zoom image will be displayed

Now we can curl our lightsail directly to test if orders are placed on

curl --location 'http://your-static-ip:8000/webhook' \
--header 'Content-Type: application/json' \
--data '{
"message": "buy"
}'

Now you might be wondering, why I had to create proxy api to call lightsail and not tradingview calling my lightsail instance directly?

Answer is very simple, tradingview only allow port 80.

Optional - Setup cron job to reboot the server

Login to your lightsail instance, type crontab -e and type 1 3 * * * /sbin/reboot

Save and close with those commands: ctrl + O Enter and ctrl + X

That cron syntax will reboot server every day at 3:01AM  

Comments