Market Breadth EMAs V2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © OverexposedPhotoOfADress

//@version=5
indicator("Market Breadth EMAs", overlay=false)

s20 = request.security("S5TW", "D", ohlc4) // 20 Day
s50 = request.security("S5FI", "D", ohlc4) // 50 Day
s100 = request.security("S5OH", "D", ohlc4) // 100 Day
s200 = request.security("S5TH", "D", ohlc4) // 200 Day

//Reversal areas for the "% above 20 EMA"
h1 = hline(0, color=color.new(color.white,100))
h2 = hline(2, color=color.new(color.white,100))
h3 = hline(14, color=color.new(color.white,100))
h4 = hline(21, color=color.new(color.white,100))
h5 = hline(33, color=color.new(color.white,100))
h6 = hline(70, color=color.new(color.white,100))
h7 = hline(78, color=color.new(color.white,100))
h8 = hline(85, color=color.new(color.white,100))
h9 = hline(95, color=color.new(color.white,100))
h10 = hline(100, color=color.new(color.white,100))
h11 = hline(49.5, color=color.new(color.white,100))
h12 = hline(50.5, color=color.new(color.white,100))

//Color background for reversal areas
fill(h1, h5, color=color.new(color.lime,95))
fill(h1, h4, color=color.new(color.lime,95))
fill(h1, h3, color=color.new(color.lime,95))
fill(h1, h2, color=color.new(color.lime,95))
fill(h6, h10, color=color.new(color.red,95))
fill(h7, h10, color=color.new(color.red,95))
fill(h8, h10, color=color.new(color.red,95))
fill(h9, h10, color=color.new(color.red,95))
fill(h11, h12, color=color.new(color.white,75))

hs = (s20 - s20[3])/5 + (s50 - s50[3])/2 +(s100 - s100[3]) + (s200 - s200[3])*2 + 50
avg= s20/5 + s50/2 + s100 + s200*2

//Histogram, strength of the General breadth change (white line) in different timeframes
havg1 = (avg - avg[1]) + 50
hcolor1 = havg1 > 50 ? color.new(color.green,80) : color.new(color.red,80)
plot(havg1, color=hcolor1, linewidth=10, style=plot.style_columns, histbase=50)

havg2 = (avg - avg[4]) + 50
hcolor2 = havg2 > 50 ? color.new(color.green,80) : color.new(color.red,80)
plot(havg2, color=hcolor2, linewidth=10, style=plot.style_columns, histbase=50)

havg3 = (avg - avg[10]) + 50
hcolor3 = havg3 > 50 ? color.new(color.green,80) : color.new(color.red,80)
plot(havg3, color=hcolor3, linewidth=10, style=plot.style_columns, histbase=50)

havg4 = (avg - avg[20]) + 50
hcolor4 = havg4 > 50 ? color.new(color.green,80) : color.new(color.red,80)
plot(havg4, color=hcolor4, linewidth=10, style=plot.style_columns, histbase=50)

//Twitch, Long-Term, and General lines
plot(s20, color=color.green, linewidth=2)
plot(s200, color=color.red, linewidth=2)
plot(avg/4, color=color.white, linewidth=4)