OPEN-SOURCE SCRIPT
Simple NIFTY Stochastic CE PE System PRAKASAN

//version=6
indicator(
title = "Simple NIFTY Stochastic CE PE System",
shorttitle = "Simple CE PE",
overlay = true
)
//--------------------------------------------------
// INPUTS
//--------------------------------------------------
stochLength = input.int(14, "Stochastic Length")
smoothK = input.int(3, "K Smoothing")
smoothD = input.int(3, "D Smoothing")
oversoldLevel = input.int(20, "Oversold Level")
overboughtLevel = input.int(80, "Overbought Level")
emaLength = input.int(20, "EMA Length")
useVWAP = input.bool(true, "Use VWAP Filter")
showEMA = input.bool(true, "Show EMA")
showVWAP = input.bool(true, "Show VWAP")
//--------------------------------------------------
// INDICATORS
//--------------------------------------------------
emaValue = ta.ema(close, emaLength)
vwapValue = ta.vwap(hlc3)
rawStoch = ta.stoch(close, high, low, stochLength)
stochK = ta.sma(rawStoch, smoothK)
stochD = ta.sma(stochK, smoothD)
//--------------------------------------------------
// TREND FILTER
//--------------------------------------------------
bullishTrend =
close > emaValue and
(not useVWAP or close > vwapValue)
bearishTrend =
close < emaValue and
(not useVWAP or close < vwapValue)
//--------------------------------------------------
// STOCHASTIC SIGNALS
//--------------------------------------------------
// Buy CE when Stochastic crosses upward from oversold
bullishSignal =
ta.crossover(stochK, stochD) and
stochK < 35 and
bullishTrend
// Buy PE when Stochastic crosses downward from overbought
bearishSignal =
ta.crossunder(stochK, stochD) and
stochK > 65 and
bearishTrend
//--------------------------------------------------
// TRADE STATE
//--------------------------------------------------
// 0 = waiting for CE
// 1 = CE active
// 2 = waiting for PE
// -1 = PE active
var int tradeState = 0
bool buyCE = false
bool exitCE = false
bool buyPE = false
bool exitPE = false
buyCE := false
exitCE := false
buyPE := false
exitPE := false
//--------------------------------------------------
// SIGNAL SEQUENCE
//--------------------------------------------------
// Step 1: Buy CE
if tradeState == 0 and bullishSignal
buyCE := true
tradeState := 1
// Step 2: Exit CE
if tradeState == 1 and bearishSignal
exitCE := true
tradeState := 2
// Step 3: Generate fresh Buy PE signal
else if tradeState == 2 and bearishSignal
buyPE := true
tradeState := -1
// Step 4: Exit PE
if tradeState == -1 and bullishSignal
exitPE := true
tradeState := 0
//--------------------------------------------------
// CHART SIGNALS
//--------------------------------------------------
plotshape(
buyCE,
title = "BUY CE",
text = "BUY CE",
style = shape.labelup,
location = location.belowbar,
color = color.green,
textcolor = color.white,
size = size.small
)
plotshape(
exitCE,
title = "EXIT CE",
text = "EXIT CE",
style = shape.labeldown,
location = location.abovebar,
color = color.orange,
textcolor = color.white,
size = size.small
)
plotshape(
buyPE,
title = "BUY PE",
text = "BUY PE",
style = shape.labeldown,
location = location.abovebar,
color = color.red,
textcolor = color.white,
size = size.small
)
plotshape(
exitPE,
title = "EXIT PE",
text = "EXIT PE",
style = shape.labelup,
location = location.belowbar,
color = color.blue,
textcolor = color.white,
size = size.small
)
//--------------------------------------------------
// INDICATOR LINES
//--------------------------------------------------
plot(
showEMA ? emaValue : na,
title = "EMA",
color = color.orange,
linewidth = 2
)
plot(
showVWAP ? vwapValue : na,
title = "VWAP",
color = color.purple,
linewidth = 2
)
//--------------------------------------------------
// ALERTS
//--------------------------------------------------
alertcondition(
buyCE,
title = "BUY CE Alert",
message = "BUY CE signal generated."
)
alertcondition(
exitCE,
title = "EXIT CE Alert",
message = "EXIT CE signal generated."
)
alertcondition(
buyPE,
title = "BUY PE Alert",
message = "BUY PE signal generated."
)
alertcondition(
exitPE,
title = "EXIT PE Alert",
message = "EXIT PE signal generated."
)
indicator(
title = "Simple NIFTY Stochastic CE PE System",
shorttitle = "Simple CE PE",
overlay = true
)
//--------------------------------------------------
// INPUTS
//--------------------------------------------------
stochLength = input.int(14, "Stochastic Length")
smoothK = input.int(3, "K Smoothing")
smoothD = input.int(3, "D Smoothing")
oversoldLevel = input.int(20, "Oversold Level")
overboughtLevel = input.int(80, "Overbought Level")
emaLength = input.int(20, "EMA Length")
useVWAP = input.bool(true, "Use VWAP Filter")
showEMA = input.bool(true, "Show EMA")
showVWAP = input.bool(true, "Show VWAP")
//--------------------------------------------------
// INDICATORS
//--------------------------------------------------
emaValue = ta.ema(close, emaLength)
vwapValue = ta.vwap(hlc3)
rawStoch = ta.stoch(close, high, low, stochLength)
stochK = ta.sma(rawStoch, smoothK)
stochD = ta.sma(stochK, smoothD)
//--------------------------------------------------
// TREND FILTER
//--------------------------------------------------
bullishTrend =
close > emaValue and
(not useVWAP or close > vwapValue)
bearishTrend =
close < emaValue and
(not useVWAP or close < vwapValue)
//--------------------------------------------------
// STOCHASTIC SIGNALS
//--------------------------------------------------
// Buy CE when Stochastic crosses upward from oversold
bullishSignal =
ta.crossover(stochK, stochD) and
stochK < 35 and
bullishTrend
// Buy PE when Stochastic crosses downward from overbought
bearishSignal =
ta.crossunder(stochK, stochD) and
stochK > 65 and
bearishTrend
//--------------------------------------------------
// TRADE STATE
//--------------------------------------------------
// 0 = waiting for CE
// 1 = CE active
// 2 = waiting for PE
// -1 = PE active
var int tradeState = 0
bool buyCE = false
bool exitCE = false
bool buyPE = false
bool exitPE = false
buyCE := false
exitCE := false
buyPE := false
exitPE := false
//--------------------------------------------------
// SIGNAL SEQUENCE
//--------------------------------------------------
// Step 1: Buy CE
if tradeState == 0 and bullishSignal
buyCE := true
tradeState := 1
// Step 2: Exit CE
if tradeState == 1 and bearishSignal
exitCE := true
tradeState := 2
// Step 3: Generate fresh Buy PE signal
else if tradeState == 2 and bearishSignal
buyPE := true
tradeState := -1
// Step 4: Exit PE
if tradeState == -1 and bullishSignal
exitPE := true
tradeState := 0
//--------------------------------------------------
// CHART SIGNALS
//--------------------------------------------------
plotshape(
buyCE,
title = "BUY CE",
text = "BUY CE",
style = shape.labelup,
location = location.belowbar,
color = color.green,
textcolor = color.white,
size = size.small
)
plotshape(
exitCE,
title = "EXIT CE",
text = "EXIT CE",
style = shape.labeldown,
location = location.abovebar,
color = color.orange,
textcolor = color.white,
size = size.small
)
plotshape(
buyPE,
title = "BUY PE",
text = "BUY PE",
style = shape.labeldown,
location = location.abovebar,
color = color.red,
textcolor = color.white,
size = size.small
)
plotshape(
exitPE,
title = "EXIT PE",
text = "EXIT PE",
style = shape.labelup,
location = location.belowbar,
color = color.blue,
textcolor = color.white,
size = size.small
)
//--------------------------------------------------
// INDICATOR LINES
//--------------------------------------------------
plot(
showEMA ? emaValue : na,
title = "EMA",
color = color.orange,
linewidth = 2
)
plot(
showVWAP ? vwapValue : na,
title = "VWAP",
color = color.purple,
linewidth = 2
)
//--------------------------------------------------
// ALERTS
//--------------------------------------------------
alertcondition(
buyCE,
title = "BUY CE Alert",
message = "BUY CE signal generated."
)
alertcondition(
exitCE,
title = "EXIT CE Alert",
message = "EXIT CE signal generated."
)
alertcondition(
buyPE,
title = "BUY PE Alert",
message = "BUY PE signal generated."
)
alertcondition(
exitPE,
title = "EXIT PE Alert",
message = "EXIT PE signal generated."
)
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.