TradingView – Supertrend (Automated Trading)

Build a Supertrend intraday strategy in TradingView and trade it automatically in your broker account.

TradingViewAutomated tradingIntraday
In short

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.

Admin

Demo

TradingView – Supertrend Automated Trading Intraday (English)

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:

Process

Step 1: Create a strategy

  1. Go to TradingView
  2. Click on Chart option on the top
  3. 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)
    1. You can select any symbol (stock, derivatives on NSE, BSE or MCX)
  4. Click on Pine Editor (it can be found on BOTTOM section of the chart screen)
  5. Change the name from “Untitled Script” to “Supertrend – Intraday“
  6. Remove existing content from the script and add following moving average 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=5
strategy("Supertrend - Intraday", overlay=true, calc_on_every_tick = false)
 
// ********** Strategy inputs - Start **********
 
// Used for intraday handling
// Session value should be from market start to the time you want to square-off 
// your intraday strategy
// Important: The end time should be at least 2 minutes before the intraday
// square-off time set by your broker
var i_marketSession = input.session(title="Market session",
     defval="0915-1455", confirm=true)
 
var float i_multiplier = input.float(title = "Multiplier",
     defval = 4, step = 0.1, confirm=true)
 
var int i_atrPeriod = input.int(title = "ATR Period",
     defval = 14, confirm=true)
 
// ********** Strategy inputs - End **********
 
 
// ********** Supporting functions - Start **********
 
// A function to check whether the bar or period is in intraday session
barInSession(sess) => time(timeframe.period, sess) != 0
 
// ********** Supporting functions - End **********
 
 
// ********** Strategy - Start **********
 
[superTrend, dir] = ta.supertrend(i_multiplier, i_atrPeriod)
 
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)
 
// Long/short condition
longCondition = close > superTrend
shortCondition = close < superTrend
 
// See if intraday session is active
bool intradaySession = barInSession(i_marketSession)
 
// Trade only if intraday session is active
  
// Long position
// When longCondition and intradaySession both are true
strategy.entry(id = "Long", direction = strategy.long, 
     when = longCondition and intradaySession)
  
// Short position
// When shortCondition and intradaySession both are true
strategy.entry(id = "Short", direction = strategy.short, 
     when = shortCondition and intradaySession)
  
// Square-off position (when session is over and position is open)
squareOff = (not intradaySession) and (strategy.position_size != 0)
strategy.close_all(when = squareOff, comment = "Square-off")
 
// ********** Strategy - End **********

Step 2: Backtest and optimize

  1. Click “Add to chart” button above the pine script editor
  2. You can select the values of the parameters (short/long term moving average etc.)
  3. You strategy will be added to the chart & it will automatically open “Strategy Tester
  4. You can see backtest results in “Strategy Tester“, there are 3 different sections:
    1. Overview – Graphical representation of your backtest results
    2. Performance Summary – Summary statistics of your backtest results
    3. List of trades – All trades that were generated by your strategy during backtesting
  5. Optimization involves changing the parameters to improve the strategy performance
    1. 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.

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.

Supertrend Parameters

TradingView Strategy Parameters

Supertrend Parameters (Quantity)

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.

  1. Click on the create alert button
    1. You can find this button in the strategy tester (next to the strategy name)
    2. It is also available next to the indicator that has been added to your chart (click the 3 dots & you will see this option
  2. A create alert window will be open, which looks like this

Create alert

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

https://tvx.stocksdeveloper.in/?apiKey=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX&account=ACC-NAME&group=false

Replace the apiKey, account and group parameters with your values:

  • API Key can be found in AutoTrader Web menu (Settings -> Security)
    • Do not share this key with anyone
  • ACC-NAME is your pseudo account or group account name. See this page if you want to understand what these are.
  • group should be true if you are passing a group account, false otherwise

Alert Name

You can give any name of your choice to the alert.

Message

This is important. It decides what orders will be placed in your account.

{
    "command": "PLACE_ORDERS",
    "orders": [
        {
            "variety": "REGULAR",
            "exchange": "NSE",
            "symbol": "SBIN",
            "tradeType": "{{strategy.order.action}}",
            "orderType": "MARKET",
            "productType": "INTRADAY",
            "quantity": {{strategy.order.contracts}}
        }
    ],
    "exchange": "{{exchange}}",
    "ticker": "{{ticker}}",
    "price": "{{close}}",
    "supertrend-resistance": "{{plot_0}}",
    "supertrend-support": "{{plot_1}}",
    "timenow": "{{timenow}}"	
}

Most of these parameters and the JSON message format are explained in our TradingView docs. Read that for full details. Here we focus on the important fields:

  • exchange: The exchange on which your symbol is traded
  • symbol: The AutoTrader Web broker independent symbol; more details are in the instruments and symbols docs
  • tradeType: BUY or SELL (automatically picked up from the strategy using a placeholder)
  • quantity: Quantity of the order (automatically picked up from the strategy using a placeholder)
    • Note that we are not giving a fixed quantity here
    • As per our strategy’s logic, the first order and the last (square-off) order will have the quantity set in the strategy’s parameters
    • All other orders that fall in between the first and last order will have double quantity
    • You can observe the quantities on the chart as well
  • analysis: All other values passed below the orders section (below the orders’ ending square bracket) are OPTIONAL. They are extra information that can help with investigation when there is a problem.
    • You can see that we are printing the values of the short and long moving average when the alert was fired, so we can confirm everything is working as expected
    • You will be able to see these values in AutoTrader Web server logs (Activity)

Precautions

  • Once an alert is set, this strategy runs as per the session time you set in the parameters.
  • If you modify the alert in the middle of the session, be careful to set the session start time after the current time. Otherwise, you might see unexpected orders being fired:
    • There is nothing wrong with AutoTrader Web in this case. If TradingView sends a buy or sell signal, then AutoTrader Web will place the order.
    • If you modify the alert and set the session start time earlier than the current time, then the strategy will take double quantity for the first signal, which is wrong.
    • Any charting software can have issues like repaint when you modify something live, so understand this and be careful.
  • More information is available in our TradingView docs.

References

Next steps

Was this page helpful?

Last updated 20 June 2026