This project is a sophisticated, automated trading bot that connects TradingView alerts to the Binance Futures platform. It operates via webhooks, allowing for instant, automated trade execution based on your custom TradingView strategies. The bot is managed and monitored through a Telegram interface, providing a seamless user experience.
The primary goal of this bot is to bridge the gap between TradingView's powerful charting and alert system and Binance's high-liquidity futures market. By listening for webhook signals from your TradingView alerts, the bot can execute market orders (long or short, open or close) on your behalf. It includes a robust backend service layer for interacting with Binance, a data model for managing settings, orders, and logs, and a Telegram bot interface for command, control, and real-time notifications.
- TradingView Integration: Receives and processes webhook alerts from TradingView for automated strategy execution.
- Binance Futures: Connects to Binance Futures (or Testnet) to open and close positions.
- Telegram Control: A fully functional Telegram bot interface to start, monitor, and control the bot's operations.
- Risk Management: Includes logic for calculating order quantities based on a percentage of your balance and respects Binance's trading limits (min/max quantity, step size, etc.).
- Persistent Storage: Uses JSON files to manage settings, logs, alerts, and order history.
- Secure and Self-Hosted: You have full control over the bot by hosting it on your own server.
You can download or clone the project from the official GitHub repository.
git clone https://github.com/beydah/TradingView-Webhook-Bot.git
cd TradingView-Webhook-BotSetting up this bot requires a server that is publicly accessible on the internet to receive webhooks from TradingView. A Virtual Private Server (VPS) is the recommended solution.
- Choose a Provider: Select a VPS provider such as DigitalOcean, Linode, Vultr, or AWS EC2.
- Select an OS: A Linux distribution like Ubuntu 22.04 LTS is recommended.
- Server Size: A basic, low-cost server is usually sufficient to run the bot.
- Access Your Server: Connect to your server via SSH using the provided credentials.
ssh root@YOUR_SERVER_IP
- Install Python and venv:
sudo apt update sudo apt install python3-pip python3-venv
- Create a Project Directory: Clone the repository (as shown in the download section) or upload the project files to a directory (e.g.,
/var/www/trading-bot). - Create the Virtual Environment:
cd /path/to/your/project python3 -m venv venv - Activate the Environment:
Your terminal prompt should now be prefixed with
source venv/bin/activate(venv).
- Purchase a Domain: Buy a domain name from a registrar like Namecheap or GoDaddy.
- Configure DNS: In your domain registrar's DNS management panel, create an A Record that points your domain (or a subdomain like
bot.yourdomain.com) to your server's public IP address.
Nginx will act as a reverse proxy, forwarding incoming webhook requests to the bot's web server.
- Install Nginx:
sudo apt install nginx
- Create an Nginx Configuration File:
sudo nano /etc/nginx/sites-available/tradingbot
- Paste the Following Configuration: Replace
your_domain.comwith your actual domain.server { listen 80; server_name your_domain.com; location / { proxy_pass http://127.0.0.1:5001; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
- Enable the Configuration:
sudo ln -s /etc/nginx/sites-available/tradingbot /etc/nginx/sites-enabled/ sudo nginx -t # Test for syntax errors sudo systemctl restart nginx
- Install Certbot:
sudo apt install certbot python3-certbot-nginx
- Obtain the SSL Certificate: Certbot will automatically detect your domain from the Nginx config, obtain a certificate, and configure Nginx for HTTPS.
Follow the on-screen prompts. Choose to redirect HTTP traffic to HTTPS.
sudo certbot --nginx -d your_domain.com
- Verify Auto-Renewal: Certbot automatically sets up a cron job for renewal. You can test it with:
sudo certbot renew --dry-run
- Telegram:
- Talk to
@BotFatheron Telegram to create a new bot. Save the Bot Token. - Talk to
@userinfobotto get your personal Telegram User ID.
- Talk to
- Binance:
- For testing, create API keys on the Binance Testnet.
- For live trading, create API keys from your main Binance account under API Management. Ensure you enable Futures trading permissions.
- IMPORTANT: For security, restrict API key access to your server's IP address only.
- Fill in Settings: The bot creates a settings file at
e_database/db_a_settings.jsonon its first run. You need to manually populate this file with all the credentials you obtained. - Install Dependencies:
pip install -r requirements.txt
- Run the Bot:
To keep the bot running permanently, use a process manager like
python3 main.py
systemdorsupervisor.
The project is organized into a service layer, a data model layer, and a database layer.
-
main.py: The main entry point of the application. It initializes and starts the TradingView and Telegram service threads. -
c_service/(Service Layer)sr_a_trade.py: Contains the core business logic for executing trades. It calculates quantities and leverage, validates them against Binance rules, and handles the process of opening/closing positions.sr_b_tradingview.py: Runs a Flask web server (using Waitress) to listen for incoming webhook calls from TradingView. It validates, parses, and queues the alerts.sr_c_binance.py: A dedicated service module for all interactions with the Binance Futures API (fetching wallet info, symbol data, market prices, placing orders, etc.).sr_d_transaction.py: Manages user state and processes commands received from the Telegram bot.sr_e_telegram.py: A service module for sending messages, buttons, and documents via the Telegram Bot API.
-
d_model/(Data Model Layer)md_a_settings.py: Manages the application's configuration, such as API keys and webhook settings.md_c_alerts.py: Manages the queue of incoming trading alerts from TradingView.md_d_orders.py: Manages the history of opened and closed orders.md_e_logs.py: Manages the logging of events, errors, and transactions.
-
e_database/- This directory stores the JSON files that act as a simple database for settings, alerts, orders, and logs.
Contributions are welcome! If you'd like to contribute to the project, please follow these steps:
- Fork the Repository: Create your own fork of the project.
- Create a Branch: Make your changes in a dedicated branch.
git checkout -b my-new-feature
- Commit Your Changes: Write clear and concise commit messages.
git commit -am 'Add some amazing feature' - Push to the Branch:
git push origin my-new-feature
- Open a Pull Request: Submit a pull request from your fork to the main repository. Please provide a detailed description of the changes you've made.
If you find a bug or have a feature request, please open an issue on the GitHub repository page.