# mtf_avg_only_on_same_mtf_00
def na = Double.NaN;
def bn = BarNumber();
# choose when to show avg
input show_2nd_agg_average_only_when_chart_time_matches = yes;
def show_2 = show_2nd_agg_average_only_when_chart_time_matches;
# choose 2nd agg for average
#input average_agg = AggregationPeriod.HOUR;
input average_agg = AggregationPeriod.week;
def avgaggmin = (average_agg / 60000);
# get agg of the chart
def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
# compare chart agg to avg agg
def issameagg = (chartmin == avgaggmin);
def show_avg = if !show_2 then 1 else issameagg;
def cls1 = close(period = average_agg);
#def cls1 = close;
input ma1_len = 10;
input ma1_type = AverageType.simple;
def ma1 = MovingAverage(ma1_type, cls1, ma1_len);
plot z1 = if show_avg then ma1 else na;
z1.SetDefaultColor(color.yellow);
#z1.setlineweight(1);
z1.HideBubble();
input agg_labels = yes;
AddLabel(agg_labels, " ", Color.black);
AddLabel(agg_labels, "Average 2nd agg time " +
(if avgaggmin < 60 then (avgaggmin + " m")
else if avgaggmin < 1440 then ((avgaggmin/60) + " H")
else if avgaggmin < 10080 then (avgaggmin/(60*24) + " Day")
else if average_agg == aggregationPeriod.MONTH then "Month"
else if average_agg == aggregationPeriod.WEEK then "Week"
else "")
, Color.cyan);
AddLabel(agg_labels, "Chart time " +
(if chartmin < 60 then (chartmin + " m")
else if chartmin < 1440 then ((chartmin/60) + " H")
else if chartmin < 10080 then (chartmin/(60*24) + " Day")
else if chartagg == aggregationPeriod.MONTH then "Month"
else if chartagg == aggregationPeriod.WEEK then "Week"
else ""),
(if issameagg then color.green else if !show_2 then Color.YELLOW else color.cyan));
AddLabel(agg_labels and !show_2, "always show avarage", (if !show_2 then Color.YELLOW else color.cyan));
# ---------- ref stuff -------------------------
#https://usethinkscript.com/threads/the-multi-10x-mtf-labels-indicator-for-thinkorswim.1129/page-4#post-69106
# https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
# ..AverageTypes..
# EXPONENTIAL
# HULL
# SIMPLE
# WEIGHTED
# WILDERS
#