Skip to main content

SuperTrend on Straddle & Strangle Strategy

warning

These strategies are for demonstration purposes only and are not intended for actual trading. AlgoTest is not responsible for any profit or loss arising from the use of these sample strategies.

FREE Supertrend on Straddle & Strangle Pinescript​

Code for Buying Strategy​

info

To learn how to add this pinescript in TradingView, click here.


// This Pine Scriptâ„¢ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlgoTest

//@version=5
indicator("SuperTrend on Straddle & Strangle", overlay = false)

atrLength = input.int(10, 'ATR Length', group = "SuperTrend")
fac = input.float(3, 'Factor', group = "SuperTrend")


spot = input.string( "BANKNIFTY" , title = "Spot Symbol", options = ["NIFTY", "BANKNIFTY", "FINNIFTY", "MIDCPNIFTY"], group = "Index")

tooltip_day = "Enter the day of the expiry. Add 0 infront, if day is in single digit. For eg : 05 instead of 5"
tooltip_month = "Enter the month of the expiry. Add 0 infront, if month is in single digit. For eg : 06 instead of 6"
tooltip_year = "Enter the year of the expiry. Use last digit of the year. For eg : 24 instead of 2024"

_day = input.string( "14" , title = "Expiry Day", tooltip = tooltip_day, group="Expiry Date")
_month = input.string( "06" , title = "Expiry Month", tooltip = tooltip_month, group="Expiry Date")
_year = input.string( "24" , title = "Expiry Year", tooltip = tooltip_year, group="Expiry Date")

tooltip_ = "You can select any Strike, you can also have SuperTrend on Straddle, Strangle"

strike_ce = input.int(50700, "Call Strike", tooltip = tooltip_, group = "Select Strike")
strike_pe = input.int(50700, "Put Strike", tooltip = tooltip_, group = "Select Strike")

var string symbol_CE = spot+_year+_month+_day+"C"+str.tostring(strike_ce)
var string symbol_PE = spot+_year+_month+_day+"P"+str.tostring(strike_pe)

[call_open, call_high, call_low, call_close] = request.security( symbol_CE, timeframe.period , [open, high, low, close] )
[put_open, put_high, put_low, put_close] = request.security( symbol_PE, timeframe.period , [open, high, low, close] )


straddle_open = call_open + put_open
straddle_close = call_close + put_close
straddle_high = math.max(straddle_open, straddle_close)
straddle_low = math.min(straddle_open, straddle_close)

plotcandle ( straddle_open , straddle_high , straddle_low , straddle_close , title = "Straddle/Strangle" , color = straddle_close > straddle_open ? color.green : color.red )


pine_supertrend(factor, atrPeriod) =>
src = straddle_close
atr = ta.atr(atrPeriod)
upperBand = src + factor * atr
lowerBand = src - factor * atr
prevLowerBand = nz(lowerBand[1])
prevUpperBand = nz(upperBand[1])

lowerBand := lowerBand > prevLowerBand or straddle_close[1] < prevLowerBand ? lowerBand : prevLowerBand
upperBand := upperBand < prevUpperBand or straddle_close[1] > prevUpperBand ? upperBand : prevUpperBand
int _direction = na
float superTrend = na
prevSuperTrend = superTrend[1]
if na(atr[1])
_direction := 1
else if prevSuperTrend == prevUpperBand
_direction := straddle_close > upperBand ? -1 : 1
else
_direction := straddle_close < lowerBand ? 1 : -1
superTrend := _direction == -1 ? lowerBand : upperBand
[superTrend, _direction]

[supertrend, direction] = pine_supertrend(factor = fac, atrPeriod = atrLength)


// [supertrend, direction] = ta.supertrend(factor = fac, atrPeriod = atrLength)

plot(direction < 0 ? supertrend : na, "Up direction", color = color.green, style=plot.style_linebr)
plot(direction > 0 ? supertrend : na, "Down direction", color = color.red, style=plot.style_linebr)


entry = direction == -1 and direction[1] == 1
exit = direction == 1 and direction[1] == -1

plotshape(entry, title = "Entry", text = 'Entry', style = shape.labelup, location = location.bottom, color= color.green, textcolor = color.white, size = size.tiny)
plotshape(exit, title = "Exit", text = 'Exit', style = shape.labeldown, location = location.top, color= color.red, textcolor = color.white, size = size.tiny)

