declare upper;
declare hide_on_daily;
input WeeksAgo = 1;#hint WeeksAgo: Excludes today
input PlotHigh = yes;
input PlotLow = yes;
input PlotClose = yes;
input PlotOpen = yes;
input ShowBubbles = no;
def AdjWeeksAgo = WeeksAgo + 1;#Adjusted to match a true LastDate which includes today
def week = GetWeek();
def lastWeek = GetLastWeek();
def year = GetYear();
def lastYear = GetLastYear();
def yyyymmdd = GetYYYYMMDD();
def lastDate = HighestAll( if week == lastWeek and year == lastYear then yyyymmdd else Double.NaN );
def currentDate = if yyyymmdd < lastDate then yyyymmdd else lastDate;
def tradingWeeksAgo = CountTradingDays( currentDate, lastDate );
def previousWeekPlus = tradingWeeksAgo <= AdjWeeksAgo;
def today = lastDate == currentDate;
script previousPlot {
input today = no;
input previousWeekPlus = no;
input data = high;
plot previousPlot = if previousWeekPlus then
HighestAll(if today then data else Double.NaN)
else Double.NaN;
}
plot previousHigh = previousPlot(today, plotHigh and previousWeekPlus, high( period = "WEEK" )[WeeksAgo]);
plot PreviousLow = previousPlot(today, plotLow and previousWeekPlus, low( period = "WEEK" )[WeeksAgo]);
plot PreviousClose = previousPlot(today, plotClose and previousWeekPlus, close( period = "WEEK" )[WeeksAgo]);
plot PreviousOpen = previousPlot(today, plotOpen and previousWeekPlus, open( period = "WEEK" )[WeeksAgo]);
#=========== Chart Bubbles ===================
def FirstBar = ShowBubbles and today != today[1];
addchartbubble(FirstBar, PreviousHigh, ("High of " + WeeksAgo + " week(s) ago"), color = PreviousHigh.takeValueColor());
addchartbubble(FirstBar, PreviousLow, ("Low of " + WeeksAgo + " week(s) ago"), color = PreviousLow.takeValueColor());
addchartbubble(FirstBar, PreviousClose, ("Close of " + WeeksAgo + " week(s) ago"), color = PreviousClose.takeValueColor());
addchartbubble(FirstBar, PreviousOpen, ("Open of " + WeeksAgo + " week(s) ago"), color = PreviousOpen.takeValueColor());