TradingView Pine Script to IBKR: Automate Your Alerts with Webhooks
TradingView is one of the best charting platforms available, but it cannot execute trades directly on Interactive Brokers. In this guide we show you how to connect TradingView alerts to IBKR using webhooks and a Python bridge server.
## How It Works
1. TradingView strategy fires an alert
2. Alert sends a webhook POST request to your server
3. Your Python server receives the signal
4. Python places the order on IBKR via ib_insync
## Step 1 — Pine Script Strategy with Alert
strategy('EMA Cross', overlay=true)
fast = ta.ema(close, 10)
slow = ta.ema(close, 50)
if ta.crossover(fast, slow)
strategy.entry('Buy', strategy.long)
alert('{"action":"buy","symbol":"AAPL","qty":10}', alert.freq_once_per_bar_close)
## Step 2 — Python Webhook Server
from flask import Flask, request
from ib_insync import IB, Stock, MarketOrder
import json
app = Flask(__name__)
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)
@app.route('/webhook', methods=['POST'])
def webhook():
data = json.loads(request.data)
contract = Stock(data['symbol'], 'SMART', 'USD')
ib.qualifyContracts(contract)
order = MarketOrder(data['action'].upper(), data['qty'])
ib.placeOrder(contract, order)
return 'OK', 200
app.run(port=5000)
## Step 3 — Set Up the TradingView Alert
In TradingView, create an alert on your strategy and set the webhook URL to your server. The alert message should be the JSON payload your server expects.
## Deploy on a VPS
For 24/7 automation deploy your Python server on a VPS so trades execute even when your computer is off.
## Need This Built?
Team NAK builds TradingView to IBKR webhook bridges as a done-for-you service. We also support MT5, Binance, and Bybit. Contact us at theteamnak.com.