TradingView – Long Straddle Options Strategy (Automated Trading)
Build a Long Straddle options strategy in TradingView and send both legs to your broker automatically.
This post shows how to build a Long Straddle options strategy in TradingView using Pine Script, then send the buy orders to your broker automatically through AutoTrader Web. TradingView fires the alert, AutoTrader Web builds the call and put option symbols and places both orders. The strategy is for learning and demonstration only. We do not promise any returns.
Introduction
In this post we build a Long Straddle options strategy on TradingView and automate it using AutoTrader Web. AutoTrader Web works with leading Indian stock brokers.
The strategy is for learning and demonstration purposes only. We do not promise any returns.
What is a Long Straddle?
A long straddle is an options strategy where you buy a call and a put on the same underlying asset, with the same expiry date and the same strike price.
This strategy works well when the underlying asset makes a big move in any direction:
- If the price moves up, you gain on the call option.
- If the price moves down, you gain on the put option.
- If the market does not move much, your maximum loss is the total premium you paid for the call and the put.
We use a moving average indicator to spot a possible big move. Because the strategy needs a large move to be profitable, you should consider using higher values for the short-term and long-term moving average periods.
TradingView does not support option charts. So you run this strategy on the underlying asset. The asset can be a stock, an index, or their future contracts. When a signal fires, AutoTrader Web’s option symbols are passed through the webhook alert.
Exit strategy: For now we assume the time needed for a big move is long, so you exit the positions manually. We may add code to automate the exit later.
Before you start
You need three things:
- An AutoTrader Web account
- A TradingView account
- A trading account with any of the supported brokers
Setup
- Open a TradingView account (if you do not have one).
- Register on AutoTrader Web.
- Add your trading account.
Step 1: Create the strategy
- Go to TradingView.
- Click the Chart option on the top.
- Choose the stock or derivative you want to work on by selecting the Symbol (you can see the symbol on the LEFT TOP section of the chart screen). You can select any symbol (stock, or derivatives on NSE, BSE or MCX).
- Click on Pine Editor (found on the BOTTOM section of the chart screen).
- Change the name from “Untitled Script” to “Long Straddle”.
- Remove the existing content from the script and add the strategy code below.
- Feel free to make any changes of your choice.
Note: We use AutoTrader Web’s public function library. Always use the latest version of this library by changing the version number (line no. 7) to the most recent one.
// 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("Long Straddle", overlay=true, calc_on_every_tick = true)
import Pritesh-StocksDeveloper/StocksDeveloper_AutoTraderWeb/6 as autotrader
// A long straddle is profitable when a market is expected to make large
// moves in any direction. It involves buying call & put at the same strike,
// so it is essentially a "market or direction neutral" strategy.
i_account = input.string(defval = "ACC_NAME", title = "Pseudo/Group Account",
tooltip = "Your Pseudo or Group account name", confirm = true,
group = "Account")
i_group = input.bool(defval = false, title = "Group Account",
tooltip = "Set it to true if you are using a Group account",
confirm = true, group = "Account")
i_exchange = input.string(defval = "NSE", title = "Exchange of Symbol",
tooltip = "Exchange (Ex. NSE, BSE, MCX)", confirm=true,
group = "Instrument")
i_symbol = input.string(defval = "BANKNIFTY", title = "Underlier Symbol",
tooltip = "Underlier symbol (Ex. NIFTY, CRUDEOIL, USDINR)", confirm=true,
group = "Instrument")
i_expiry = input.string(defval = "27-JAN-2022", title="Expiry (DD-MMM-YYYY)",
tooltip = "Expiry date for the option contracts", confirm = true,
group = "Instrument")
// Short and Long moving avg. period input parameters
i_shortPeriod = input.int(title = "Short MA Period", defval = 50, minval = 2,
maxval = 50, confirm = true, group = "Strategy")
i_longPeriod = input.int(title = "Long MA Period", defval = 200, minval = 3,
maxval = 200, confirm = true, group = "Strategy")
i_quantity = input.int(title = "Quantity", defval = 25, minval = 1,
tooltip = "Quantity (in multiples of lot size)",
confirm = true, group = "Strategy")
i_productType = input.string(defval = "NORMAL", title="Product Type",
tooltip = "Product Type (INTRADAY, DELIVERY, NORMAL)", confirm = true,
group = "Strategy")
// Gap between option strikes (Example: BANKNIFTY option strikes has a gap
// of 100 points)
i_gap = input.int(defval = 100, title = "Gap between option strikes",
tooltip = "Example: BANKNIFTY option strikes have a gap of 100",
confirm = true, group = "Strategy")
// Some brokers do not allow MARKET orders in options, so we use LIMIT orders
i_use_limit_ot = input.bool(defval = false, title = "Use LIMIT Order",
tooltip = "Set it to true if your broker has blocked MARKET orders. You must set limit order price if this is set to true.",
confirm = true, group = "Strategy")
i_price = input.float(title = "Limit Order Price", defval = 100, minval = 1,
tooltip = "Limit order price (set it a lot higher than option LTP, so that your order will execute just like a MARKET order)",
confirm = true, group = "Strategy")
// A function to prepare long straddle alert message
prepareLongStraddle(string exchange, string symbol, string expiry, int gap,
string account, bool group, int quantity, string productType,
bool useLimitOrder, float price) =>
// Calculate the strike which is closest to current live price
strike = autotrader.calcClosestStrike(close, gap)
// Prepare call option symbol as per AutoTrader Web's format
callSymbol = autotrader.prepareOptionSymbol(underlier = symbol,
expiry = expiry, optionType = "CE", strike = strike)
// Prepare put option symbol as per AutoTrader Web's format
putSymbol = autotrader.prepareOptionSymbol(underlier = symbol,
expiry = expiry, optionType = "PE", strike = strike)
// Select order type based on user input
orderType = (useLimitOrder) ? "LIMIT" : "MARKET"
// Use price based on order type
prc = (useLimitOrder) ? price : 0
// Prepare alert message for placing two orders (call buy & put buy)
autotrader.preparePlaceOrderAlertMessageForTwoOrders(account = account,
symbol = callSymbol, tradeType = "BUY", group = group,
exchange = exchange, quantity = quantity, productType = productType,
orderType = orderType, price = prc,
account2 = account, symbol2 = putSymbol, tradeType2 = "BUY",
group2 = group, exchange2 = exchange, quantity2 = quantity,
productType2 = productType, orderType2 = orderType, price2 = prc,
comments = "Long Straddle")
// Calculate moving averages
shortAvg = ta.sma(close, i_shortPeriod)
longAvg = ta.sma(close, i_longPeriod)
// Plot moving averages
plot(series = shortAvg, color = color.red, title = "Short MA", linewidth = 2)
plot(series = longAvg, color = color.blue, title = "Long MA", linewidth = 2)
// Long/short condition
longCondition = ta.crossover(shortAvg, longAvg)
shortCondition = ta.crossunder(shortAvg, longAvg)
// If either long or short condition is true then message will be set to
// alert json, otherwise it will be blank
isLongOrShort = longCondition or shortCondition
message = (isLongOrShort) ? prepareLongStraddle(i_exchange, i_symbol,
i_expiry, i_gap, i_account, i_group, i_quantity, i_productType,
i_use_limit_ot, i_price) : ""
// When we expect a large movement in the underlier, then enter into a
// long straddle
// Note that the strategy.long type is meaningless for us because
// we are entering into a long strddle.
// A long straddle buys call & put at the same strike,
// so it is essentially a market or direction neutral strategy.
strategy.entry(id = "Long", direction = strategy.long, when = longCondition,
alert_message = message)
strategy.entry(id = "Short", direction = strategy.short, when = shortCondition,
alert_message = message)
Step 2: Add the strategy to the chart
- Make sure you selected the symbol of the underlying asset or its future (Ex. BANKNIFTY).
- Click the Add to Chart button (see image below).

