# Created by @tony_futures inspired by @traderdaye's quarterly theory concept
# Labels only for upper study
declare hide_on_daily;
DefineGlobalColor("divider", Color.WHITE);
DefineGlobalColor("a", Color.DARK_GRAY);
DefineGlobalColor("m", CreateColor(223,83,107));#Color.DARK_RED);
DefineGlobalColor("d", CreateColor(97,208,79));#Color.DARK_GREEN);
DefineGlobalColor("x", CreateColor(34,151,230));#Color.BLUE);
def DayOfWeek = getDayOfWeek(getYyyyMmDd());
# days of month
# 1-7 1st week
# 8-14 2nd
# 15-21 3rd
# 22-28 4th
# weekly/daily stuff
def first_week = 1;
def second_week = 7;
def third_week = 14;
def fourth_week = 21;
def last_week = 28;
def daymo = GetDayofMonth( GetYyyyMmDd() );
def weekNumber = if dayOfWeek == 1 and daymo >= first_week and daymo <= second_week then 1 else if dayOfWeek == 1 and daymo > second_week and daymo <= third_week then 2 else if dayOfWeek == 1 and daymo > third_week and daymo <= fourth_week then 3 else if dayOfWeek == 1 and daymo <= last_week then 4 else if dayOfWeek == 1 then 0 else weekNumber[1];
def whichWeek = weekNumber;
############
# 6hr/90m stuff
def Midnight = 0000; #Midnight Open
def six = 0600;
def noon = 1200;
def eighteen = 1800;
def currentAgg = getAggregationPeriod() / 60000;
def bn = barNumber();
def isMidnight2 = SecondsFromTime(Midnight) >= 0;
def isSix2 = SecondsFromTime(six) >= 0;
def isNoon2 = SecondsFromTime(noon) >= 0;
def isEighteen = SecondsFromTime(eighteen) >= 0;
# 6hr sections
def isQ2_6 = isMidnight2 and !isSix2;
def isQ3_6 = isSix2 and !isNoon2;
def isQ4_6 = isNoon2 and !isEighteen;
def isQ1_6 = isEighteen;
# 90m sections
def DividerTF1 = RoundDown((90 / currentAgg),0);
def qStart_90 = if (isQ1_6 and !isQ1_6[1]) OR (isQ2_6 and !isQ2_6[1]) OR (isQ3_6 and !isQ3_6[1]) OR (isQ4_6 and !isQ4_6[1]) then bn else qStart_90[1];
def Q1 = bn >= qStart_90 and bn < (qStart_90 + DividerTF1);
def Q2 = bn >= (qStart_90 + DividerTF1) and bn < (qStart_90 + (2 * DividerTF1));
def Q3 = bn >= (qStart_90 + (2 * DividerTF1)) and bn < (qStart_90 + (3 * DividerTF1));
def Q4 = bn >= (qStart_90 + (3 * DividerTF1));
# ------------------------------------
input showWeeklyLabels = yes;
AddLabel(showWeeklyLabels and whichWeek == 1, "Weekly Accumulation", GlobalColor("a"));
AddLabel(showWeeklyLabels and whichWeek == 2, "Weekly Manipulation", GlobalColor("m"));
AddLabel(showWeeklyLabels and whichWeek == 3, "Weekly Distribution", GlobalColor("d"));
AddLabel(showWeeklyLabels and whichWeek == 4, "Weekly Reversal/Continuation", GlobalColor("x"));
input showDailyLabels = yes;
AddLabel(showDailyLabels and dayOfWeek == 1, "Daily Accumulation", GlobalColor("a"));
AddLabel(showDailyLabels and dayOfWeek == 2, "Daily Manipulation", GlobalColor("m"));
AddLabel(showDailyLabels and dayOfWeek == 3, "Daily Distribution", GlobalColor("d"));
AddLabel(showDailyLabels and dayOfWeek == 4, "Daily Reversal/Continuation", GlobalColor("x"));
input show6hrLabels = yes;
AddLabel(show6hrLabels and isQ1_6, "6hr Accumulation", GlobalColor("a"));
AddLabel(show6hrLabels and isQ2_6, "6hr Manipulation", GlobalColor("m"));
AddLabel(show6hrLabels and isQ3_6, "6hr Distribution", GlobalColor("d"));
AddLabel(show6hrLabels and isQ4_6, "6hr Reversal/Continuation", GlobalColor("x"));
input show90mLabels = yes;
AddLabel(show90mLabels and Q1, "90m Accumulation", GlobalColor("a"));
AddLabel(show90mLabels and Q2, "90m Manipulation", GlobalColor("m"));
AddLabel(show90mLabels and Q3, "90m Distribution", GlobalColor("d"));
AddLabel(show90mLabels and Q4, "90m Reversal/Continuation", GlobalColor("x"));