MM Chop Filter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MoneyMovesInvestments
//@version=5
indicator("MM Chop Filter", shorttitle = "MM Chop Filter")


cae_len = input.int(21, minval=1, title="Length",inline="cae1")
cae_src = input.source(close, "Source",inline="cae1") 
ch_ut=input.float(60.0, "Choppy Thresholds: Upper", inline = "choppy1") 
ch_lt=input.float(40.0, "Lower", inline = "choppy1")
bc_buy = input.bool(true, "Breakup",inline="caebc")
bc_sell = input.bool(true, "Breakdown <- Color Bars",inline="caebc")

buy_alerts = input.bool(true,"Buy", inline="caeal")
sell_alerts = input.bool(true,"Sell", inline="caeal")
choppy_alerts = input.bool(true,"Choppy <- Alerts", inline="caeal")

//User has to agree to have the indi loading on the chart.
var user_consensus = input.string(defval="", title="TYPE 'agree' TO ADD TO CHART. \nTrading involves a risk of loss, and may not be appropriate for every one. Please consider carefully if trading is appropriate for you. Past performance is not indicative of future results. Any and all indicator signals provided by 'MM Chop And Explode' are for educational purposes only. Is your responsibility knowing that by typing 'agree' you are accepting that the AI would trade on your behalf at your own risk. \nRELEASE INFORMATION 2021 © Money Moves Investments", confirm = true, group="consensus")
var icc = false
if user_consensus == "agree"
    icc := true
else
    icc := false


cae_up = ta.rma(math.max(ta.change(cae_src), 0), cae_len)
cae_down = ta.rma(-math.min(ta.change(cae_src), 0), cae_len)
cae_rsi = cae_down == 0 ? 100 : cae_up == 0 ? 0 : 100 - (100 / (1 + cae_up / cae_down))

plot(icc?cae_rsi:na, color=color.black)

cae_change1 = cae_rsi[1] > ch_ut ? color.aqua :na
cae_change2= cae_rsi[1] < ch_lt ? color.red: na

p1 = plot(icc?cae_rsi:na, style=plot.style_linebr, linewidth=3, color=cae_change1)
p2 = plot(icc?cae_rsi:na, style=plot.style_linebr, linewidth=3, color=cae_change2)

band1 = hline(70)
band2 = hline(30)
band3 = hline (60)
band4 = hline (40)
midline= hline (50)
band5=hline(55)
band6=hline(45)
//fill(band1, band3, color=color.green, transp=90)
fill(band3, band4, color=icc?color.yellow:#00000000, transp=90)
//fill(band4, band2, color=color.red, transp=90)
//fill (band5,band6, color=color.black, transp=50)

choppy_bup = ta.crossover(cae_rsi, ch_ut)
choppy_bdown = ta.crossunder(cae_rsi, ch_lt)

up_to_choppy = ta.crossunder(cae_rsi, ch_ut)
down_to_choppy = ta.crossover(cae_rsi, ch_lt)

bgcolor(icc and choppy_bup ? color.new(color.aqua, 70) : na)
bgcolor(icc and choppy_bdown ? color.new(color.red, 70) : na)

plotshape(icc and choppy_bup, title="Choppy Range Break-ups", style=shape.triangleup,color=color.aqua,location=location.bottom)
plotshape(icc and choppy_bdown, title = "Choppy Range Break-downs",style = shape.triangledown,color=color.red,location=location.top)
barcolor(icc and choppy_bup and bc_buy ? color.aqua : na) 
barcolor(icc and choppy_bdown and bc_sell ? color.red : na)

alertcondition(icc and choppy_bup and buy_alerts, "BUY", "Choppy Range Break-up")
alertcondition(icc and choppy_bdown and sell_alerts, "SELL", "Choppy Range Break-down")
alertcondition(icc and up_to_choppy and choppy_alerts, "BUY EXIT", "Got inside Choppy Range")
alertcondition(icc and down_to_choppy and choppy_alerts, "SELL EXIT", "Got inside Choppy Range")
// cond3 = cae_rsi > 55 and close[1] < cae_rsi and close < 60 ? 1 : 0
// barcolor(cond3 ? color.yellow : na)

// cond4 = cae_rsi > 45 and close[1] < cae_rsi and close [1] < 55 ? 1 : 0
// barcolor(cond4 ? color.black : na)

// cond5 = cae_rsi > 40 and close[1] < cae_rsi and close [1] < 55 ? 1 : 0
// barcolor(cond5 ? color.yellow : na)
