Roof and Floors From Actieve Inversiones
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jjsch

//@version=4    
study("Roof and Floors Actieve - Basic ", overlay=true, max_bars_back=1001)//max bars back sirve para que el indicador le diga el maximo de velas que usaremos

barras = input(220, title="Bars", minval= 10, maxval=1000)
//obtener el minimo y obtener el maximo de x numero de periodos
minimo_precio  = lowest(barras) //este es el eje x
minimo_barra = abs(lowestbars(barras)) //este es el eje y

maximo_precio = highest(barras)
maximo_barra = abs(highestbars(barras))

line50 = minimo_precio+ (maximo_precio - minimo_precio) * 0.5 
line61 = minimo_precio+ (maximo_precio - minimo_precio) * 0.618
line38 = minimo_precio+ (maximo_precio - minimo_precio) * 0.382

x2 = max(minimo_barra, maximo_barra)

if barstate.islast  //ATENCION!!! TODA ESTA FORMULA ES PARA CREAR LAS LINEAS DE EXTREMOS DE LOS FIBO
    precio_50 = minimo_precio + (maximo_precio - minimo_precio) * 0.5
    precio_618 = 0.0 //hay que redefinir un numero, por eso usamos este signo
    x = max(minimo_barra, maximo_barra)

    if minimo_barra > maximo_barra
        precio_618 := maximo_precio - (maximo_precio - minimo_precio) * 0.618
    else //tambien
        precio_618 := minimo_precio + (maximo_precio - minimo_precio) * 0.618
        
    erase1=line.new(bar_index[x],minimo_precio, bar_index[0], minimo_precio, color=color.new(color.black,40), style=line.style_solid, extend=extend.both)
    erase2=line.new(bar_index[x],maximo_precio, bar_index[0], maximo_precio, color=color.new(color.black,40), style=line.style_solid, extend=extend.both)
    line.delete(erase1)
    line.delete(erase2)



//{LINEAS Y ETIQUETAS


secmin =security(syminfo.tickerid, "D", minimo_precio)
secmax =security(syminfo.tickerid, "D", maximo_precio)


delmin=line.new(bar_index[x2], secmin, bar_index[0], secmin, width=7 ,color=color.new(color.black,40) , extend= extend.both)
delmax=line.new(bar_index[x2], secmax, bar_index[0], secmax, width=7 ,color=color.new(color.black,40) , extend= extend.both)

line.delete(delmin[1])
line.delete(delmax[1])

la100= label.new(bar_index[200], secmax, text= "     Metal Roof     ", color= color.new(color.black,40), textcolor= color.white )
label.delete(la100[1])

la0= label.new(bar_index[200], secmin, text= "     Metal Floor     ", color= color.new(color.black,40), textcolor= color.white )
label.delete(la0[1])

fhmin =security(syminfo.tickerid, "240", minimo_precio)
fhmax =security(syminfo.tickerid, "240", maximo_precio)

fhdelmin=line.new(bar_index[x2], fhmin, bar_index[0], fhmin, width=5,color=color.new(color.red,40) , extend= extend.both)
fhdelmax=line.new(bar_index[x2], fhmax, bar_index[0], fhmax, width=5,color=color.new(color.red,40) , extend= extend.both)

line.delete(fhdelmin[1])
line.delete(fhdelmax[1])

fhla100= label.new(bar_index[200], fhmax, text= "   Wooden Roof   ", color= color.new(color.red,40), textcolor= color.white )
label.delete(fhla100[1])

fhla0= label.new(bar_index[200], fhmin, text= "   Wooden Floor   ", color= color.new(color.red,40), textcolor= color.white )
label.delete(fhla0[1])

hourmin =security(syminfo.tickerid, "60", minimo_precio)
hourmax =security(syminfo.tickerid, "60", maximo_precio)

hourdelmin=line.new(bar_index[x2], hourmin, bar_index[0], hourmin, width=3 ,color=color.new(color.blue,40) , extend= extend.both)
hourdelmax=line.new(bar_index[x2], hourmax, bar_index[0], hourmax, width=3 ,color=color.new(color.blue,40) , extend= extend.both)

line.delete(hourdelmin[1])
line.delete(hourdelmax[1])

hourla100= label.new(bar_index[200], hourmax, text= "  Plastic Roof  ", color= color.new(color.blue, 40), textcolor= color.white )
label.delete(hourla100[1])

hourla0= label.new(bar_index[200], hourmin, text= "  Plastic Floor  ", color= color.new(color.blue, 40), textcolor= color.white )
label.delete(hourla0[1])


thirtymin =security(syminfo.tickerid, "30", minimo_precio)
thirtymax =security(syminfo.tickerid, "30", maximo_precio)

thirtydelmin=line.new(bar_index[x2], thirtymin, bar_index[0], thirtymin, width= 2,color=color.new(color.green,40) , extend= extend.both)
thirtydelmax=line.new(bar_index[x2], thirtymax, bar_index[0], thirtymax, width= 2,color=color.new(color.green,40) , extend= extend.both)

line.delete(thirtydelmin[1])
line.delete(thirtydelmax[1])

thirtyla100= label.new(bar_index[200], thirtymax, text= "Paper Roof", color= color.new(color.green,40), textcolor= color.white )
label.delete(thirtyla100[1])

thirtyla0= label.new(bar_index[200], thirtymin, text= "Paper Floor", color= color.new(color.green,40), textcolor= color.white )
label.delete(thirtyla0[1])



fivemin =security(syminfo.tickerid, "5", minimo_precio)
fivemax =security(syminfo.tickerid, "5", maximo_precio)