Step 3: Set the strategy parameters
Parameters are grouped by their use-case.
Account
| Parameter | What it means |
|---|---|
| Pseudo/Group Account | Name of the Pseudo or Group account you want to trade into. Use a Pseudo account to trade into a single account, and a Group account to trade into one or more accounts. |
| Group Account | Check this box if you are using a Group account. |
Instrument
We use AutoTrader Web’s broker-independent instruments. You can use this instrument search tool.
| Parameter | What it means |
|---|---|
| Exchange of Symbol | Exchange of the symbol (Ex. NSE, BSE, MCX). |
| Underlier Symbol | Symbol of the underlying asset (Ex. BANKNIFTY). The strategy uses this to build the option symbol. |
| Expiry | The expiry of the options you want to trade, in DD-MMM-YYYY format. |
Strategy
| Parameter | What it means |
|---|---|
| Short MA Period | Short-term moving average value. |
| Long MA Period | Long-term moving average value. |
| Quantity | Quantity (should be a multiple of lot size). Ex. to buy 1 lot of BANKNIFTY use quantity as 25. |
| Product Type | Use NORMAL (for carryforward positions) or INTRADAY (for intraday positions). |
| Gap between Option Strikes | The difference between two consecutive option strikes (Example: BANKNIFTY option strikes have a gap of 100 points). |
| Use LIMIT Order | Tick this if your broker does not allow MARKET orders in options. |
| Limit Order Price | Applies only if you use LIMIT orders. Set it a lot higher than the current LTP, but below the circuit limit. This makes the options buy order execute just like a MARKET order. Example: if BANKNIFTY is trading close to 35000 and both the call and put of the 35000 strike are trading at around 700, set this price higher, such as 1200. |

Step 4: Create the alert
- Press (Alt + A) to bring up the create alert window. You can also use the buttons shown in the screenshot below.
- Condition – Long Straddle
- Alert Actions
- Webhook – Use the URL below. Remember to add your API key. You can find your API key in the AutoTrader Web menu (Settings – Security).
- You can use any other alert actions as per your preference.
- Alert Name – Any meaningful name (Ex. Long Straddle Alert).
- Message – We pass the alert content from the strategy, so use this placeholder (see screenshot below).
- {{strategy.order.alert_message}}


Disclaimer
This strategy is provided for demonstration purposes only. You can use it as learning or reference material. We do not promise any kind of financial returns based on this strategy.
Ideally, you should research and build your own strategies and backtest them before making them live.
Next steps
Thanks for the feedback. Still stuck? Contact support.
Last updated 20 June 2026