TradingView – Long Straddle Options Strategy (Automated Trading)

Build a Long Straddle options strategy in TradingView and send both legs to your broker automatically.

TradingViewAutomated tradingOptions strategy
In short

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:

Setup

  1. Open a TradingView account (if you do not have one).
  2. Register on AutoTrader Web.
  3. Add your trading account.

Step 1: Create the strategy

  1. Go to TradingView.
  2. Click the Chart option on the top.
  3. 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).
  4. Click on Pine Editor (found on the BOTTOM section of the chart screen).
  5. Change the name from “Untitled Script” to “Long Straddle”.
  6. Remove the existing content from the script and add the strategy code below.
  7. 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

  1. Make sure you selected the symbol of the underlying asset or its future (Ex. BANKNIFTY).
  2. Click the Add to Chart button (see image below).

Add to Chart

Step 3: Set the strategy parameters

Parameters are grouped by their use-case.

Account

ParameterWhat it means
Pseudo/Group AccountName 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 AccountCheck 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.

ParameterWhat it means
Exchange of SymbolExchange of the symbol (Ex. NSE, BSE, MCX).
Underlier SymbolSymbol of the underlying asset (Ex. BANKNIFTY). The strategy uses this to build the option symbol.
ExpiryThe expiry of the options you want to trade, in DD-MMM-YYYY format.

Strategy

ParameterWhat it means
Short MA PeriodShort-term moving average value.
Long MA PeriodLong-term moving average value.
QuantityQuantity (should be a multiple of lot size). Ex. to buy 1 lot of BANKNIFTY use quantity as 25.
Product TypeUse NORMAL (for carryforward positions) or INTRADAY (for intraday positions).
Gap between Option StrikesThe difference between two consecutive option strikes (Example: BANKNIFTY option strikes have a gap of 100 points).
Use LIMIT OrderTick this if your broker does not allow MARKET orders in options.
Limit Order PriceApplies 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.

Long Straddle Parameters

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
  • 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}}

Long Straddle - Add Alert

Long Straddle Alert

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

Was this page helpful?

Last updated 20 June 2026