#
# TD Ameritrade IP Company, Inc. (c) 2013-2023
#
input aggregationPeriod = {default "DAY", "WEEK", "MONTH"};
input length = 25;
input hideR1_S1 = yes;
Assert(length > 0, "'length' should be positive: " + length);
def yyyymmdd = GetYYYYMMDD();
def month = GetYear() * 12 + GetMonth();
def day_number = DaysFromDate(First(yyyymmdd)) + GetDayOfWeek(First(yyyymmdd));
def period;
switch (aggregationPeriod) {
case DAY:
period = CountTradingDays(Min(First(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case WEEK:
period = Floor(day_number / 7);
case MONTH:
period = Floor(month - First(month));
}
def count = CompoundValue(1, if period != period[1] then (count[1] + period - period[1]) % length else count[1], 0);
def start = CompoundValue(1, count < count[1] + period - period[1], yes);
def highValue = if start then Highest(high(period = aggregationPeriod), length)[1] else if highValue[1] != 0 then highValue[1] else Double.NaN;
def lowValue = if start then Lowest(low(period = aggregationPeriod), length)[1] else if lowValue[1] != 0 then lowValue[1] else Double.NaN;
def closeValue = if start then close(period = aggregationPeriod)[1] else closeValue[1];
def range = highValue - lowValue;
plot R5 = (highValue / lowValue) * closeValue;
plot R4 = closeValue + range * (1.1) / 2;
plot R3 = closeValue + range * (1.1) / 4;
plot R2 = closeValue + range * (1.1) / 6;
plot R1 = closeValue + range * (1.1) / 12;
plot S1 = closeValue - range * (1.1) / 12;
plot S2 = closeValue - range * (1.1) / 6;
plot S3 = closeValue - range * (1.1) / 4;
plot S4 = closeValue - range * (1.1) / 2;
plot S5 = (closeValue - (R5 - closeValue));
R1.SetHiding(hideR1_S1);
S1.SetHiding(hideR1_S1);
R5.SetDefaultColor(GetColor(5));
R4.SetDefaultColor(GetColor(5));
R3.SetDefaultColor(GetColor(5));
R2.SetDefaultColor(GetColor(5));
R1.SetDefaultColor(GetColor(5));
S1.SetDefaultColor(GetColor(6));
S2.SetDefaultColor(GetColor(6));
S3.SetDefaultColor(GetColor(6));
S4.SetDefaultColor(GetColor(6));
S5.SetDefaultColor(GetColor(6));
def paintingStrategy = if aggregationPeriod == aggregationPeriod.DAY then PaintingStrategy.POINTS else if aggregationPeriod == aggregationPeriod.WEEK then PaintingStrategy.TRIANGLES else PaintingStrategy.SQUARES;
R5.SetPaintingStrategy(paintingStrategy);
R4.SetPaintingStrategy(paintingStrategy);
R3.SetPaintingStrategy(paintingStrategy);
R2.SetPaintingStrategy(paintingStrategy);
R1.SetPaintingStrategy(paintingStrategy);
S1.SetPaintingStrategy(paintingStrategy);
S2.SetPaintingStrategy(paintingStrategy);
S3.SetPaintingStrategy(paintingStrategy);
S4.SetPaintingStrategy(paintingStrategy);
S5.SetPaintingStrategy(paintingStrategy);
input bubbles = yes;
input bubblemover = 2;
def mover = bubbles and !IsNaN(close[bubblemover + 1]) and IsNaN(close[bubblemover]);
AddChartBubble(mover, R5, "R5", R5.TakeValueColor());
AddChartBubble(mover, R4, "R4", R4.TakeValueColor());
AddChartBubble(mover, R3, "R3", R3.TakeValueColor());
AddChartBubble(mover, R2, "R2", R2.TakeValueColor());
AddChartBubble(mover and hideR1_S1 == no, R1, "R1", R1.TakeValueColor());
AddChartBubble(mover, S5, "S5", S5.TakeValueColor());
AddChartBubble(mover, S4, "S4", S4.TakeValueColor());
AddChartBubble(mover, S3, "S3", S3.TakeValueColor());
AddChartBubble(mover, S2, "S2", S2.TakeValueColor());
AddChartBubble(mover and hideR1_S1 == no, S1, "S1", S1.TakeValueColor());
#