#
# Accumulation_Distribution_Divergence
# Assembled by Kory Gill (@korygill) for BenTen at usethinkscript.com
# Added Previous Day/Week H/L
declare lower;
declare once_per_bar;
input OpenTime = 0930;
input ADL_Label=yes;
input week_vertical_label=yes;
def bn = BarNumber();
def nan = Double.NaN;
def sft = if getaggregationPeriod()==aggregationPeriod.HOUR or getaggregationPeriod()==aggregationPeriod.FOUR_HOURS then secondsfromTime(0900) else SecondsFromTime(OpenTime);
def data = if close > close[1] then close - Min(close[1], low) else if close < close[1] then close - Max(close[1], high) else 0;
def SumData = if bn == 1 then data else SumData[1] + data;
def ADBP = SumData;
def hVal;
def lVal;
if bn == 1
then
{
hVal = nan;
lVal = nan;
}
else if bn == 2
then
{
hVal = ADBP;
lVal = ADBP;
}
else
{
if sft == 0
then
{
hVal = ADBP;
lVal = ADBP;
}
else
{
hVal = Max(hVal[1], ADBP);
lVal = Min(lVal[1], ADBP);
}
}
def pdh = if sft[-1] == 0 then hVal else pdh[1];
def pdl = if sft[-1] == 0 then lVal else pdl[1];
plot ppdh = pdh;
plot ppdl = pdl;
ppdh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ppdl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ppdh.SetDefaultColor(GetColor(1));
ppdl.SetDefaultColor(GetColor(0));
#Previous Week High/Low
#Weeks Defined
def ymd = GetWeek();
def count = if ymd != ymd[1] and !IsNaN(close) then count[1] + 1 else count[1];
def cond = HighestAll(count) - count + 1;
input vertical = yes;
AddVerticalLine(ymd != ymd[1], "", Color.GRAY);
def pwh =
if cond != cond[1] then ADBP
else if cond and ADBP > pwh[1] then ADBP
else pwh[1];
def pwh1 = if cond[-1] != cond then pwh[1] else pwh1[1];
plot ppwh = if cond == cond[-1] then pwh1 else Double.NaN;
ppwh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ppwh.setlineWeight(3);
def pwl =
if cond[1] != cond then adbp
else if cond and adbp < pwl[1] then adbp
else pwl[1];
def pwl1 = if cond[-1] != cond then pwl[1] else pwl1[1];
plot ppwl = if cond == cond[-1] then pwl1 else Double.NaN;
ppwl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ppwl.setlineWeight(3);
input bubbles = no;
input bubblemover = 2;
def mover = bubbles and IsNaN(close[bubblemover]) and !IsNaN(close[bubblemover + 1]);
AddChartBubble(mover, ppdh, "dh", ppdh.TakeValueColor());
AddChartBubble(mover, ppdl, "dl", ppdl.TakeValueColor());
AddChartBubble(mover, ppwh, "wh", ppwh.TakeValueColor());
AddChartBubble(mover, ppwl, "wl", ppwl.TakeValueColor());
plot ADL = ADBP;
AddLabel(ADL_label,"ADL[2]: "+ADL[2],color.DARK_ORANGE);
AddLabel(ADL_label,"ADL [1]: "+ADL[1],color.DARK_ORANGE);
AddLabel(ADL_label,"ADL: "+ADL,color.DARK_ORANGE);
AddVerticalLine(week_vertical_label and cond[1] == 3 and cond == 2, "Prevous Week Start", Color.WHITE);
AddVerticalLine(week_vertical_label and cond[1] == 2 and cond == 1, "Current Week Start", Color.WHITE);
#