alertcondition(entry, "Entry", "Entry")
alertcondition(exit, "Exit", "Exit")

Code for Exiting the Strategy​


// This Pine Scriptâ„¢ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlgoTest
//@version=5

indicator("SuperTrend on Straddle & Strangle", overlay = false)
atrLength = input.int(10, 'ATR Length', group = "SuperTrend")
fac = input.float(3, 'Factor', group = "SuperTrend")
spot = input.string( "BANKNIFTY" , title = "Spot Symbol", options = ["NIFTY", "BANKNIFTY", "FINNIFTY", "MIDCPNIFTY"], group = "Index")
tooltip_day = "Enter the day of the expiry. Add 0 infront, if day is in single digit. For eg : 05 instead of 5"
tooltip_month = "Enter the month of the expiry. Add 0 infront, if month is in single digit. For eg : 06 instead of 6"
tooltip_year = "Enter the year of the expiry. Use last digit of the year. For eg : 24 instead of 2024"
_day = input.string( "14" , title = "Expiry Day", tooltip = tooltip_day, group="Expiry Date")
_month = input.string( "06" , title = "Expiry Month", tooltip = tooltip_month, group="Expiry Date")
_year = input.string( "24" , title = "Expiry Year", tooltip = tooltip_year, group="Expiry Date")
tooltip_ = "You can select any Strike, you can also have SuperTrend on Straddle, Strangle"
strike_ce = input.int(50700, "Call Strike", tooltip = tooltip_, group = "Select Strike")
strike_pe = input.int(50700, "Put Strike", tooltip = tooltip_, group = "Select Strike")
var string symbol_CE = spot+_year+_month+_day+"C"+str.tostring(strike_ce)
var string symbol_PE = spot+_year+_month+_day+"P"+str.tostring(strike_pe)
[call_open, call_high, call_low, call_close] = request.security( symbol_CE, timeframe.period , [open, high, low, close] )
[put_open, put_high, put_low, put_close] = request.security( symbol_PE, timeframe.period , [open, high, low, close] )
straddle_open = call_open + put_open
straddle_close = call_close + put_close
straddle_high = math.max(straddle_open, straddle_close)
straddle_low = math.min(straddle_open, straddle_close)
plotcandle ( straddle_open , straddle_high , straddle_low , straddle_close , title = "Straddle/Strangle" , color = straddle_close > straddle_open ? color.green : color.red )
pine_supertrend(factor, atrPeriod) =>
src = straddle_close
atr = ta.atr(atrPeriod)
upperBand = src + factor * atr
lowerBand = src - factor * atr
prevLowerBand = nz(lowerBand[1])
prevUpperBand = nz(upperBand[1])
lowerBand := lowerBand > prevLowerBand or straddle_close[1] < prevLowerBand ? lowerBand : prevLowerBand
upperBand := upperBand < prevUpperBand or straddle_close[1] > prevUpperBand ? upperBand : prevUpperBand
int _direction = na
float superTrend = na
prevSuperTrend = superTrend[1]
if na(atr[1])
_direction := 1
else if prevSuperTrend == prevUpperBand
_direction := straddle_close > upperBand ? -1 : 1
else
_direction := straddle_close < lowerBand ? 1 : -1
superTrend := _direction == -1 ? lowerBand : upperBand
[superTrend, _direction]
[supertrend, direction] = pine_supertrend(factor = fac, atrPeriod = atrLength)
// [supertrend, direction] = ta.supertrend(factor = fac, atrPeriod = atrLength)
plot(direction < 0 ? supertrend : na, "Up direction", color = color.green, style=plot.style_linebr)
plot(direction > 0 ? supertrend : na, "Down direction", color = color.red, style=plot.style_linebr)
entry = direction == 1 and direction[1] == -1
exit = direction == -1 and direction[1] == 1
plotshape(entry, title = "Entry", text = 'Entry', style = shape.labelup, location = location.bottom, color= color.green, textcolor = color.white, size = size.tiny)
plotshape(exit, title = "Exit", text = 'Exit', style = shape.labeldown, location = location.top, color= color.red, textcolor = color.white, size = size.tiny)
alertcondition(entry, "Entry", "Entry")
alertcondition(exit, "Exit", "Exit")

How to Automate Supertrend on Straddle/Strangle Strategy​