fivedelmin=line.new(bar_index[x2], fivemin, bar_index[0], fivemin, color=color.new(color.silver,10) , extend= extend.both)
fivedelmax=line.new(bar_index[x2], fivemax, bar_index[0], fivemax, color=color.new(color.silver,10) , extend= extend.both)

line.delete(fivedelmin[1])
line.delete(fivedelmax[1])

fivela100= label.new(bar_index[200], fivemax, text= "Cristal Roof", color= color.new(color.silver,10), textcolor= color.white )
label.delete(fivela100[1])

fivela0= label.new(bar_index[200], fivemin, text= "Cristal Floor", color= color.new(color.silver,10), textcolor= color.white )
label.delete(fivela0[1])

//}LINEAS Y ETIQUETAS

//{RUPTURAS

prevFive_min= close < fivemin[20]? line.new(bar_index[1], fivemin[20], bar_index, fivemin[20], 
     color=color.new(color.silver,40), 
     extend=extend.both):na
line.delete(prevFive_min[1])

delPrevFive_min=close < fivemin[20]? label.new(bar_index[10],fivemin[20],
     text="Broken Floor",
     color=color.new(color.silver,40), 
     textcolor= color.white):na
label.delete(delPrevFive_min[1])

prevFive_max = close > fivemax[20]? line.new(bar_index[1], fivemax[20], bar_index, fivemax[20], 
     color=color.new(color.silver,40), 
     extend=extend.both):na
line.delete(prevFive_max[1])

delPrevFive_max = close > fivemax[20]? label.new(bar_index[10],fivemax[20],
     text="Broken Roof",
     textcolor=color.white, 
     color=color.new(color.silver,40)):na
label.delete(delPrevFive_max[1])


prevthirty_min= close < thirtymin[20]? line.new(bar_index[1], thirtymin[20], bar_index, thirtymin[20],
     style=line.style_dashed,
     color=color.new(color.green,40), 
     extend=extend.both,
     width=2):na
line.delete(prevthirty_min[1])

delPrevthirty_min=close < thirtymin[20]? label.new(bar_index[10],thirtymin[20],
     text="Broken Floor",
     color=color.new(color.green,40), 
     textcolor=color.white):na
label.delete(delPrevthirty_min[1])

prevthirty_max = close > thirtymax[20]? line.new(bar_index[1], thirtymax[20], bar_index, thirtymax[20],
     style=line.style_dashed,
     color=color.new(color.green,40), 
     extend=extend.both,
     width=2):na
line.delete(prevthirty_max[1])

delPrevthirty_max = close > thirtymax[20]? label.new(bar_index[10],thirtymax[20],
     text="Broken Roof",
     textcolor=color.white, 
     color=color.new(color.green,40)):na
label.delete(delPrevthirty_max[1])




prevhour_min= close < hourmin[20]? line.new(bar_index[1], hourmin[20], bar_index, hourmin[20],
     style=line.style_dashed,
     color=color.new(color.blue,40),
     extend=extend.both,
     width=3):na
line.delete(prevhour_min[1])

delPrevhour_min=close < hourmin[20]? label.new(bar_index[10],hourmin[20],
     text="Broken Floor",
     color=color.new(color.blue,40),
     textcolor=color.white):na
label.delete(delPrevhour_min[1])

prevhour_max = close > hourmax[20]? line.new(bar_index[1], hourmax[20], bar_index, hourmax[20],
     style=line.style_dashed,
     color=color.new(color.blue,40),
     extend=extend.both,
     width=3):na
line.delete(prevhour_max[1])

delPrevhour_max = close > hourmax[20]? label.new(bar_index[10],hourmax[20],
     text="  Broken Roof  ",
     textcolor=color.white, 
     color=color.new(color.blue,40)):na
label.delete(delPrevhour_max[1])



prevfh_min= close < fhmin[20]? line.new(bar_index[1], fhmin[20], bar_index, fhmin[20],
     style=line.style_dashed,
     color=color.new(color.red,40),
     extend=extend.both,
     width=5):na
line.delete(prevfh_min[1])

delPrevfh_min=close < fhmin[20]? label.new(bar_index[10],fhmin[20],
     text="   Broken Floor   ",
     color=color.new(color.red,40),
     textcolor=color.white):na
label.delete(delPrevfh_min[1])

prevfh_max = close > fhmax[20]? line.new(bar_index[1], fhmax[20], bar_index, fhmax[20],
     style=line.style_dashed,
     color=color.new(color.red,40),
     extend=extend.both,
     width=5):na
line.delete(prevfh_max[1])

delPrevfh_max = close > fhmax[20]? label.new(bar_index[10],fhmax[20],
     text="   Broken Roof   ",
     textcolor=color.white, 
     color=color.new(color.red,40)):na
label.delete(delPrevfh_max[1])



prevsecmin= close < secmin[20]? line.new(bar_index[1], secmin[20], bar_index, secmin[20],
     style=line.style_dashed,
     color=color.new(color.black,40),
     extend=extend.both,
     width=7):na
line.delete(prevsecmin[1])

delPrevsecmin=close < secmin[20]? label.new(bar_index[10],secmin[20],
     text="     Broken Floor     ",
     color=color.new(color.black,40),
     textcolor=color.white):na
label.delete(delPrevsecmin[1])

prevsecmax = close > secmax[20]? line.new(bar_index[1], secmax[20], bar_index, secmax[20],
     style=line.style_dashed,
     color=color.new(color.black,40),
     extend=extend.both,
     width=7):na
line.delete(prevsecmax[1])

delPrevsecmax = close > secmax[20]? label.new(bar_index[10],secmax[20],
     text="     Broken Roof     ",
     textcolor=color.white, 
     color=color.new(color.black,40)):na
label.delete(delPrevsecmax[1])
//RUPTURAS}

