Trading bot that exposes webhooks meant to be used to connect TradingView alerts with Interactive Brokers, providing the ability to trigger round-the-clock stop loss strategies.
This bot is only configured to trade on SMART eligible exchanges in USD. Extend the bot if you wish to trade other exchanges, currencies, or specialized instruments (like mutual funds).
Each trade/account interaction/error with the webhooks/IBKR will result in a Telegram notification.
Webhooks:
- Fetch current holdings and send the data via Telegram to a user (smoke test for IBKR Trading API).
- Create a sell market order for a specific share at a specific quantity.
- Ensure that you have a paid version of TradingView that supports webhooks. You may replace TradingView with another price alert service that can send HTTP POST requests.
- Ensure that you have an active and funded IBKR Pro account. API access must be enabled in Trader Workstation (TWS) or IB Gateway.
- For best results, ensure that you are subscribed to some form of real-time US market data, either via TradingView or via broker integration.
- In TWS, configure API access to 'enable ActiveX and Socket Clients', as well as set the correct port number.
- Setup a telegram bot via BotFather. Retrieve the bot token, start a chat, and retrieve the chat id.
- Setup the application
Requirements: Python 3.9 or later, pip, venv, and OpenSSL (optional)
- Clone repository
git clone https://github.com/ryo-wijaya/ibkr-tv-tradebot
cd ibkr-tv-tradebot
- Create and activate virtual environment
python -m venv env
.\env\Scripts\activate (windows) or source ./env/bin/activate (Linux/Mac)
- Install dependencies
pip install -r requirements.txt
- Define or export environment variables
TELEGRAM_BOT_TOKEN=your-telegram-bot-token
TELEGRAM_CHAT_ID=your-chat-id
IBKR_HOST=127.0.0.1
IBKR_PORT=7497
CLIENT_ID=1001
WEBHOOK_SECRET=your-secure-webhook-secret
- Start the application
uvicorn main:app --reload --port 8080
- Ensure the bot is up, running, and configured correctly, whether locally or deployed. For testing, its best to try out with a paper account first.
- In TradingView (or any other alternative), create a price alert for a stock you own.
- [Local] Perform tests for each price alert in the Webhook section:
- For a connection smoke test, set up the webhook:
http://localhost:8000/webhook/fetch-portfolio
with payload:You should receive a Telegram message containing your positions. This is a sanity tests for the IBKR API connection.{ "key": "your-webhook-secret" }
- For a live sell market order test, set up the webhook:
http://localhost:8000/webhook/sell-market-order
with payload:You should receive a Telegram message indicating the close and success status.{ "key": "your-webhook-secret", "symbol": "{{ticker}}", "quantity": 1 }
- For a connection smoke test, set up the webhook:
If deploying to a server or cloud provider (e.g., GCP, AWS):
- Update the webhook URL in TradingView to point to your publicly accessible endpoint.
- Use a process manager (e.g., gunicorn, pm2) to run the bot in production mode.
- Keep the WEBHOOK_SECRET and Telegram bot token confidential.
- Use HTTPS for production deployments to secure webhook requests.
- Periodically rotate sensitive credentials (e.g., WEBHOOK_SECRET, TELEGRAM_BOT_TOKEN).
- Activate the virtual environment (for windows)
.\env\Scripts\activate
- Run a file from root
python -m <sub-folder-name>.<file-name>
- Start a local dev server
uvicorn main:app --reload --port 8080
- Generate a new 32 byte hexadecimal string (64 characters) to use as webhook secret
openssl rand -hex 32