Setting up vpn on lightsail for receiving tradingview signals and trade automatically on Binance
on
Get link
Facebook
X
Pinterest
Email
Other Apps
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.
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
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
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.
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
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
Comments
Post a Comment