This document explains the automation of the "Supertrend Straddle/Strangle Strategy," a technical analysis method created to capture theta on expiry days. The strategy uses the Supertrend indicator to pinpoint potential entry and exit points for trades on a Straddle/Strangle chart.

What is the Supertrend Straddle/Strangle Strategy?​

The Supertrend Straddle/Strangle strategy uses the Supertrend technical indicator to spot opportunities for selling/buying straddle or strangle options to capture theta & gamma. A supertrend is a popular indicator among technical analysts to determine the direction of the trend.

This strategy is used for intraday trading by plotting the Supertrend indicator on a Straddle/Strangle chart. When a 1-minute candle closes below the Supertrend, the straddle is shorted, and when a 1-minute candle closes above the Supertrend, the short straddle position is exited. You can also use this for buying straddle/strangle when the 1-minute candle closes above the Supertrend and exit the long straddle/strangle positions when the 1-minute candle closes below the Supertrend. You can also use a different time frame according to your preference.

Selecting a timeframe for a strategy is very important. Whether you choose shorter timeframe candles, such as 1, 2, or 5 minutes, or opt for a higher timeframe, like 10 or 15 minutes, it can affect your strategy in both good and bad ways. Let's delve into how different timeframes affect your strategy.

Pros & ConsSmaller Time Frame (1 minute to 5 minutes)Higher Time Frame (10-15 minutes and above)
Entry & ExitUsing a smaller time frame can provide early entry & exit which helps in targeting a big reward compared to a small stop-loss.Using a higher time frame may result in delayed entry, but it can improve signal accuracy.
Risk RewardUsing a smaller timeframe offers a better risk-reward opportunity because of early entry, leading to a smaller stop loss.Using a higher time frame gives a moderate risk-reward opportunity as entry is a little bit late, resulting in a bigger stop loss.
AccuracyAccuracy will not be good with a smaller time frame due to many false signals.Accuracy will be better with a higher time frame as there are fewer but better signals.
Number of TradesThe number of Trades is higher with a smaller time frame which may result in higher charges.The number of trades is lower with a higher time frame
Risk of LossA high risk of loss is possible with a smaller time frame due to many small losses.Moderate risk of loss is possible with a higher time frame due to fewer but larger losses.
Market ConditionsWorks well in a low-volatility market.Can perform effectively in both low-volatility and high-volatility markets.

The Power of the Supertrend​

Imagine Supertrend as a dynamic line on your trading chart. When the price is above the Supertrend, it often suggests that the price is in an uptrend and there is a high possibility that it will further go up. When the price is below the Supertrend, it suggests that the price is in a downtrend and there is a high possibility that it will further go down. So you can use these signals for both buying/selling options.

The Supertrend Straddle strategy is a theta eater strategy based on the logic that if a straddle/strangle combined price is below Supertrend then there is a high possibility that it will go further down to zero. Thus, in the Supertrend Straddle strategy, we look for the 1-minute candle to close below the Supertrend on the Straddle/strange chart. You can choose any other time frame as per your preference. 

This strategy capitalises on situations where the price seems to be losing the momentum of an uptrend and moves to a downtrend. We will look for a candle to close below the supertrend to initiate short trades, aiming to profit if the price continues its downward journey.

Spotting Your Entry: The Entry Candle​

Our strategy hinges on a specific type of price action on a straddle or strangle chart -- the Entry candle. This isn't a fancy candlestick pattern; it's simply a candle that meets the following criteria:

Price Position ​

The opening of the 1-minute candle should be above Supertrend and the close of the same 1-minute candle should be below Supertrend on a straddle/strangle chart as shown in the image below.

As shown in the image above, this is a straddle chart of Nifty 24150 CE and 24150 PE. When the highlighted candle closed below the supertrend, it was time to short 24150 CE and 24150 PE.

Exiting Gracefully: Stop-Loss and Take Profit​

No trading strategy is complete without a solid risk management plan. Here's how the Supertrend Straddle/Strangle strategy incorporates stop-loss and take-profit orders:

  • Stop-Loss: To minimise potential losses, we will close our Nifty ATM Straddle position if any 1-minute candle closes above the supertrend, as depicted in the image below.

  • Take-Profit: Since we employ this strategy on expiry days, our goal is to collect the entire premium of the straddle. Therefore, we anticipate that the value of the straddle will ultimately dwindle to 0. Consequently, we will close our positions at 03:29 on the same day.

Automating Supertrend Straddle Strategy on AlgoTest​

