SuperTrend strategy — Step-by-step with TradeAdapter
Full tutorial: Pine Script strategy, TradingView alert, and execution via MetaTrader or Direct API (e.g. Binance).
Educational purpose
This page shows how to automate a trading strategy on TradingView with TradeAdapter. The goal is to demonstrate the end-to-end flow (strategy → alert → broker). The example is not a recommendation for profitability; use demo accounts first and adapt the strategy to your own risk and rules.
On this page #
What you’ll do #
-
Create a SuperTrend strategy in Pine Script
Paste the code in TradingView’s Pine Editor, adjust inputs (ATR length, factor, order size), and add it to the chart.
-
Create one alert with TradeAdapter’s message
Condition: your strategy + Order fills only. Notifications: your TradeAdapter webhook. Message: the JSON template (MetaTrader or Direct API).
-
Trigger and verify
When the strategy fires, the alert sends the order to TradeAdapter. Check MyTrades and your MT terminal or exchange.
Before you start #
Choose one path and complete its setup first:
EA installed and attached to a chart, API key in the EA, Algo Trading and WebRequests enabled. See MetaTrader overview and MT4 Setup or MT5 Setup.
API keys created on the exchange and added to the alert message (or Message Generator). See Direct API overview and e.g. Binance guide.
You also need a TradingView plan with webhooks (Essential or higher).
1 — Create the SuperTrend strategy in Pine Script #
The SuperTrend is a trend-following method: it flips long/short when the SuperTrend line changes direction. Below is a minimal strategy that works with TradeAdapter: it uses strategy.entry() so that when you set the alert to Order fills only, TradingView sends the order details (action, size, price) in the alert.
1.1 Open Pine Editor and create a new strategy #
In TradingView, at the bottom of the screen click Pine Editor → Open → New strategy. Delete the default code and paste the following.
//@version=5
strategy("SuperTrend - TradeAdapter", overlay=true, default_qty_type=strategy.fixed, default_qty_value=0.01)
// ─── Inputs ─────────────────────────────────────────────────────────────────
atrLen = input.int(10, "ATR Length", minval=1)
factor = input.float(3.0, "Factor", minval=0.1, step=0.1)
orderQty = input.float(0.01, "Order size (lots for MT / units for exchange)", minval=0.01, step=0.01)
// ─── SuperTrend ─────────────────────────────────────────────────────────────
[supertrend, direction] = ta.supertrend(factor, atrLen)
// Plot
bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color=color.green, style=plot.style_linebr)
downTrend = plot(direction < 0 ? na : supertrend, "Down Trend", color=color.red, style=plot.style_linebr)
fill(bodyMiddle, upTrend, color.new(color.green, 90))
fill(bodyMiddle, downTrend, color.new(color.red, 90))
// ─── Entries (one position at a time; next signal closes and reverses) ───────
if ta.change(direction) < 0
strategy.entry("Long", strategy.long, qty=orderQty)
if ta.change(direction) > 0
strategy.entry("Short", strategy.short, qty=orderQty)
1.2 Adjust the inputs #
- ATR Length — Period for the ATR used by SuperTrend (default 10).
- Factor — Multiplier for the SuperTrend band (default 3.0).
- Order size — For MetaTrader use lot size (e.g.
0.01,0.1). For Direct API (e.g. Binance) use a valid contract/quantity for the symbol (check the exchange’s min and step).
Order size
MT: order_contracts is in LOTS. Check your broker’s symbol specification (min volume, volume step). Direct API: set a quantity that respects the exchange’s minimum and step for the symbol.
1.3 Save and add to chart #
Click Save, then Add to chart. You should see the SuperTrend line and the Strategy Tester at the bottom. Confirm that the strategy opens long/short on the chart as the trend changes.
2 — Create the TradingView alert #
Open the chart where the SuperTrend strategy is applied. Press Alt + A (or the alarm icon) to create a new alert.
2.1 Condition #
Under Condition, select SuperTrend – TradeAdapter (or whatever you named the strategy). Set the trigger to Order fills only. That way TradingView will fill {{strategy.order.action}}, {{strategy.order.contracts}}, and {{strategy.order.price}} when the alert fires.
2.2 Notifications #
In the Notifications tab:
- Enable Webhook URL.
- Paste your personal webhook URL from your TradeAdapter Profile (same webhook for MT and Direct API; the message content tells TradeAdapter where to send the order).
2.3 Message — MetaTrader #
If you are using MT4 or MT5, paste the following JSON in the Message field. Replace YOUR_MT_ACCOUNT with your MetaTrader account number (no spaces). Keep the {{...}} placeholders as they are — TradingView fills them when the alert fires.
{
"mt_trading_account": "YOUR_MT_ACCOUNT",
"platform": "metatrader",
"order_type": "Market",
"time": "{{timenow}}",
"ticker": "{{ticker}}",
"order_contracts": "{{strategy.order.contracts}}",
"order_action": "{{strategy.order.action}}",
"order_price": "{{strategy.order.price}}",
"target_type": "none",
"stop_loss": "",
"take_profit": "",
"market_position": "{{strategy.market_position}}",
"prev_market_position": "{{strategy.prev_market_position}}",
"market_position_size": "{{strategy.market_position_size}}",
"prev_market_position_size": "{{strategy.prev_market_position_size}}",
"strategy_number": "1"
}
You can also build this message with the Message Generator (Strategy).
2.4 Message — Direct API (Binance example) #
If you are using Binance (or another direct API), use the JSON format for that exchange. Example for Binance Spot with strategy placeholders — replace YOUR_API_KEY and YOUR_SECRET with your Binance API credentials. Use "real_order": "No" to test without real orders.
{
"binance_api_key": "YOUR_API_KEY",
"binance_secret": "YOUR_SECRET",
"real_order": "No",
"order_market": "Spot",
"order_type": "Market",
"isolated_margin": "No",
"auto_rounding": "Yes",
"exchange": "{{exchange}}",
"ticker": "{{ticker}}",
"time": "{{timenow}}",
"order_contracts": "{{strategy.order.contracts}}",
"order_action": "{{strategy.order.action}}",
"order_price": "{{strategy.order.price}}",
"market_position": "{{strategy.market_position}}",
"market_position_size": "{{strategy.market_position_size}}",
"prev_market_position": "{{strategy.prev_market_position}}",
"prev_market_position_size": "{{strategy.prev_market_position_size}}"
}
Use the Binance Message Generator (Strategy) for the exact template. For other exchanges, see Direct API overview and the specific setup guide.
Recreate the alert after code or inputs change
If you change the Pine Script strategy or its inputs (e.g. order size), delete the alert and create it again so TradingView uses the updated logic.
3 — First trigger and verification #
When the SuperTrend flips direction, the strategy will submit an order and the alert will fire. TradeAdapter will receive the webhook and forward the order to your MT EA or to the exchange.
-
Check TradingView Alerts log
In TradingView, open the Alerts tab and check the log. You should see an entry with the time, message, and symbol when the alert fired.
-
Check MyTrades
Go to MyTrades. For MetaTrader, use the MetaTrader view. Confirm that the signal was received and that the order was executed or see the error message if it failed.
-
Confirm in your broker
MetaTrader: In your MT4/MT5 terminal, check the Trade or Positions tab — the position should appear. Direct API: In your exchange account (e.g. Binance), check the order history or open positions.
For faster testing, use a lower timeframe (e.g. 1 minute) so the SuperTrend can trigger sooner. Start with a demo or small size.
Next steps #
- MetaTrader: TradingView alerts for MetaTrader — message format details. If the symbol in TradingView doesn’t match your broker’s symbol, see Symbol alignment.
- Direct API: Binance guide, Direct API overview, and the setup page for your exchange.
- FAQ: FAQ — MetaTrader, FAQ — TradingView.