A high-performance, self-hosted trading bot that connects TradingView alerts to Binance Futures via Webhooks. Built for speed, reliability, and security using modern technologies.
- ⚡ Ultra-Low Latency: Built with FastAPI and Uvicorn for millisecond-level order execution.
- � Containerized: Full Docker support for one-command deployment.
- �️ Secure: Robust security practices with environment-based configuration (
.env) and webhook signature validation. - � Database Driven: Uses SQLite and SQLAlchemy for reliable data persistence (Logs, Orders, Alerts).
- � Telegram Integration: Full control via Telegram. Start/stop the bot, check balance, correct position, and view logs remotely.
- � Advanced Trading: Supports Long/Short directions, dynamic leverage, and percentage-based sizing.
- 🔄 Auto-Recovery: Deployment with
docker-composeensures the service automatically restarts on failure.
graph LR
TV[TradingView Alert] -->|Webhook POST| API[FastAPI Webhook]
API -->|Validate & Queue| DB[(SQLite Database)]
DB -->|Process Queue| Trade[Trade Service]
Trade -->|Execute Order| Binance[Binance Futures API]
User[User via Telegram] <-->|Commands & Alerts| Telegram[Telegram Bot]
Telegram <-->|Query Status| DB
Trade -->|Notify| Telegram
- A server (VPS) with Docker and Docker Compose installed.
- A Binance Futures account with API keys (Enable Futures trading).
- A Telegram Bot Token (from @BotFather) and your User ID (from @userinfobot).
- A TradingView account (Pro/Premium recommended for Webhooks).
-
Clone the Repository
git clone https://github.com/beydah/TradingView-Webhook-Bot.git cd TradingView-Webhook-Bot -
Configure Environment Copy the example configuration and edit it with your credentials:
cp .env.example .env nano .env
Security Note: Never commit your
.envfile to version control. It contains sensitive API keys. -
Run with Docker Compose
docker-compose up -d --build
The bot is now running on port
80(or whichever port you defined in.env).
- Install Python 3.10+ and
pip. - Install Dependencies:
pip install -r requirements.txt
- Run the Application:
python main.py
Your .env file controls the bot's behavior. Here are the key settings:
| Variable | Description | Example |
|---|---|---|
BINANCE_API_KEY |
Your Binance API Key | vmPUZE6mv... |
BINANCE_SECRET_KEY |
Your Binance Secret Key | NhqPtmdSJ... |
TELEGRAM_BOT_TOKEN |
Token from BotFather | 123456:ABC-DEF... |
TELEGRAM_USER_ID |
Your numeric Telegram ID | 123456789 |
ALERT_KEY |
Secret key for Webhook validation | my_secret_password |
WEBHOOK_PORT |
Port to listen on (Internal) | 80 |
-
Create an Alert on TradingView.
-
In the Webhook URL field, enter:
http://YOUR_SERVER_IP/webhook -
In the Message field, use the following JSON format:
Open Long:
{ "symbol": "BTCUSDT", "alert": "long_open", "price": {{close}}, "key": "YOUR_ALERT_KEY" }Close Long:
{ "symbol": "BTCUSDT", "alert": "long_close", "price": {{close}}, "key": "YOUR_ALERT_KEY" }(Replace
YOUR_ALERT_KEYwith theALERT_KEYdefined in your.envfile)
Interact with your bot directly from Telegram:
/start- Initialize the bot./help- Show available commands./getpos- Show current open positions on Binance./getwallet- Show current wallet balance and exposure./getalert- Show the last 20 received alerts./getlog- Download the latest system logs as a text file.
We use unittest for testing.
Run all tests:
python -m unittest discover testsProject Structure:
.
├── app/
│ ├── api/ # API Routers (FastAPI)
│ ├── core/ # Config, DB, Logging
│ ├── models/ # SQLAlchemy Models
│ └── services/ # Business Logic (Binance, Trade, Telegram)
├── data/ # SQLite DB Storage
├── tests/ # Unit Tests
├── main.py # Entry Point
└── Dockerfile # Container Definition
Q: Does this work on Binance Spot? A: No, this bot is specifically designed for Binance Futures.
Q: Can I run multiple bots?
A: Yes, but you should run them in separate containers or change the WEBHOOK_PORT to avoid conflicts.
Q: My logs show "Invalid API Key"?
A: Ensure your ALERT_KEY in the TradingView JSON message matches the ALERT_KEY in your .env file.
Contributions are welcome! Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
Disclaimer: Trading cryptocurrencies involves significant risk. The authors of this software are not responsible for any financial losses incurred while using this bot. Use at your own risk.