MM Chop Filter Range Boxes
// 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 Range Boxes", shorttitle = "MM Chop Filter Range Boxes", overlay = true) 


show_chop = input.bool(true, "Show")
cae_len = input.int(14, minval=1, title="Length", inline="cae1")
cae_src = input.source(close, "Source", inline="cae1")
ch_ut=input.float(60.0, "Choppy Range Thresholds: Upper", inline = "choppy1") 
ch_lt=input.float(40.0, "Lower", inline = "choppy1")

bout_circ=input.bool(true,"Break-Out Circles",inline="choppy2")
bin_cross=input.bool(true,"Break-In Crosses <- Choppy Range",inline="choppy2")

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 Chart' 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))

var is_chop = false
var float chop_beg_h = na
var float chop_beg_l = na
var box chop_box = na

var chop_cond = false
chop_cond:= cae_rsi >= ch_lt and cae_rsi <= ch_ut

if chop_cond and not is_chop[1]
    is_chop := true
    chop_beg_h:=high
    chop_beg_l:=low
    chop_box := icc and show_chop? box.new(bar_index, chop_beg_h, bar_index, chop_beg_l, bgcolor=color.new(color.orange,70), border_color=color.black):na
else if chop_cond and is_chop[1] and is_chop
    box.set_right(chop_box, bar_index)
    if high>chop_beg_h
        box.set_top(chop_box, high)
        chop_beg_h:=high
    if low<chop_beg_l
        box.set_bottom(chop_box,low)
        chop_beg_l:=low
else
    is_chop:=false
    chop_beg_h:=0.0
    chop_beg_l:=0.0


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)

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")

plotshape(icc and choppy_bup and bout_circ, title="Choppy Range Break-ups",style=shape.circle,location=location.belowbar,color=color.aqua)
plotshape(icc and choppy_bdown and bout_circ,title="Choppy Range Break-downs",style=shape.circle,location=location.abovebar,color=color.red)

plotshape(icc and up_to_choppy and bin_cross, title="Choppy Range Down Break-Ins",style=shape.cross,location=location.abovebar,color=color.aqua)
plotshape(icc and down_to_choppy and bin_cross,title="Choppy Range Up Break-Ins",style=shape.cross,location=location.belowbar,color=color.red)