Stochastic OB/OS Zones Heatmap
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vranabanana


// The code is based on the Stochastic RSI Heatmap, but uses a normal Stochastic instead the Stochastic RSI when calculating "k" for more accuracy. 
// Credit for the idea goes to Indicator-Jones

//@version=5

//---------------Stochastic Overbought/Oversold Heatmap-----------------

indicator(title="Stochastic OB/OS zones Heatmap", overlay=true,  shorttitle="Stochastic zones", format=format.price, precision=2, timeframe="", timeframe_gaps=false)

// Stochastic settings
var string GRP1       = '══════════   Settings   ══════════'
lengthStoch = input.int(14, "Stochastic Length", minval=1, group=GRP1)
smoothK = input.int(3, "K", minval=1, group=GRP1)
smoothD = input.int(3, "D", minval=1, group=GRP1)
lengthRSI = input.int(14, "RSI Length", minval=1, group=GRP1)
src = input(close, title="RSI Source", group=GRP1)
rsi1 = ta.rsi(src, lengthRSI)
k = ta.stoch(src, high, low, lengthStoch)
d = ta.sma(k, smoothD)

// BUY ZONE levels
var string GRP2       = '══════════   BUY ZONE   ══════════'
BuySto1 = input.int (defval = 20, title="Oversold Zone 1", minval=1, maxval=100, group=GRP2) 
BuySto2 = input.int (defval = 15, title="Oversold Zone 2", minval=1, maxval=100, group=GRP2) 
BuySto3 = input.int (defval = 10, title="Oversold Zone 3", minval=1, maxval=100, group=GRP2) 
BuySto4 = input.int (defval = 5, title="Oversold Zone 4", minval=1, maxval=100, group=GRP2) 

//  SELL ZONE levels
var string GRP3       = '══════════   SELL ZONE   ══════════'
SellSto1 = input.int (defval = 80, title="Overbought Zone 1", minval=1, maxval=100, group=GRP3)
SellSto2 = input.int (defval = 85, title="Overbought Zone 2", minval=1, maxval=100, group=GRP3)
SellSto3 = input.int (defval = 90, title="Overbought Zone 3", minval=1, maxval=100, group=GRP3)
SellSto4 = input.int (defval = 95, title="Overbought Zone 4", minval=1, maxval=100, group=GRP3)

// Calculations
BUY1 = k < BuySto1 and d < BuySto1
BUY2 = k < BuySto2 and d < BuySto2
BUY3 = k < BuySto3 and d < BuySto3
BUY4 = k < BuySto4 and d < BuySto4
SELL1 = k > SellSto1 and d > SellSto1
SELL2 = k > SellSto2 and d > SellSto2
SELL3 = k > SellSto3 and d > SellSto3
SELL4 = k > SellSto4 and d > SellSto4

// Colors
bgcolor (BUY1 ? color.new(color.blue,65) : na, title= "Background Oversold Zone 1")
bgcolor (BUY2 ? color.new(color.blue,65) : na, title= "Background Oversold Zone 2")
bgcolor (BUY3 ? color.new(color.blue,55) : na, title= "Background Oversold Zone 3") 
bgcolor (BUY4 ? color.new(color.blue,1) : na, title= "Background Oversold Zone 4") 

bgcolor (SELL1 ? color.new(color.fuchsia,75) : na, title= "Background Overbought Zone 1")
bgcolor (SELL2 ? color.new(color.fuchsia,65) : na, title= "Background Overbought Zone 2")
bgcolor (SELL3 ? color.new(color.fuchsia,55) : na, title= "Background Overbought Zone 3") 
bgcolor (SELL4 ? color.new(color.fuchsia,1) : na, title= "Background Overbought Zone 4") 


