MA 3:1 & CZs
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EJC1453
//
// MA 3:1 & Consolidation Zones
// Tres Medias Móviles y Zonas de Consolidación en un único Indicador.
// By Dev. Camilotto Ezequiel J.
//
//@version=4
//================================================================================
//////////////////////////////////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////////////////////////////////////////
//=============================| MEDIAS MÓVILES 3:1 |=============================

study(shorttitle="MA 3:1 & Consolidation Zones", title="MA 3:1 & CZs", overlay=true)
m1 = input(20, minval=1)
m2 = input(50, minval=1)
m3 = input(200, minval=1)

src = input(close, title="Source")

media_1 = sma(src, m1)
media_2 = sma(src, m2)
media_3 = sma(src, m3)

plot(media_1, color=#A5D6A7)
plot(media_2, color=#F48FB1)
plot(media_3, color=#FFEE58)
//================================================================================
//////////////////////////////////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////////////////////////////////////////
//============================| CONSOLIDATION ZONES |=============================

prd = input(defval = 10, title="Loopback Period", minval = 2, maxval = 50)
conslen = input(defval = 5, title="Min Consolidation Length", minval = 2, maxval = 20)
paintcons = input(defval = true, title = "Paint Consolidation Area ")
zonecol = input(defval = color.new(color.blue, 70), title = "Zone Color")

float hb_ = highestbars(prd) == 0 ? high : na
float lb_ = lowestbars(prd) == 0 ? low : na
var int dir = 0
float zz = na
float pp = na

dir := iff(hb_ and na(lb_), 1, iff(lb_ and na(hb_), -1, dir))
if hb_ and lb_
    if dir == 1
        zz := hb_
    else
        zz := lb_
else
    zz := iff(hb_, hb_, iff(lb_, lb_, na))

for x = 0 to 1000
    if na(close) or dir != dir[x]
        break
    if zz[x]
        if na(pp)
            pp := zz[x]
        else
            if dir[x] == 1 and zz[x] > pp
                pp := zz[x]
            if dir[x] == -1 and zz[x] < pp
                pp := zz[x]

var int conscnt = 0
var float condhigh = na
var float condlow = na
float H_ = highest(conslen)
float L_ = lowest(conslen)
var line upline = na
var line dnline = na
bool breakoutup = false
bool breakoutdown = false

if change(pp)
    if conscnt > conslen
        if pp > condhigh
            breakoutup := true
        if pp < condlow
            breakoutdown := true
    if conscnt > 0 and pp <= condhigh and pp >= condlow
        conscnt := conscnt + 1
    else
        conscnt := 0
else
    conscnt := conscnt + 1

if conscnt >= conslen
    if conscnt == conslen
        condhigh := H_
        condlow  := L_
    else
        line.delete(upline)
        line.delete(dnline)
        condhigh := max(condhigh, high)
        condlow := min(condlow, low)
    
    upline := line.new(bar_index, condhigh, bar_index - conscnt, condhigh, color = color.lime, style = line.style_dashed)
    dnline := line.new(bar_index, condlow, bar_index - conscnt, condlow, color = color.red, style = line.style_dashed)
fill(plot(condhigh, color = na, style = plot.style_stepline),
   plot(condlow, color = na, style = plot.style_stepline),
   color = paintcons and conscnt > conslen ? zonecol : color.new(color.white, 100))

alertcondition(breakoutup, title='Breakout Up', message='Breakout Up')
alertcondition(breakoutdown, title='Breakout Down', message='Breakout Down')
//================================================================================
////////////////////////////////////////////////////////////////////////////////////////////////////


