Supply and Demand Zone Confirmation
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MBCryptocurrency

//@version=5
indicator("Supply and Demand Zone","MB Supply & Demand" ,  overlay=true, timeframe="", timeframe_gaps=false)


//CCI

length1 = input.int(20,'CCI length', minval=1, group="CCI Settings")
src1 = input(hlc3, title="Source" , group="CCI Settings")
ma = ta.sma(src1, length1)
cci = (src1 - ma) / (0.015 * ta.dev(src1, length1))

band0 = input.int(-100, "Lower Band" , group="CCI Settings")
band1 = input.int(100, "Upper Band" , group="CCI Settings")
band2= input.int(-150, "Lower Low Band" , group="CCI Settings")
band3= input.int(150, "Upper up Band" , group="CCI Settings")

ma(source1, length1, type) =>
    switch type
        "SMA" => ta.sma(source1, length1)
        "EMA" => ta.ema(source1, length1)
        "SMMA (RMA)" => ta.rma(source1, length1)
        "WMA" => ta.wma(source1, length1)
        "VWMA" => ta.vwma(source1, length1)



timeframe1= input.timeframe('15','First CCI timeframe' , group="CCI Timeframe Settings")
timeframe2= input.timeframe('60','second CCI timeframe' , group="CCI Timeframe Settings")

CCI1= request.security(syminfo.tickerid, timeframe1 ,cci,gaps = barmerge.gaps_off)
plot(CCI1, join= true,display= display.none)
CCI2= request.security(syminfo.tickerid, timeframe2,cci,gaps = barmerge.gaps_off)
plot(CCI2, join= true,display= display.none)


///RSI



rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="RSI MA Settings")
maLengthInput = input.int(14, title="MA Length", group="RSI MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="RSI MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

timeframe=input.timeframe('15', "RSI timeframe", group="RSI Settings")
rsi15= request.security(syminfo.tickerid, timeframe,rsi)

plot(rsi, "RSI", color=#7E57C2, display = display.none)
plot(rsiMA, "RSI-based MA", color=color.yellow, display = display.none)
rsiUpperBand = input.int(65, "RSI Upper Band", group="OverBought/OverSold Settings")
midline=input.int(50, "RSI Middle Band", group="OverBought/OverSold Settings")
rsiLowerBand = input.int(35, "RSI Lower Band", group="OverBought/OverSold Settings")
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green, display = display.none)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green, display = display.none)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill", display = display.none)


cross= ta.cross(cci,band0)



//condition

signal_buy = false
signal_sell = false


if  CCI1 < band0 and CCI2 < band2 and rsiMA < rsiLowerBand and rsi15 < midline                                               //buy when long time is above zero and short time is below zero
    signal_buy := true

if  CCI1 > band1 and CCI2 > band3 and rsiMA > rsiUpperBand and rsi15 > midline                                                   //sell when long time is below zero and short time is above zero
    signal_sell := true

//only first signal to show

Buy = false
Sell = false




Buy := signal_buy ? true : signal_sell ? false : cross ? false : Buy[1]
Sell := signal_sell ? true : signal_buy ? false : cross ? false : Sell[1]



Final_buy = signal_buy and not (Buy[1])
Final_sell = signal_sell and not (Sell[1]) 




// alerts
alertcondition(Final_buy, "Buy", "Time to Buy")

alertcondition(Final_sell , "Sell", "Time to Sell")


//plotshapes

plotshape(Final_sell, title="Sell", style = shape.triangledown, color=color.rgb(255, 0, 0, 50) ,  size = size.small, location = location.abovebar, text = "Sell Zone", textcolor= color.yellow)


plotshape(Final_buy, title="Buy", style = shape.triangleup, color=color.rgb(0, 255, 0, 50) ,  size = size.small, location = location.belowbar, text = "Buy Zone", textcolor= color.blue)