TradingView has a feature called Alerts, which allows you to set up notifications for your trading strategies. For example, if you want to sell an ATM Straddle when our Supertrend Straddle/Strangle Indicator gives a Sell Signal, you can create an alert for that. Once the strategy conditions are met, you will receive an alert via SMS, email, or mobile notification. You can then manually execute the trade on your broker's platform.

Alternatively, if you prefer automation, you can use AlgoTest's Signals to automatically execute the trade based on the alert. You just need to set up the alert on TradingView and link it with AlgoTest Signals to automate your indicator-based strategy.

Requirements​

TradingView Essential Plan​

You should have a minimum TradingView Essential Plan activated on your TradingView account to use AlgoTest Signal.

AlgoTest Signals Plan​

You should have a Signals Plan activated on your AlgoTest account.

Broker Connect with AlgoTest​

You need to connect your broker with AlgoTest to use AlgoTest Signal. To connect your broker with AlgoTest click here. 

Pine Script Code​

You will need a Pine Script Code that can generate buy or sell signals as per your conditions in AlgoTest. You can get this code on top of this page.

Connecting Supertrend Straddle Indicator with AlgoTest​

To automate the Supertrend Straddle Strategy on AlgoTest, you first need to create alerts in TradingView. You can then use these alerts to establish a webhook connection with AlgoTest. You will create alerts in TradingView, which will generate entry and exit alerts based on our strategy conditions. Then, in AlgoTest, you will create an "ATM Straddle Sell" strategy in which you add CE and PE legs and connect both of them via webhook. When your Supertrend Straddle strategy gives a sell signal in TradingView, AlgoTest will capture the alert and sell the ATM CE and ATM PE leg position on the broker's end. Similarly, when your strategy gives an exit alert in TradingView, AlgoTest will capture the alert and exit your entire ATM Straddle position.

Adding Supertrend on Straddle & Strangle indicator in TradingView​

To incorporate supertrend on a Straddle/Strangle Chart, you can utilise a custom Pine Script code that we have developed. This code will display the Supretrend indicator on the chart and enable you to choose option strikes in the settings. To understand how to add the Supretrend on Straddle & Strangle Pine Script Code, available at the top of this page, in TradingView, please click here. Once you've added the Pine Script code in TradingView, the indicator will be visible on the chart, as illustrated in the image below.

Remember to click on the settings button on the indicator and choose the latest expiry and ATM strike where you want to place the supertrend indicator, as shown in the image below.

For instance, if you want to plot a supertrend on the Nifty 1st August 2024 contract 24300 CE & PE Leg, then you should set the inputs as illustrated below.

Creating Alerts in TradingView​

Alerts in TradingView notify you when entry or exit conditions are met. We'll set up alerts for our entry and exit conditions.

  • Assume we want to automate the Supertrend Straddle Strategy on the 5-minute chart. So we will open the Nifty 5 Minute chart in TradingView as shown in the image below. You can choose any time frame you prefer.

  • To create an alert, click on the "Add Alert" button as shown in the image below.

  • We need to create an alert for two conditions: Short Entry and Short Exit. We'll start by setting up an alert for the Short Entry condition, as illustrated in the image below.

  • Similarly, we will create another alert i.e. Short Exit as shown in the image below.

  • Now you will be able to view these alerts in the alert section as shown in the image below.

As we have created our 2 alerts. Now we need to connect these alerts with AlgoTest for automation. So when the Supertrend Straddle Short entry condition is met, an alert will be generated in TradingView and it will be executed automatically on the broker end by AlgoTest. Similarly, when the Exit alert is generated it should automatically exit our entry position on the broker's end.

Connecting Alerts with AlgoTest​

We can connect TradingView Alerts with AlgoTest via webhooks. Follow some simple steps as shown below.

  • Launch a new browser tab. In this new tab, navigate to the AlgoTest website and log in.

  • Once logged in, find and choose the 'Signals' feature of AlgoTest, as seen in the figure below.

Choose 'New Signal' and the 'TradingView Indicator' option.

  • There are now two options available to you:

1. 'Live Deployment'

2. 'Forward Test'

Now, we will briefly explain the above options. 

Firstly, let me explain the word 'Deployment'. 'Deploying a stratеgy' mеans to apply thе stratеgy in thе actual stock markеt.

What does Live Dеploymеnt mеan?

