TradingView – Supertrend (Automated Trading)
Build a Supertrend intraday strategy in TradingView and trade it automatically in your broker account.
This guide shows how to build a simple Supertrend intraday strategy in TradingView and trade it automatically through AutoTrader Web. You write the Pine Script strategy, backtest and tune it, then connect it to your broker with a TradingView alert that sends a webhook to AutoTrader Web. Automated trading on TradingView works with leading Indian stock brokers.
In this post we will set up automated trading on a Supertrend strategy inside TradingView. AutoTrader Web supports automated trading from TradingView for leading Indian stock brokers.
We use a TradingView strategy (not a plain indicator) for a few reasons:
- Backtesting
- Easily add and configure parameters
- Easily double the quantity when the signal changes
- Use placeholders available from the strategy
The strategy is for learning and demonstration purposes only. We do not promise any returns.
Strategy logic
This is a very basic Supertrend strategy. It runs between the session times you set. Set the start time and end time to control when your intraday trading starts and stops.
The rules are simple:
- Go long when the price crosses above Supertrend.
- Go short when the price crosses below Supertrend.
To reverse a position when the signal flips, the strategy uses double quantity for every signal except the first and the last.
Here is how the quantity works. Say you set the base quantity to 10. The strategy enters the first position with quantity 10. After that, every new signal uses double the base quantity. This makes sure the strategy takes a reverse position each time the signal changes. Finally, once the session end time has passed, the strategy squares off the open position.
Setup
Before you start, get these three things ready:
- Open an account on TradingView (if you do not have one)
- Register on AutoTrader Web
- Add your Trading Account
Process
Step 1: Create a strategy
- Go to TradingView
- Click on Chart option on the top
- Choose the stock/derivative you want to work on by selecting the Symbol (you can see the symbol on LEFT TOP section of the chart screen)
- You can select any symbol (a stock, or a derivative on the Indian stock exchanges)
- Click on Pine Editor (it can be found on BOTTOM section of the chart screen)
- Change the name from “Untitled Script” to “Supertrend – Intraday“
- Remove existing content from the script and add the following Supertrend code
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pritesh-StocksDeveloper
//@version=6
// "calc_on_every_tick = true" means the strategy checks the signal on every
// price change (tick), not only when the candle closes. So your order fires
// faster during live trading. The catch: results while the candle is still
// forming can change until it closes, so live trades may not match backtest
// results. Set it to "false" if you want to act only on closed candles.
strategy("Supertrend - Intraday", overlay = true, calc_on_every_tick = true)
// ========================================================================
// YOUR SETTINGS
// You change all of these from the strategy Settings window on the chart.
// You do NOT need to edit the code.
// ========================================================================
// Trading time window for the day. Format is START-END in 24-hour time.
// Example "0915-1455" = start at 9:15 am, stop new trades and square off by
// 2:55 pm. Keep the end time at least 2 minutes before your broker's auto
// square-off time.
i_marketSession = input.session(title = "Market session",
defval = "0915-1455", confirm = true, group = "Strategy")
// Supertrend sensitivity. A smaller number reacts faster (more trades); a
// bigger number is calmer (fewer trades).
i_multiplier = input.float(title = "Multiplier",
defval = 4, step = 0.1, confirm = true, group = "Strategy")
// How many candles Supertrend looks back at. Smaller = more sensitive.
i_atrPeriod = input.int(title = "ATR Period",
defval = 14, confirm = true, group = "Strategy")
// ----- Where to send the order (set these once in Settings) -----
// Your AutoTrader Web account name (or a Group name to trade many accounts).
i_account = input.string("ACC_NICKNAME", "Account or Group name",
tooltip = "Your Pseudo account name, or a Group name to trade many accounts at once",
group = "Order routing")
// Tick this ON only if the name above is a Group, not a single account.
i_useGroup = input.bool(false, "This is a Group account",
tooltip = "Tick this if the name above is a Group account", group = "Order routing")
// The contract/stock to trade, written the AutoTrader Web way.
// Examples: NIFTY_28-JUL-2026_FUT (a future) or SBIN (a stock).
i_symbol = input.string("NIFTY_28-JUL-2026_FUT", "Symbol",
tooltip = "AutoTrader Web broker-independent symbol, e.g. NIFTY_28-JUL-2026_FUT or SBIN",
group = "Order routing")
// The exchange code for that symbol. Use NFO for futures/options, NSE for stocks.
i_exchange = input.string("NFO", "Exchange",
tooltip = "Exchange code, e.g. NFO for futures/options or NSE for stocks",
group = "Order routing")
// Product type: INTRADAY (same-day), DELIVERY, or NORMAL (carry forward).
i_product = input.string("INTRADAY", "Product type",
tooltip = "INTRADAY, DELIVERY or NORMAL", group = "Order routing")
// ========================================================================
// THE ALERT MESSAGE (this is what tells AutoTrader Web what to do)
// ========================================================================
//
// AutoTrader Web reads a simple text, ONE setting per line, like this:
//
// account=ACC_NICKNAME
// symbol=NIFTY_28-JUL-2026_FUT
// exchange=NFO
// producttype=INTRADAY
// tradetype=BUY
// ordertype=MARKET
// lots=1
//
// Below, we build that same text automatically from your settings above.
//
// - The "+" signs just glue the pieces together into one long text.
// - "\n" means "go to a new line" (the same as pressing Enter). We put one
// after every setting so each ends up on its own line, exactly as shown
// above. Without "\n" everything would run together on one line and would
// not work.
// - {{strategy.order.action}} and {{strategy.order.contracts}} are TradingView
// placeholders. TradingView fills them in for you when the alert fires:
// {{strategy.order.action}} becomes BUY or SELL
// {{strategy.order.contracts}} becomes the size of that order
// Leave these two exactly as they are. Do NOT type your own BUY/SELL or a
// fixed number here, or the direction and the reverse-size will be wrong.
// - Size line: on a futures or options chart, {{strategy.order.contracts}} is a
// LOT count, so we send "lots=". On a stock chart it is a share count, so we
// send "quantity=". The line below picks the right one from your symbol, so
// the same script works for both.
// - The first line uses "group=" when you ticked "This is a Group account",
// otherwise "account=". Everything else stays the same.
// Pick the size keyword from the symbol: F&O contracts are counted in lots,
// stocks in shares.
symUpper = str.upper(i_symbol)
isDerivative = str.contains(symUpper, "_FUT") or str.contains(symUpper, "_CE_") or str.contains(symUpper, "_PE_")
sizeKey = isDerivative ? "lots=" : "quantity="
alertMsg = (i_useGroup ? "group=" : "account=") + i_account + "\n" +
"symbol=" + i_symbol + "\n" +
"exchange=" + i_exchange + "\n" +
"producttype=" + i_product + "\n" +
"tradetype={{strategy.order.action}}\n" +
"ordertype=MARKET\n" +
sizeKey + "{{strategy.order.contracts}}"
// ========================================================================
// STRATEGY LOGIC (you normally do not need to touch this)
// ========================================================================
// A small helper that answers: is the current candle inside your trading
// time window? true = yes, we may trade; false = outside your hours.
barInSession(sess) => not na(time(timeframe.period, sess))
// Calculate the Supertrend line and its direction from your settings.
[superTrend, dir] = ta.supertrend(i_multiplier, i_atrPeriod)
// Colour the line: red when the trend is down, green when it is up.
colResistance = dir == 1 and dir == dir[1] ? color.new(color.red, 0) : color.new(color.red, 100)
colSupport = dir == -1 and dir == dir[1] ? color.new(color.green, 0) : color.new(color.green, 100)
plot(superTrend, color = colResistance, linewidth = 2)
plot(superTrend, color = colSupport, linewidth = 2)
// The trade rules: go long when price is above the line, short when below.
longCondition = close > superTrend
shortCondition = close < superTrend
// Are we inside your trading hours right now?
bool intradaySession = barInSession(i_marketSession)
// Enter LONG (buy) when price is above the line and we are inside your hours.
// We attach our alert text so the order is sent to AutoTrader Web.
if longCondition and intradaySession
strategy.entry(id = "Long", direction = strategy.long, alert_message = alertMsg)
// Enter SHORT (sell) when price is below the line and we are inside your hours.
if shortCondition and intradaySession
strategy.entry(id = "Short", direction = strategy.short, alert_message = alertMsg)
// Square off (close everything) once your hours are over and a trade is open.
squareOff = (not intradaySession) and (strategy.position_size != 0)
if squareOff
strategy.close_all(comment = "Square-off", alert_message = alertMsg)
Step 2: Backtest and optimize
- Click “Add to chart” button above the pine script editor
- You can select the values of the parameters (short/long term moving average etc.)
- You strategy will be added to the chart & it will automatically open “Strategy Tester“
- You can see backtest results in “Strategy Tester“, there are 3 different sections:
- Overview – Graphical representation of your backtest results
- Performance Summary – Summary statistics of your backtest results
- List of trades – All trades that were generated by your strategy during backtesting
- Optimization involves changing the parameters to improve the strategy performance
- You can click the small Settings icon available in “Strategy Tester” just to the right of your strategy name. Modify the parameters and backtest again.
No trades in the report? Check the capital
If you add the strategy but the Strategy Tester shows “This report requires trade data” and there are no arrows on the chart, the most common reason is the backtest capital. For a high-value instrument like an index future, one contract can cost more than the money set aside for the test, so the strategy cannot afford even one order and places nothing.
Fix it in the strategy Settings → Properties tab:
- Increase Initial capital so it can afford at least one contract. For an index future, a large value such as
10000000works well. - You can also lower the Order size, or set a Margin percentage to trade with leverage.
As soon as the capital is enough, the arrows appear on the chart and the report fills in at the bottom.
Parameters
See the screenshots below to understand how to set the parameters of the strategy as well as the contracts (or quantity).
Important: Set the session ending time at least 2 minutes before your broker’s intraday square-off time.

