#Sorting_MovingAverages_Max_Min
plot ma10 = Average(close, 10);
plot ma20 = Average(close, 20);
plot ma50 = Average(close, 50);
plot ma100 = Average(close, 100);
def maxavg = Max(ma10, Max(ma20, Max(ma50, ma100)));
def minavg = Min(ma10, Min(ma20, Min(ma50, ma100)));
def h = if ma10 == maxavg
then Max(ma20, Max(ma50, ma100)) else
if ma20 == maxavg
then Max(ma10, Max(ma50, ma100)) else
if ma50 == maxavg
then Max(ma10, Max(ma20, ma100))
else Max(ma10, Max(ma20, ma50));
def h1 = if ma10 == maxavg and ma20 == maxavg
then ma20
else Max(ma50, ma100);
def h2 = Max(ma20, minavg);
AddLabel(1, if maxavg == ma10 then "ma10" else
if maxavg == ma20 then "ma20" else
if maxavg == ma50 then "ma50"
else "ma100", Color.YELLOW);
AddLabel(1, if h == ma10 then "ma10" else
if h == ma20 then "ma20" else
if h == ma50 then "ma50"
else "ma100", Color.YELLOW);
AddLabel(1, if Min(h1, h2) == ma10 then "ma10" else
if Min(h1, h2) == ma20 then "ma20" else
if Min(h1, h2) == ma50 then "ma50"
else "ma100", Color.YELLOW);
AddLabel(1, if minavg == ma10 then "ma10" else
if minavg == ma20 then "ma20" else
if minavg == ma50 then "ma50"
else "ma100", Color.YELLOW);
input bubblemover = 6;
def bm = bubblemover;
def bm1 = bm + 1;
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), ma10[bm1], "10: " + ma10[bm1], ma10.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), ma20[bm1], "20: " + ma20[bm1], ma20.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), ma50[bm1], "50: " + ma50[bm1], ma50.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), ma100[bm1], "100: " + ma100[bm1], ma100.TakeValueColor());
#Debug
Input debug = no;
AddChartBubble(debug and IsNaN(close[-1]), low, +maxavg + "\n" + h + "\n" + Min(h1, h2) + "\n" + minavg, Color.WHITE, no);
AddChartBubble(debug and IsNaN(close[-1]), low, ma10 + "\n" + ma20 + "\n" + ma50 + "\n" + ma100, Color.YELLOW, no);
;