Skip to main content

VWAP on Straddle & Strangle Strategy

FREE VWAP on Straddle & Strangle Pinescript

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("VWAP on Straddle & Strangle", overlay = false)

var bool first = true
var strike_gap = map.new<string, int>()

if first
first := false
strike_gap.put("NIFTY", 50)
strike_gap.put("BANKNIFTY", 100)
strike_gap.put("FINNIFTY", 50)
strike_gap.put("MIDCPNIFTY", 25)


spot = input.string( "NIFTY" , 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( "11" , title = "Expiry Day", tooltip = tooltip_day, group="Expiry Date")
_month = input.string( "07" , 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 VWAP on Straddle, Strangle"

strike_ce = input.int(24300, "Call Strike", tooltip = tooltip_, group = "Select Strike")
strike_pe = input.int(24300, "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] )

call_volume = request.security( symbol_CE, timeframe.period , volume )
put_volume = request.security( symbol_PE, timeframe.period , volume )

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)
straddle_volume = call_volume + put_volume


var float sumPriceVolume = 0.0
var float sumVolume = 0.0
var float vwap = 0.0

if (dayofweek != dayofweek[1])
sumPriceVolume := 0.0
sumVolume := 0.0
vwap := 0.0

sumPriceVolume += straddle_close * straddle_volume
sumVolume += straddle_volume
vwap := sumPriceVolume / sumVolume

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

// vwap = ta.vwap(straddle_close)

plot ( vwap , title = "VWAP on Straddle" , color = color.blue , linewidth = 2 )


entry = straddle_close < vwap and straddle_close[1] >= vwap[1]
exit = straddle_close >= vwap and straddle_close[1] < vwap[1]

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

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