# Range Extension of Previous Day Range
# tomsk
# 1.12.2020
# https://usethinkscript.com/threads/plot-25-and-50-of-previous-day%E2%80%99s-range.1472/#post-13757
# V1.0 - 01.11.2020 - tomsk - Range Extension of Previous Day Range
# V1.1 - 01.11.2020 - tomsk - Added plot of previous day range midpoint
# V1.2 - 01.11.2020 - tomsk - Added chart bubbles to identify all plot lines
# V1.3 - 01.12.2020 - tomsk - Added labels and plot lines for previous day's high/low
# This study plots the 25% and 50% extension of the previous day's range
# To be used on intraday charts only
declare hide_on_daily;
input n = 3;
input displayLabel = yes;
def bar = barNumber();
def CurrentDay = GetDay() == GetLastDay();
def prevHigh = if CurrentDay then high(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevLow = if CurrentDay then low(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevRange = if CurrentDay then prevHigh - prevLow else Double.NaN;
plot UExt25 = prevHigh + (prevRange * 0.25);
plot UExt50 = prevHigh + (prevRange * 0.50);
plot LExt25 = prevLow - (prevRange * 0.25);
plot LExt50 = prevLow - (prevRange * 0.50);
plot PrevHigh_ = prevHigh;
plot PrevLow_ = prevLow;
plot PrevMid = prevLow + (prevRange / 2);
UExt25.SetDefaultColor(Color.Cyan);
UExt50.SetDefaultColor(Color.Pink);
LExt25.SetDefaultColor(Color.White);
LExt50.SetDefaultColor(Color.Green);
PrevHigh_.SetDefaultColor(Color.Green);
PrevHigh_.SetStyle(Curve.Short_Dash);
PrevHigh_.SetLineWeight(3);
PrevLow_.SetDefaultColor(Color.Red);
PrevLow_.SetStyle(Curve.Short_Dash);
PrevLow_.SetLineWeight(3);
PrevMid.SetDefaultColor(Color.Yellow);
PrevMid.SetStyle(Curve.Short_Dash);
PrevMid.SetLineWeight(3);
AddLabel(displayLabel, "Previous Day's High = " + prevHigh, Color.Yellow);
AddLabel(displayLabel, "Previous Day's Low = " + prevLow, Color.Pink);
def lastBar = if isNaN(close[-1]) and !isNaN(close) then bar else lastBar[1];
def UExt25Loc = if isNaN(close[-1]) and !isNaN(close) then UExt25 else UExt25Loc[1];
def UExt50Loc = if isNaN(close[-1]) and !isNaN(close) then UExt50 else UExt50Loc[1];
def PrevHighLoc = if isNaN(close[-1]) and !isNaN(close) then PrevHigh else PrevHighLoc[1];
def PrevLowLoc = if isNaN(close[-1]) and !isNaN(close) then PrevLow else PrevLowLoc[1];
def PrevMidLoc = if isNaN(close[-1]) and !isNaN(close) then PrevMid else PrevMidLoc[1];
def LExt25Loc = if isNaN(close[-1]) and !isNaN(close) then LExt25 else LExt25Loc[1];
def LExt50Loc = if isNaN(close[-1]) and !isNaN(close) then LExt50 else LExt50Loc[1];
addChartBubble(bar == HighestAll(lastBar+n), UExt25Loc, "Upper 25%", UExt25.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), UExt50Loc, "Upper 50%", UExt50.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), PrevHighLoc, "Prev Day High", PrevHigh_.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), PrevLowLoc, "Prev Day Low", PrevLow_.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), PrevMidLoc, "Prev Mid", PrevMid.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), LExt25Loc, "Lower 25%", LExt25.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), LExt50Loc, "Lower 50%", LExt50.TakeValueColor());
# End Range Extension of Previous Day Range