This option allows you to deploy your trading strategy in the live market. Whеn you choosе this option, AlgoTеst monitors thе strategy entry and exit conditions in real-time and as soon as an alert comes from TradingView it will execute the order in your broker terminal.

What does the Forward Test mean? 

Forward tеsting or paper trading means trading with virtual money using LIVE markеt data. You can see your trades and PnL in AlgoTest but you will not need any broker for forward testing. 

If you want to use this strategy in a live trading environment, select 'Live Deployment.' If you'd rather paper trade at first, select 'Forward Test.' Please keep in mind that the methods for linking live and forward trading are the same.

  • Click 'Create Strategy'. Now we have to create a strategy in which we will add a Nifty 24300 CE and Nifty 24300 PE Leg Sell Position.

  • Configure the 24300 CE leg in leg builder as shown in the image below and click on add leg.

Important Strategy Specifications:

  • Nifty Index

  • Time of Entry: 9:16 (This implies that our strategy will start responding to alerts from TradingView only after the chosen entry time, in this example, 9:16. Please remember that this timing does not correspond to the execution at 9:16.)

  • Time of Exit: 3:15 (Our strategy will no longer respond to the TradingView alerts after 3:15. Any alert generated after this time will not be considered. Until 3:15, the strategy will only respond to TradingView alerts.)

Due to the presence of an exit condition, we have not included a stop-loss in the strategy. You, on the other hand, have the option to include one if you wish.

  • Now you can see Leg1 is added under Strategy Legs as shown in the image below.

  • Similarly, we will also add another leg, i.e., the 24300 PE leg in the leg builder. So there will be a total of two legs in this strategy. If a sell signal comes from TradingView, it will execute both the ATM CE and PE legs. After this, click on the "Save and Continue" button.

  • Now, name the strategy. You will be asked to select your broker from the options shown in the image. Choose your preferred broker.

  • It will ask for execution parameters, We'll give you recommendations for the best execution. If you don't want to get into the specifics, just use the settings shown in the image below for both legs of your strategy. You can read about execution settings here.

After you've finished customising these options, click 'Save Settings, and Continue.'

  • You can name the entry alert 'Strategy Entry' (or any other name you like). Now, as seen in the figure below, copy the JSON Block code using the copy icon. We need to put this JSON Block in the TradingView entry alert.

  • Return to the TradingView tab and click on the settings button on our entry alert as shown in the image below.

  • Paste the JSON code that we copied from AlgoTest in the Message box of the alert as shown in the image below. After pasting the JSON code in the message box, we need to copy the webhook URL from AlgoTest and paste it into the webhook URL field of the same alert.

  • Navigate to the AlgoTest tab and click 'I have pasted.' The webhook URL will be shown, along with a 'Copy' button. Click 'Copy,' as seen in the figure below. This webhook URL must be pasted into the 'Notification' page we accessed in the previous step.

  • Then, as seen in the figure below, enter the copied Webhook URL into the 'Webhook URL' space under the 'Notification' tab and click on the Create button. If the Webhook URL is already filled in, please be sure to clear it before inserting the copied URL.

  • Our TradingView Entry alert has now been successfully established. The next step is to set up an exit alert. Return to the AlgoTest page, and you'll see an option for adding an exit alert (optional), as seen in the image below.

  • Click 'Add Exit Alert,' as seen in the figure above.

-Give it a distinct name and save the alert configuration.

-Then you'll notice a JSON block message. Make a copy of this message. 

-Go back to the TradingView tab.
We had already created an exit alert in TradingView. We need to paste this JSON code in the message box of the exit alert.

  • Paste the JSON Code in the message box of the exit alert as shown in the image below.

  • Now we need to copy the webhook URL from AlgoTest and paste that into the exit alert. To copy the webhook URL from AlgoTest, go to the AlgoTest tab, click on "I have pasted" and copy the webhook URL as shown in the image below.

  • Paste it into the notification tab's 'Webhook URL' option in the alert, just as we did when establishing the entry alert. Click on the save button.

  • Finally, return to the AlgoTest page and click on 'Start Listening,' as seen in the figure below by the green marker.

  • Your strategy will show up as "listening" on the SIGNAL tab of AlgoTest if all the stages have been followed successfully, as seen in the screenshot below.

Our strategy is now active and prepared to execute the trade in case any alerts are generated in TradingView by the Supertrend Straddle/Strangle indicator according to our strategy conditions.