#High & low of week& previous week Candle Study
input showbubbles = yes;
input bubblemover = 2;
def count = if !IsNaN(close) and GetWeek() != GetWeek()[1] then count[1] + 1 else count[1];
def cond = HighestAll(count) - count + 1;
def na = Double.NaN;
DefineGlobalColor("HW", Color.WHITE);
DefineGlobalColor("LW", Color.WHITE);
DefineGlobalColor("LHW", Color.CYAN);
DefineGlobalColor("HLW", Color.YELLOW);
#Week 1 (Current)
plot hw1 = if cond == 1 then high(period = AggregationPeriod.WEEK) else na;
plot lw1 = if cond == 1 then low(period = AggregationPeriod.WEEK) else na;
hw1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lw1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hw1.SetDefaultColor(GlobalColor("HW"));
lw1.SetDefaultColor(GlobalColor("LW"));
def lhw1 = if IsNaN(close) then lhw1[1] else if high == hw1 then low else lhw1[1];
def hlw1 = if IsNaN(close) then hlw1[1] else if low == lw1 then high else hlw1[1];
plot lowofhighweek1 = if cond == 1 then lhw1 else na;
plot highoflowweek1 = if cond == 1 then hlw1 else na;
lowofhighweek1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highoflowweek1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowofhighweek1.SetDefaultColor(GlobalColor("LHW"));
highoflowweek1.SetDefaultColor(GlobalColor("HLW"));
#Week 2 (Previous)
plot hw2 = if cond == 2 then high(period = AggregationPeriod.WEEK) else na;
plot lw2 = if cond == 2 then low(period = AggregationPeriod.WEEK) else na;
hw2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lw2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hw2.SetDefaultColor(GlobalColor("HW"));
lw2.SetDefaultColor(GlobalColor("LW"));
def lhw2 = if IsNaN(hw2) then lhw2[1] else if high == hw2 then low else lhw2[1];
def hlw2 = if IsNaN(lw2) then hlw2[1] else if low == lw2 then high else hlw2[1];
plot lowofhighweek2 = if cond <= 2 then lhw2[1] else na;
plot highoflowweek2 = if cond <= 2 then hlw2 else na;
lowofhighweek2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highoflowweek2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowofhighweek2.SetDefaultColor(GlobalColor("LHW"));
highoflowweek2.SetDefaultColor(GlobalColor("HLW"));
### Bubbles
def b = bubblemover;
def mover = showbubbles and IsNaN(close[b]) and !IsNaN(close[b + 1]);
AddChartBubble(mover, lowofhighweek1, "LHW1 " + AsText(lowofhighweek1), lowofhighweek1.TakeValueColor());
AddChartBubble(mover, highoflowweek1, "HLW1 " + AsText(highoflowweek1), highoflowweek1.TakeValueColor());
AddChartBubble(mover, lowofhighweek2, "LHW2 " + AsText(lowofhighweek2), lowofhighweek2.TakeValueColor());
AddChartBubble(mover, highoflowweek2, "HLW1 " + AsText(highoflowweek2), highoflowweek2.TakeValueColor());
#