TradingView Strategy Parameters

TradingView Strategy Quantity
Step 3: Automated trading
Once you have found a good set of parameters, the next step is to set up automated trading.
- Click on the create alert button
- You can find this button in the strategy tester (next to the strategy name)
- It is also available next to the indicator that has been added to your chart (click the 3 dots & you will see this option
- A create alert window will be open, which looks like this

Create Alert
Alert settings
Let us go through the alert settings to use.
Condition
The value of condition should be the strategy that you just added.
Important: You must set the desired parameters on your strategy before creating the alert.
Webhook URL
Sign in to AutoTrader Web and open Alert Auto. → Tokens from the left menu. Create a token and copy your private link:
https://signals.stocksdeveloper.in/tradingview/YOUR-TOKEN
- Keep this link private — it is the key that lets your alerts place orders in your account.
- You do not put your API key anywhere in TradingView; the link is all you need.
- If the link is ever exposed, open Alert Auto. → Tokens again, revoke it, and create a new one. The old link stops working at once.
Alert Name
You can give any name of your choice to the alert.
Message
Good news: you do not write the order by hand. The strategy already builds the full order text from the settings you filled in. So in the Message box, put only this one line:
{{strategy.order.alert_message}}
That is all you type here. When a buy signal fires for 1 lot, AutoTrader Web receives exactly this:
account=ACC_NICKNAME
symbol=NIFTY_28-JUL-2026_FUT
exchange=NFO
producttype=INTRADAY
tradetype=BUY
ordertype=MARKET
lots=1
Where do these values come from? You set them once in the strategy Settings, under the Order routing group:
- Account or Group name: your Pseudo account name. Tick This is a Group account to send the order to a whole group of accounts instead.
- Symbol: the AutoTrader Web broker-independent symbol, for example
NIFTY_28-JUL-2026_FUTfor a future orSBINfor a stock. Keep this format in the box here, so the strategy sets the order size (lots or quantity) for you automatically. AutoTrader Web also accepts TradingView’s own symbol (likeNIFTY1!orNIFTYU2026) if you write a plain-text alert yourself — see Using your TradingView symbol. More details are in the instruments and symbols docs. - Exchange: the exchange code, for example
NFOfor futures and options, orNSEfor stocks. - Product type:
INTRADAYfor intraday trading.
The last two lines are filled by TradingView for you, so you leave them alone:
- tradetype: BUY or SELL for each order.
- lots / quantity: the order size. The strategy sends it as lots for a futures or options symbol, and as quantity (shares) for a stock, picking the right one from your symbol automatically. The first order and the last (square-off) order use the base size you set; the orders in between use double, so each new signal reverses the position. You can see these sizes on the chart.
The full alert format, with every field, option settings and risk limits, is explained in the Alert Automation guide.
After the alert fires, you can follow it from received to placed on the Alert Trail screen in AutoTrader Web.
Coding in Pine? This strategy builds the alert text for you inside the script, so you never type it by hand. For option strategies, our free, open-source StocksDeveloperAlerts library builds ready-made structures like straddles and iron condors.
Important: switch the alert on before the session starts
Switch the alert on before your session start time (in this example, before 9:15 am), while the strategy has no open position. Here is why it matters.
This strategy uses the base quantity for its first trade of the day, and then double the quantity for every trade after that. The double quantity is what reverses the position in a single order: one half closes the old position, and the other half opens the new one in the opposite direction.
Now suppose you switch the alert on in the middle of the session. The strategy may already be in a position from an earlier signal, so the first alert your account receives is a double-quantity reverse order, while your account is still flat. Your account and the strategy then drift apart:
- That first order makes your account 2 lots, but the strategy is only 1 lot in that direction.
- The next signal sends a 2-lot order the other way, which closes your account back to zero, while the strategy is now 1 lot the other way. So your account is flat when it should be holding a position, and no reverse position opens.
- From here, your account stays out of step with the strategy for the rest of the day.
What to do:
- Switch the alert on before the session opens, so the strategy starts flat and takes the base quantity on its first trade, or
- If you must start mid-session, set the session start time to a minute or two after the current time. The strategy then starts fresh from that time and takes the base quantity first.
Precautions
- Once an alert is set, this strategy runs as per the session time you set in the parameters.
- Switch the alert on before the session starts. See Important: switch the alert on before the session starts above for why, and what to do if you must start mid-session.
- Any charting software can repaint a signal when you change something live, so be careful when editing an alert during market hours.
- More information is available in our TradingView docs.
References
Next steps
Thanks for the feedback. Still stuck? Contact support.
Last updated 12 July 2026