How to Automate a TradingView Strategy — Safely
TradingView has no public trading API, but it has something almost as good: alerts with webhooks. When a strategy or indicator condition fires, TradingView sends a JSON message to a URL you choose. Point that URL at a bridge service connected to your broker, and your chart strategy places real orders. Here is the pipeline, and the safety rails it needs.
The pipeline, end to end
- The strategy is Pine Script. Your rules live in a TradingView strategy script that computes entries and exits on each bar close.
- Alerts carry the orders. The script's alert messages embed what to do — instrument, direction, stop loss, take profit — as JSON.
- A webhook bridge receives them. The bridge authenticates the message (a per-user token and secret, since TradingView cannot sign requests), validates it, applies risk checks, and translates it into a broker API order.
- The broker executes. A market order with the stop and target attached server-side, so protection exists even if the bridge goes offline afterward.
- Reconciliation watches. A loop compares broker state against expected state and logs every fill, rejection, and stop-out — the audit trail you will want the first time something looks odd.
The failure modes to respect
- The script you automated is not the strategy you tested. The commonest and costliest failure. A repainting indicator, a different data feed, or an intrabar assumption means the live script's trades never matched your backtest in the first place. The fix is trade-level verification before going live — run both engines over the same period and compare every trade (how and why engines disagree).
- Position-state drift. If an alert is missed (platform hiccup, expired alert) the bridge's idea of your position diverges from the broker's. Reconciliation and fail-closed checks — refuse to trade when the position cannot be read — are not optional.
- Sizing surprises. Risk-based sizing needs the stop distance at the moment of entry. Bake the stop into the alert payload so the bridge sizes from the same number the strategy used.
- Weekend and overnight holds. Decide in the strategy — not in a 2 a.m. panic — whether positions flatten before rollover and weekends.
Practice first, verified first
Run the whole pipeline against a demo account until the live statistics have enough trades to compare against the backtest's expectation. ForexEdge implements this pipeline for forex with the verification built in: strategies built and backtested in the app export as Pine Script, earn a parity badge when TradingView's tester matches trade for trade, and then trade through the Live Bridge to your own OANDA practice account — with every fill, cost, and the backtest-vs-tester-vs-live win rates tracked on one page.
ForexEdge backtests forex strategies without code, verifies them trade-for-trade against TradingView, and tracks live results against the tested expectation.
Try ForexEdge free