Option Buying Pivot and SMA 3 Pivot crossover
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sabarijegan

//@version=5
indicator("Option Buying Pivot and SMA 3 Pivot crossover", overlay=true)

// Calculate Current candle Pivot point
currentCandlePivot = (high[1] + low[1] + close[1]) / 3

// Calculate Current candle True Range
prevHighLowDiff = high[1] - low[1]
prevHighCloseDiff = high[1] - close[2]
prevLowCloseDiff = low[1] - close[2]
currentCandleTrueRange = math.max(prevHighLowDiff, math.max(prevHighCloseDiff, prevLowCloseDiff))

// Calculate Current candle Down Side stop-loss and Up Side stop-loss
PutValue = currentCandlePivot + currentCandleTrueRange
CallValue = currentCandlePivot - currentCandleTrueRange

// Calculate Simple Moving Average of Pivot Point
smaPivot = ta.sma(currentCandlePivot, 3)

// Long Entry Logic
longEntry = currentCandlePivot > smaPivot
bgcolor(longEntry ? color.new(color.green, 90) : na, title="Long")

// Short Entry Logic
shortEntry = currentCandlePivot < smaPivot
bgcolor(shortEntry ? color.new(color.red, 90) : na, title="Short")

// Win Logic
winLong = close > CallValue and longEntry
winShort = close < PutValue and shortEntry
bgcolor(winLong ? color.new(color.green, 90) : na, title="Win Long")
bgcolor(winShort ? color.new(color.green, 90) : na, title="Win Short")

// Loss Logic
lossLong = close < CallValue and longEntry
lossShort = close > PutValue and shortEntry
bgcolor(lossLong ? color.new(color.red, 90) : na, title="Loss Long")
bgcolor(lossShort ? color.new(color.red, 90) : na, title="Loss Short")

// Plotting Entry Labels and Stop Loss Levels
plotshape(series=longEntry, title="Buy Call", color=color.green, style=shape.labeldown, location=location.belowbar, text="Buy Call")
plotshape(series=shortEntry, title="Buy Put", color=color.red, style=shape.labelup, location=location.abovebar, text="Buy Put")

// Plotting Win and Loss Labels
plotshape(series=winLong, title="Win Call", color=color.green, style=shape.labelup, location=location.abovebar, text="Win Call")
plotshape(series=winShort, title="Win Put", color=color.green, style=shape.labeldown, location=location.belowbar, text="Win Put")

plotshape(series=lossLong, title="Loss Call", color=color.red, style=shape.labelup, location=location.abovebar, text="Loss Call")
plotshape(series=lossShort, title="Loss Put", color=color.red, style=shape.labeldown, location=location.belowbar, text="Loss Put")

// Plotting Pivot Point, SMA, Call, and Put Values
plot(currentCandlePivot, color=color.blue, title="Pivot Point")
plot(smaPivot, color=color.orange, title="SMA of Pivot")
plot(CallValue, color=color.green, title="Call Value")
plot(PutValue, color=color.red, title="Put Value")
