- Demos
- Setup
- Multi-Account Trading
- Place Order
- Square-off Position
- Instruments
- Strategies
- Dynamic Alerts
- API Library
- Errors
- Additional Information
Let us look how we can automate the trading from Trading View to many Indian stock brokers. This page will guide you to place automated orders from Trading View to brokers like Supports leading Indian Stock Brokers.
Demos
- Indicator or price based alerts
- Moving Average Strategy
- Supertrend Strategy
- Long Straddle Options Strategy
Setup
- Create an account on Trading View.
- You can compare the different plans offered
- Note: Automation is offered via webhook feature; which is available in pro-plan at the time of writing. Please check trading view website for latest updates.
- Register on AutoTrader Web (if not already done so)
- Add Trading Accounts
- Create Group Accounts if you need to trade in groups
- Note down your API Key, you will need it while creating an alert
- AutoTrader Web Menu (Settings -> API Key)
- Create an alert
- You can learn more about alerts here
- Open a chart & add your indicators
- Create an Alert (Press Alt + a)
- Scroll down to Alert Actions
- Add a webhook for the alert. Please see the next section Webhook URL for url information. More details about webhook is here
- Make sure your alert has valid format as per the explanation given below
- Optional: Use “Send Email” option so that a copy of alert is also sent to your email. This would be helpful in investigation.
Webhook URL
There are two types of users on TradingView:
- Those ones who is building strategies for their personal use
- These people can use basic url: https://tvx.stocksdeveloper.in/
- Those one who are building strategies for their clients or customers
- Instead of hardcoding the apiKey & account in the JSON message, you can also specify it in the url
- Here is a sample: https://tvx.stocksdeveloper.in/?apiKey=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX&account=ACC-NAME&group=false
- If you have used apiKey in the URL, then you can simply skip the apiKey property from JSON message. If apiKey is present in the url, then it is the one which is used. Just remove this row from JSON message (
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
) - If you have specified account number in the URL, then you MUST remove the “account” property from the JSON message. Because if the system finds “account” property in the JSON message, then it is selected.
- You can also specify group=true in the URL, if you are passing a group account name
Multi-Account Trading
- To trade in multiple trading accounts, please use Group Account (details given below)
- To trade in multiple accounts with different quantity, create multiple group accounts & pass multiple orders in a single alert with different quantity
- Note: You only need one TradingView subscription to trade in multiple accounts
Place Order
To place an order, your alert content must be in the following json format. We have given plenty of example, please go through them.
Syntax
Let us take a look at an example and then we will explain each section.
{
"command": "PLACE_ORDERS",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"orders": [
{
"account": "ACC_NUM",
"group": false,
"variety": "REGULAR",
"validity": "DAY",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_FUT",
"tradeType": "BUY",
"orderType": "MARKET",
"productType": "INTRADAY",
"quantity": 75,
"disclosedQuantity": 0,
"price": 0,
"triggerPrice": 0,
"target": 0,
"stoploss": 0,
"trailingStoploss": 0,
"amo": false
},
{
"account": "ACC_NUM",
"group": false,
"variety": "REGULAR",
"validity": "DAY",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_CE_14000",
"tradeType": "SELL",
"orderType": "LIMIT",
"productType": "INTRADAY",
"quantity": 75,
"disclosedQuantity": 0,
"price": 120.5,
"triggerPrice": 0,
"target": 25,
"stoploss": 25,
"trailingStoploss": 2,
"amo": false
}
]
}
Syntax Explanation
- All valid values for parameters can be seen on API Parameters
- The alert message must be in a valid JSON format
- You can lean more about Json Syntax
- Following tools can be used to validate your JSON syntax
Let us understand the basic structure of the message.
{
// This line is fixed and
// indicates a place order command
"command": "PLACE_ORDERS",
// This is your secret api key
// (found in AT Web menu (Settings -> API Key)
// It is optional, if you have provided it in the webhook URL
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
// Next section is a collection of orders,
// you can have anywhere between
// 1 to 10 orders from a single alert
"orders": [
{
// Order 1
},
{
// Order 2
}
],
// You can also add any custom properties.
// This helps to add details of the analysis done in trading view.
// These can be useful for investigation purpose
// and can be viewed from AT Web menu (AutoTrader -> Activity)
// NOTE: Addition of following properties is OPTIONAL,
// you can also skip it.
// You can use ANY built-in variables provided by Trading View.
// More details on following link
// https://in.tradingview.com/chart/?solution=43000531021
// Some example of built-in variables given below
"exchange": "{{exchange}}",
"ticker": "{{ticker}}",
"price": {{close}},
"volume": "{{volume}}",
"timenow": "{{timenow}}"
}
Order syntax
Remember orders is a JSON array. AutoTrader allows anywhere between 1 to 10 orders.
Why multiple orders are allowed on a single alert?
- A user might want to execute an option strategy involving multiple legs (which means he would need to place multiple orders on a single alert)
- Users might want to place an order into multiple accounts with same or different quantity
- Users might want to place an order into multiple group accounts with same or different quantity
"orders": [
{
// Pseudo or Group account number
// It is optional, if you have provided it in the webhook URL
"account": "ACC_NUM",
// true (if you are passing a Group account)
// (Optional, default false)
"group": false,
// Variety: REGULAR, BO, CO
"variety": "REGULAR",
// Validity: DAY, IOC (Optional, default DAY)
"validity": "DAY",
// Exchange: NSE, BSE, MCX
"exchange": "NSE",
// Symbol: AutoTrader Web's symbol
// Search symbol: https://web.stocksdeveloper.in/instrument
"symbol": "NIFTY_29-APR-2021_FUT",
// Trade type: BUY, SELL
// You can also use strategy placeholder {{strategy.order.action}}
"tradeType": "BUY",
// Order type: MARKET, LIMIT, STOP_LOSS, SL_MARKET
"orderType": "MARKET",
// Product type: INTRADAY, DELIVERY, NORMAL
"productType": "INTRADAY",
// Quantity
"quantity": 75,
// Disclosed quantity (Optional, default 0)
"disclosedQuantity": 0,
// Price (optional for MARKET order, default 0)
"price": 0,
// Trigger price (Required only SL, SL_M or Cover orders)
"triggerPrice": 0,
// Target (Only required for Bracket order)
"target": 0,
// Stoploss (Only required for Bracket order)
"stoploss": 0,
// Trailing stoploss
// (Only applicable for Bracket order but is optional)
"trailingStoploss": 0,
// AMO: true to indicate an After Market Order
// (Optional, default false)
"amo": false
},
{
// Another order...
}
]
Examples
Place a Regular Order
Example 1: Place a regular market order to buy Nifty April future.
{
"command": "PLACE_ORDERS",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"orders": [
{
"account": "ACC_NUM",
"variety": "REGULAR",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_FUT",
"tradeType": "BUY",
"orderType": "MARKET",
"productType": "INTRADAY",
"validity": "DAY",
"quantity": 75
}
]
}
Example 2: Place a regular market order to buy 2 quantity of SBIN.
{
"command": "PLACE_ORDERS",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"orders": [
{
"account": "ACC_NUM",
"variety": "REGULAR",
"exchange": "NSE",
"symbol": "SBIN",
"tradeType": "BUY",
"orderType": "MARKET",
"validity": "DAY",
"productType": "INTRADAY",
"quantity": 2
}
]
}
Example 3: Place an order by using TradingView Startegy.
Webhook URL:
https://tvx.stocksdeveloper.in/?apiKey=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX&account=ACC-NAME&group=false
Replace the api key, account name & group parameters with your values.
Now let us look at the alert JSON contents. Here we are using placeholders given by TradingView alerts; these are available in strategies.
{
"command": "PLACE_ORDERS",
"orders": [
{
"variety": "REGULAR",
"exchange": "NSE",
"symbol": "SBIN",
"tradeType": "{{strategy.order.action}}",
"orderType": "MARKET",
"productType": "INTRADAY",
"quantity": {{strategy.order.contracts}}
}
]
}
Place a Cover Order
Example 1: Place a Cover order to sell 1 lot of BANKNIFTY future with a stoploss of 32,000.
{
"command": "PLACE_ORDERS",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"orders": [
{
"account": "ACC_NUM",
"variety": "CO",
"validity": "DAY",
"exchange": "NSE",
"symbol": "BANKNIFTY_29-APR-2021_FUT",
"tradeType": "SELL",
"orderType": "MARKET",
"productType": "INTRADAY",
"quantity": 25,
"triggerPrice": 32000
}
]
}
Place a Bracket Order
Example 1: Place a bracket order to buy 2 quantity of SBIN with target of 3/-, stoploss of 4/- & trailing stoploss of 1/-.
{
"command": "PLACE_ORDERS",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"orders": [
{
"account": "ACC_NUM",
"variety": "BO",
"validity": "DAY",
"exchange": "NSE",
"symbol": "SBIN",
"tradeType": "BUY",
"orderType": "MARKET",
"productType": "INTRADAY",
"quantity": 2,
"price": 330,
"target": 3,
"stoploss": 4,
"trailingStoploss": 1
}
]
}
Place an order into a Group Account
Example 1: Place an order into a group account having name HIGH_PROFILE.
Note: Group accounts can be created from AutoTrader Web menu (Settings -> Group Accounts). More details in Group Accounts.
{
"command": "PLACE_ORDERS",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"orders": [
{
"account": "HIGH_PROFILE",
"group": true,
"variety": "REGULAR",
"exchange": "NSE",
"symbol": "SBIN",
"tradeType": "BUY",
"orderType": "MARKET",
"validity": "DAY",
"productType": "INTRADAY",
"quantity": 2
}
]
}
Example 2: Place an order into two group accounts having names HIGH_PROFILE & LOW_PROFILE (with different quantity).
{
"command": "PLACE_ORDERS",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"orders": [
{
"account": "HIGH_PROFILE",
"group": true,
"variety": "REGULAR",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_FUT",
"tradeType": "BUY",
"orderType": "MARKET",
"productType": "INTRADAY",
"validity": "DAY",
"quantity": 150
},
{
"account": "LOW_PROFILE",
"group": true,
"variety": "REGULAR",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_FUT",
"tradeType": "BUY",
"orderType": "MARKET",
"productType": "INTRADAY",
"validity": "DAY",
"quantity": 75
}
]
}
Place multiple orders
Example 1: Let us put an order for a option straddle strategy. We will buy Call & Put options with 2 different orders being fired from a single alert.
{
"command": "PLACE_ORDERS",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"orders": [
{
"account": "ACC_NAME",
"variety": "REGULAR",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_CE_15000",
"tradeType": "BUY",
"orderType": "MARKET",
"productType": "INTRADAY",
"validity": "DAY",
"quantity": 75
},
{
"account": "ACC_NAME",
"variety": "REGULAR",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_PE_15000",
"tradeType": "BUY",
"orderType": "MARKET",
"productType": "INTRADAY",
"validity": "DAY",
"quantity": 75
}
]
}
Add analysis details
Example 1: You can add any analysis detail in the given format.
"field_name": "field_value"
You can also add any custom properties. This helps to add details of the analysis done in trading view. These can be useful for investigation purpose and can be viewed from AT Web menu (AutoTrader -> Activity).
Note: Addition of following properties is OPTIONAL, you can also skip it.
You can use ANY built-in variables provided by Trading View. More details on following link:
https://in.tradingview.com/chart/?solution=43000531021
{
"command": "PLACE_ORDERS",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"orders": [
{
"account": "ACC_NUM",
"variety": "REGULAR",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_FUT",
"tradeType": "BUY",
"orderType": "MARKET",
"productType": "INTRADAY",
"validity": "DAY",
"quantity": 75
}
],
"exchange": "{{exchange}}",
"ticker": "{{ticker}}",
"price": "{{close}}",
"volume": "{{volume}}",
"short moving avg": "{{plot_0}}",
"long moving avg": "{{plot_1}}",
"timenow": "{{timenow}}"
}
Square-off Position
To square-off a position, your alert content must be in the following json format. We have given plenty of example, please go through them.
Syntax
Let us take a look at an example and then we will explain each section.
{
"command": "SQUARE_OFF_POSITION",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"positions": [
{
"account": "ACC_NUM",
"group": false,
"category": "NET",
"type": "MIS",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_FUT"
},
{
"account": "ACC_NUM",
"group": false,
"category": "NET",
"type": "MIS",
"exchange": "NSE",
"symbol": "SBIN"
}
]
}
Syntax Explanation
- All valid values for parameters can be seen on API Parameters
- The alert message must be in a valid JSON format
- You can lean more about Json Syntax
- Following tools can be used to validate your JSON syntax
Let us understand the basic structure of the message.
{
// This line is fixed and
// indicates a square-off position command
"command": "SQUARE_OFF_POSITION",
// This is your secret api key
// (found in AT Web menu (Settings -> API Key)
// It is optional, if you have provided it in the webhook URL
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
// Next section is a collection of positions,
// you can have anywhere between
// 1 to 10 positions from a single alert
"positions": [
{
// Position 1
},
{
// Position 2
}
],
// You can also add any custom properties.
// This helps to add details of the analysis done in trading view.
// These can be useful for investigation purpose
// and can be viewed from AT Web menu (AutoTrader -> Activity)
// NOTE: Addition of following properties is OPTIONAL,
// you can also skip it.
// You can use ANY built-in variables provided by Trading View.
// More details on following link
// https://in.tradingview.com/chart/?solution=43000531021
// Some example of built-in variables given below
"exchange": "{{exchange}}",
"ticker": "{{ticker}}",
"price": "{{close}}",
"volume": "{{volume}}",
"timenow": "{{timenow}}"
}
Position syntax
Remember positions is a JSON array. AutoTrader allows anywhere between 1 to 10 positions.
Why multiple positions are allowed on a single alert?
- A user might want to execute an option strategy involving multiple legs (which means he would need to square-off multiple positions on a single alert)
- Users might want to square-off a position from multiple accounts or groups
"positions": [
{
// Pseudo or Group account number
// It is optional, if you have provided it in the webhook URL
"account": "ACC_NUM",
// true (if you are passing a Group account)
// (Optional, default false)
"group": false,
// Category: NET, DAY
"category": "NET",
// Type: MIS, CNC, NRML, BO, CO
"type": "MIS",
// Exchange: NSE, BSE, MCX
"exchange": "NSE",
// Symbol: AutoTrader Web's symbol
// Search symbol: https://web.stocksdeveloper.in/instrument
"symbol": "NIFTY_29-APR-2021_FUT"
},
{
// Another position...
}
]
Examples
Square-off a single position
Webhook URL:
https://tvx.stocksdeveloper.in/
Example 1: Square-off a single position
{
"command": "SQUARE_OFF_POSITION",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"positions": [
{
"account": "ACC_NUM",
"group": false,
"category": "NET",
"type": "MIS",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_FUT"
}
]
}
Example 2: Square-off a single position (api key, account number & group flag are provided in the url)
Webhook URL:
https://tvx.stocksdeveloper.in/?apiKey=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX&account=ACC-NAME&group=false
{
"command": "SQUARE_OFF_POSITION",
"positions": [
{
"category": "NET",
"type": "MIS",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_FUT"
}
]
}
Square-off a single position in a GROUP account
{
"command": "SQUARE_OFF_POSITION",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"positions": [
{
"account": "GROUP_ACC_NUM",
"group": true,
"category": "NET",
"type": "MIS",
"exchange": "NSE",
"symbol": "NIFTY_29-APR-2021_FUT"
}
]
}
Square-off a multiple positions
{
"command": "SQUARE_OFF_POSITION",
"apiKey": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"positions": [
{
"account": "ACC_NUM",
"group": false,
"category": "NET",
"type": "MIS",
"exchange": "NSE",
"symbol": "NIFTY_12-AUG-2021_CE_16000"
},
{
"account": "ACC_NUM",
"group": false,
"category": "NET",
"type": "MIS",
"exchange": "NSE",
"symbol": "NIFTY_12-AUG-2021_PE_16000"
}
]
}
Instruments
To find correct exchange & symbol values, please use our broker independent symbols. You can use this instrument search tool.
Always use AutoTrader Web’s broker independent symbols.
Note: Do NOT use broker specific symbols, you will get instrument not found error.
You can read more about AutoTrader Web’s broker independent instrument symbols.
Strategies
Strategies are extremely useful in TradingView. The primary reason to use strategies are:
- Backtesting
- Easily add and configure parameters
- Easily double the quantity when the signal changes
- Use placeholders available from strategy
Examples
Dynamic Alerts
When you want to add dynamic content to your alert message, you can use any one of the following placeholders.
{{strategy.order.alert_message}}
You can create alert json in your strategy and pass alert json to alert_message parameter. Your alert json content can then use any one of the parameters based on what you have used.
See following examples:
strategy.entry(id = "Long", long = strategy.long, when = longCondition,
alert_message = "Your alert message content")
Strategies using Dynamic Alerts
API Library
We have built public API library for our AutoTrader Web users. This library has many functions which makes it easy for a pine script programmer to integrate with AutoTrader Web.
How to use?
To use this library, import it in your pine script by using the following line at the top.
Step 1: Visit the library page & copy the import statement
- Visit this library page
- Scroll down until you see the library code. There is a copy button just above the code (see screenshot below)
Step 2: Add the copied import statement to your pine script (just below strategy() function)
Functions
preparePlaceOrderAlertMessage
Prepares a complete place order alert message in AutoTrader Web’s JSON structure.
Signature
// @function Prepare a place order alert json message
// @param account Pseudo or Group account number
// @param symbol AutoTrader Web's stock/derivative symbol
// @param tradeType Trade type [BUY, SELL]
// @param group Set it to true if you are using a group account (Default: false)
// @param exchange Symbol's exchange [NSE, BSE, MCX] (Default: NSE)
// @param quantity Quantity (Default: 1)
// @param price Price (Default: 0)
// @param orderType Order type [MARKET, LIMIT, STOP_LOSS, SL_MARKET] (Default: MARKET)
// @param productType Product type [INTRADAY, DELIVERY, NORMAL] (Default: INTRADAY)
// @param triggerPrice Trigger price (Default: 0)
// @param variety Variety [REGULAR, BO, CO] (Default: REGULAR)
// @param validity Validity [DAY, IOC] (Default: DAY)
// @param disclosedQuantity Disclosed quantity (Default: 0)
// @param target Target (for Bracket order only) (Default: 0)
// @param stoploss Stoploss (for Bracket order only) (Default: 0)
// @param trailingStoploss (for Bracket order only) Trailing Stoploss (Default: 0)
// @param amo Set it to true for AMO (After Market Order) (Default: false)
// @param comments Comments (Default: blank)
// @returns A complete alert message to place orders
export preparePlaceOrderAlertMessage(string account, string symbol, string tradeType,
bool group = false, string exchange = "NSE", int quantity = 1,
float price = 0, string orderType = "MARKET",
string productType = "INTRADAY", float triggerPrice = 0,
string variety = "REGULAR", string validity = "DAY",
int disclosedQuantity = 0, float target = 0, float stoploss = 0,
float trailingStoploss = 0, bool amo = false, string comments = "")
Example
message = preparePlaceOrderAlertMessage(account = "ACC_NUM",
symbol = "BANKNIFTY_24-FEB-2022_CE_34000",
tradeType = "BUY", group = false, exchange = "NSE",
quantity 25, price = 0, orderType = "MARKET",
productType = "NORMAL")
strategy.entry(id = "Long", direction = strategy.long, when = longCondition,
alert_message = message)
preparePlaceOrderAlertMessageForTwoOrders
Prepares a complete place order alert message in AutoTrader Web’s JSON structure for two orders to be placed in a single alert. It is useful for trading option strategies. If you want to place more than two orders in a single alert, you can make a similar function.
Signature
// @function Prepare a place order alert json message for 2 orders
// @param account Pseudo or Group account number
// @param symbol AutoTrader Web's stock/derivative symbol
// @param tradeType Trade type [BUY, SELL]
// @param group Set it to true if you are using a group account (Default: false)
// @param exchange Symbol's exchange [NSE, BSE, MCX] (Default: NSE)
// @param quantity Quantity (Default: 1)
// @param price Price (Default: 0)
// @param orderType Order type [MARKET, LIMIT, STOP_LOSS, SL_MARKET] (Default: MARKET)
// @param productType Product type [INTRADAY, DELIVERY, NORMAL] (Default: INTRADAY)
// @param triggerPrice Trigger price (Default: 0)
// @param account2 Pseudo or Group account number [for order 2]
// @param symbol2 AutoTrader Web's stock/derivative symbol [for order 2]
// @param tradeType2 Trade type [BUY, SELL] [for order 2]
// @param group2 Set it to true if you are using a group account (Default: false) [for order 2]
// @param exchange2 Symbol's exchange [NSE, BSE, MCX] (Default: NSE) [for order 2]
// @param quantity2 Quantity (Default: 1) [for order 2]
// @param price2 Price (Default: 0) [for order 2]
// @param orderType2 Order type [MARKET, LIMIT, STOP_LOSS, SL_MARKET] (Default: MARKET) [for order 2]
// @param productType2 Product type [INTRADAY, DELIVERY, NORMAL] (Default: INTRADAY) [for order 2]
// @param triggerPrice2 Trigger price (Default: 0) [for order 2]
// @param comments Comments
// @returns A complete alert message to place 2 orders
export preparePlaceOrderAlertMessageForTwoOrders(string account, string symbol,
string tradeType, bool group = false, string exchange = "NSE",
int quantity = 1, float price = 0, string orderType = "MARKET",
string productType = "INTRADAY", float triggerPrice = 0,
string account2, string symbol2, string tradeType2,
bool group2 = false, string exchange2 = "NSE", int quantity2 = 1,
float price2 = 0, string orderType2 = "MARKET",
string productType2 = "INTRADAY", float triggerPrice2 = 0,
string comments = "")
Example
message = preparePlaceOrderAlertMessageForTwoOrders(account = "ACC_NUM",
symbol = "BANKNIFTY_24-FEB-2022_CE_34000",
tradeType = "BUY", group = false, exchange = "NSE",
quantity = 25, productType = "NORMAL", orderType = "MARKET", price = 0,
account2 = "ACC_NUM",
symbol2 = "BANKNIFTY_24-FEB-2022_PE_34000",
tradeType2 = "BUY", group2 = false, exchange2 = "NSE",
quantity2 = 25, productType2 = "NORMAL", orderType2 = "MARKET", price2 = 0,
comments = "Long Straddle")
strategy.entry(id = "Long", direction = strategy.long, when = longCondition,
alert_message = message)
prepareSqOffPositionAlertMessage
Prepares a complete square-off alert message in AutoTrader Web’s JSON structure.
Signature
// @function Prepare a square-off position alert json message
// @param account Pseudo or Group account number
// @param symbol AutoTrader Web's stock/derivative symbol
// @param group Set it to true if you are using a group account (Default: false)
// @param exchange Symbol's exchange [NSE, BSE, MCX] (Default: NSE)
// @param category Position category [NET, DAY]
// @param type Position type [MIS, CNC, NRML, BO, CO]
// @param comments Comments (Default: Blank)
// @returns A complete alert message to square-off position
export prepareSqOffPositionAlertMessage(string account, string symbol,
bool group = false, string exchange = "NSE", string category = "NET",
string type = "MIS", string comments = "")
Example
message = prepareSqOffPositionAlertMessage(account = "ACC_NUM",
symbol = "BANKNIFTY_24-FEB-2022_CE_34000",
group = false, exchange = "NSE",
type = "NRML", comments = "Square Off Request")
strategy.entry(id = "Long", direction = strategy.long, when = longCondition,
alert_message = message)
prepareSqOffPositionAlertMessageForTwoPositions
Prepares a complete square-off alert message in AutoTrader Web’s JSON structure for two orders to be placed in a single alert. It is useful for trading option strategies. If you want to place more than two orders in a single alert, you can make a similar function.
Signature
// @function Prepare a square-off position alert json message for two positions
// @param account Pseudo or Group account number
// @param symbol AutoTrader Web's stock/derivative symbol
// @param group Set it to true if you are using a group account (Default: false)
// @param exchange Symbol's exchange [NSE, BSE, MCX] (Default: NSE)
// @param category Position category [NET, DAY]
// @param type Position type [MIS, CNC, NRML, BO, CO]
// @param account2 Pseudo or Group account number [for order 2]
// @param symbol2 AutoTrader Web's stock/derivative symbol [for order 2]
// @param group2 Set it to true if you are using a group account (Default: false) [for order 2]
// @param exchange2 Symbol's exchange [NSE, BSE, MCX] (Default: NSE) [for order 2]
// @param category2 Position category [NET, DAY] [for order 2]
// @param type2 Position type [MIS, CNC, NRML, BO, CO] [for order 2]
// @param comments Comments
// @returns A complete alert message to square-off of two positions
export prepareSqOffPositionAlertMessageForTwoPositions(string account,
string symbol, bool group = false, string exchange = "NSE",
string category = "NET", string type = "MIS", string account2,
string symbol2, bool group2 = false, string exchange2 = "NSE",
string category2 = "NET", string type2 = "MIS",
string comments = "")
Example
message = prepareSqOffPositionAlertMessageForTwoPositions(
account = "ACC_NUM",
symbol = "BANKNIFTY_24-FEB-2022_CE_34000",
group = false, exchange = "NSE",
type = "NRML", account2 = "ACC_NUM",
symbol2 = "BANKNIFTY_24-FEB-2022_CE_34000",
tradeType2 = "BUY", group2 = false, exchange2 = "NSE",
type2 = "NRML", comments = "Square Off Request")
strategy.entry(id = "Long", direction = strategy.long, when = longCondition,
alert_message = message)
prepareFutureSymbol
Prepares a future symbol as per AutoTrader Web’s broker independent format defined here.
Signature
// @function Prepares future symbol for AutoTrader Web
// @param underlier Underlier symbol (Ex. BANKNIFTY, USDINR, CRUDEOIL)
// @param expiry Expiry date in (DD-MMM-YYYY) format
// @returns future symbol as per AutoTrader Web's format
export prepareFutureSymbol(string underlier, string expiry)
Example
futSymbol = prepareFutureSymbol(underlier = "BANKNIFTY",
expiry = "24-FEB-2022")
prepareOptionSymbol
Prepares an option symbol as per AutoTrader Web’s broker independent format defined here.
Signature
// @function Prepares option symbol for AutoTrader Web
// @param underlier Underlier symbol (Ex. BANKNIFTY, USDINR, CRUDEOIL)
// @param expiry Expiry date in (DD-MMM-YYYY) format
// @param optionType Option type [CE, PE]
// @param strike Strike price
// @returns option symbol as per AutoTrader Web's format
export prepareOptionSymbol(string underlier, string expiry, string optionType,
int strike)
Example
callSymbol = prepareOptionSymbol(underlier = "BANKNIFTY",
expiry = "24-FEB-2022", optionType = "CE", strike = 34000)
calcClosestStrike
Calculates the closest option strike price as per the given underlier price.
Signature
// @function Calculates the closest option strike price as per the given
// underlier price
// @param underlierPrice The underlier's price
// @param gap The gap between option strikes
// (Example. The gap is 100 for options of BANKNIFTY)
// @returns the closest option strike price
export calcClosestStrike(float underlierPrice, int gap)
Example
strike = calcClosestStrike(underlierPrice = close, gap = 100)
Other functions
There are other functions also available in the library which might be useful in certain scenarios. Please look at the library code at the bottom of this link.
Errors
What to do when orders are not placed but you see the alert on Trading View?
- Use “Send Email” option while configuring the alert, so that a copy of alert is also sent to your email. This is extremely helpful in investigation.
- First & foremost check you have passed correct api_key. It can be passed in the webhook url or json message.
- Check if your alert message content is valid json. See JSON Errors section below.
- Check your activity logs (Menu: AutoTrader -> Activity) to see if your order has failed validation or at the broker
- If you do not see any activity logs & you have entered correct API key; then do the following:
- Send us details of your investigation:
- Alert setting screenshot showing api key used properly
- Alert message time & content in text format (available on TradingView “Alert Log” window)
- Activity logs screenshot
- Note: Support team will only investigate once above mentioned details are properly provided to us.
- Send us details of your investigation:
JSON Errors
If your JSON format is wrong or order details are not valid, then you will get errors. As long as you have entered correct api_key; you can see all of your errors on AutoTrader Web menu (AutoTrader -> Activity).
JSON parsing errors can occur if you your alert content is not in a valid JSON format. In such cases, use this validation tool to find errors. Copy the content of the generated alerts into the validation tool (you can view generated alerts in “Alert Log” section on TradingView). See example below:
Here we missed a comma, hence an error is shown…
Additional Information
If you are testing, then do not forget to stop all alerts before closing the Trading View website. Trading View alerts or strategies keep running on their server, unless you have stopped it. In case a user forgets to stop, orders might keep executing in his accounts.
Automation has it’s own risks & users must understand how Trading view Alerts & Strategies work before proceeding with automation. Also we strongly recommend doing thorough testing with either minimum possible capital before going on to increase your capital as well as risk.
Please be careful, avoid modifying the alerts or strategy settings during live market. This is because, TradingView can repaint a signal when you modify the settings; leading to unexpected results. If you are doing testing then it is okay to modify in live market. Here are some references to repainting:
The support for Trading View will evolve further as we try to understand our clients needs. So feel free to share your feedback.