TradingView Webhook Bot
This project is a powerful Python‑based trading bot that bridges TradingView alerts and Binance Futures. It listens for webhook signals generated by your custom PineScript strategies and instantly executes trades with risk‑managed position sizing all hosted by you for full control.
Install / Use
npx skills add beydah/TradingView-Webhook-BotInstalls into whichever agent you are using.
README
🚀 TradingView Webhook Bot for Binance
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.
🌟 Features
- ⚡ 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.
🏗️ Architecture
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
🚀 Getting Started
Prerequisites
- 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).
� Installation (Docker) - Recommended
-
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 .envSecurity Note: Never commit your
.envfile to version control. It contains sensitive API keys. -
Run with Docker Compose
docker-compose up -d --buildThe bot is now running on port
80(or whichever port you defined in.env).
🐍 Installation (Manual)
- Install Python 3.10+ and
pip. - Install Dependencies:
pip install -r requirements.txt - Run the Application:
python main.py
⚙️ Configuration
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 |
🔗 TradingView Setup
-
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)
📱 Telegram Commands
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.
🧪 Development & Testing
We use unittest for testing.
Run all tests:
python -m unittest discover tests
Project 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
❓ FAQ
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.
🤝 Contributing
Contributions are welcome! Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests.
📄 License
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.
Related Skills
node-connect
385.5kDiagnose OpenClaw Android, iOS, or macOS node pairing, QR/setup code, route, auth, and connection failures.
blender-python-addon
40.5kBlender Python add-on rules for operators, panels, properties, registration, testing, and API-safe scripting
flutter-development-guidelines-cursorrules-prompt-file
40.5kCursor rules for Flutter development with MVVM architecture, Riverpod state management, Material widgets, and Dart style guidelines.
commit-push-pr
140.7kCommit, push, and open a PR
