Mervaleta Breadth
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FractalTrade15
//@version=5


indicator("Amplitud MERVAL", overlay = false, format = format.percent)

//----------------------------------------------Funciones----------------------------------------------//
f_ma(src, period, ma_type) =>
    ma = ma_type == "SMA"? ta.sma(src,period) : ta.ema(src,period)


//----------------------------------------------Inputs----------------------------------------------//
len = input.int(20, title = "Periodo", group = "Inputs")
matype = input.string("SMA", title = "Tipo de Media", options = ["SMA", "EMA"], group = "Inputs")
bullc = input(color.green, title = "Color 1", group = "Colores")
bearc = input(color.red, title = "Color 2", group = "Colores")

h1 = input.int(85, title = "Banda Superior 1", group = "Bandas")
h2 = input.int(90, title = "Banda Superior 2", group = "Bandas")
l1 = input.int(15, title = "Banda Inferior 1", group = "Bandas")
l2 = input.int(10, title = "Banda Inferior 2", group = "Bandas")


//----------------------------------------------Calculos----------------------------------------------//

[ggal_usd, ggal_ma] = request.security("NASDAQ:GGAL",  timeframe.period,[close, f_ma(close, len, matype)]) //traigo los valores del galicia en dolares y su media movil
ggal_ars = request.security("BCBA:GGAL", timeframe.period,close) //traigo los valores de galicia en pesos
dolar = ggal_ars/ggal_usd*10 //calculo el Dolar CCL

//traigo los valores en de los activos del merval al dolar ccl y sus respectivas medias moviles
[papel_1  , papel_1_ma ]  = request.security("BCBA:YPFD", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_2  , papel_2_ma ]  = request.security("BCBA:TXAR", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_3  , papel_3_ma ]  = request.security("BCBA:TECO2",timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_4  , papel_4_ma ]  = request.security("BCBA:PAMP", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_5  , papel_5_ma ]  = request.security("BCBA:TGSU2",timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_6  , papel_6_ma ]  = request.security("BCBA:BMA",  timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_7  , papel_7_ma ]  = request.security("BCBA:BBAR", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_8  , papel_8_ma ]  = request.security("BCBA:ALUA", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_9  , papel_9_ma ]  = request.security("BCBA:CEPU", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_10 , papel_10_ma]  = request.security("BCBA:LOMA", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_11 , papel_11_ma]  = request.security("BCBA:BYMA", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_12 , papel_12_ma]  = request.security("BCBA:CVH",  timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_13 , papel_13_ma]  = request.security("BCBA:EDN",  timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_14 , papel_14_ma]  = request.security("BCBA:CRES", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_15 , papel_15_ma]  = request.security("BCBA:COME", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)])  
[papel_16 , papel_16_ma]  = request.security("BCBA:HARG", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_17 , papel_17_ma]  = request.security("BCBA:TRAN", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)])  
[papel_18 , papel_18_ma]  = request.security("BCBA:SUPV", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_19 , papel_19_ma]  = request.security("BCBA:MIRG", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_20 , papel_20_ma]  = request.security("BCBA:VALO", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_21 , papel_21_ma]  = request.security("BCBA:TGNO4",timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 
[papel_22 , papel_22_ma]  = request.security("BCBA:AGRO", timeframe.period,[close/dolar, f_ma(close/dolar, len, matype)]) 

//evaluo si cada papel esta por arriba de su media
ggal_up     = ggal_usd > ggal_usd    ?  1 : 0
papel_1_up  = papel_1  > papel_1_ma  ?  1 : 0
papel_2_up  = papel_2  > papel_2_ma  ?  1 : 0
papel_3_up  = papel_3  > papel_3_ma  ?  1 : 0
papel_4_up  = papel_4  > papel_4_ma  ?  1 : 0
papel_5_up  = papel_5  > papel_5_ma  ?  1 : 0
papel_6_up  = papel_6  > papel_6_ma  ?  1 : 0
papel_7_up  = papel_7  > papel_7_ma  ?  1 : 0
papel_8_up  = papel_8  > papel_8_ma  ?  1 : 0
papel_9_up  = papel_9  > papel_9_ma  ?  1 : 0
papel_10_up = papel_10 > papel_10_ma ?  1 : 0
papel_11_up = papel_11 > papel_11_ma ?  1 : 0
papel_12_up = papel_12 > papel_12_ma ?  1 : 0
papel_13_up = papel_13 > papel_13_ma ?  1 : 0
papel_14_up = papel_14 > papel_14_ma ?  1 : 0
papel_15_up = papel_15 > papel_15_ma ?  1 : 0
papel_16_up = papel_16 > papel_16_ma ?  1 : 0
papel_17_up = papel_17 > papel_17_ma ?  1 : 0
papel_18_up = papel_18 > papel_18_ma ?  1 : 0
papel_19_up = papel_19 > papel_19_ma ?  1 : 0
papel_20_up = papel_20 > papel_20_ma ?  1 : 0
papel_21_up = papel_21 > papel_21_ma ?  1 : 0
papel_22_up = papel_22 > papel_22_ma ?  1 : 0

//calculo la amplitud del Merval
merval_breadth = 100*math.avg(papel_1_up,papel_2_up,papel_3_up,papel_4_up,papel_5_up,papel_6_up,papel_7_up,papel_8_up,papel_9_up,papel_10_up,papel_11_up,papel_12_up,papel_13_up,papel_14_up,papel_15_up,papel_16_up,papel_17_up,papel_18_up,papel_19_up,papel_20_up,papel_21_up,papel_22_up,ggal_up)

//----------------------------------------------Graficos----------------------------------------------//
S20UpperBand1 = plot(h1, "S20 Upper Band", color=color.new(bearc,90))
S20UpperBand2 = plot(h2, "S20 Upper Band", color=color.new(bearc,90))
fill(S20UpperBand2,S20UpperBand1, h2, h1,color.new(bearc,97),color.new(bearc,80))

p50sh = plot(50, "S20 Middle Band", color=color.new(color.silver, 97), linewidth = 10)
p50 = plot(50, "S20 Middle Band", color=color.new(color.black, 50))

S20LowerBand1 = plot(l1, "S20 Lower Band", color=color.new(bullc,90))
S20LowerBand2 = plot(l2, "S20 Lower Band", color=color.new(bullc,90))
fill(S20LowerBand1,S20LowerBand2, l1,l2,color.new(bullc,80),color.new(bullc,97))

plot(merval_breadth, color = color.from_gradient(merval_breadth,l2,h2,bearc,bullc))