# $TICK Oscillator with Divergence
# Mobius
# V01.03.2020
declare lower;
input length = 5;
input AvgType = AverageType.HULL;
addLabel(1, "Green Line sloping up or red sloping down is divergence", color.white);
def o = open("$TICK");
def h = high("$TICK");
def l = low("$TICK");
def c = close("$TICK");
def x = BarNumber();
def nan = Double.NaN;
AddChart(h, l, c, o, ChartType.CANDLE, Color.YELLOW);
AddChart(h, l, o, c, ChartType.CANDLE, Color.CYAN);
def RTH = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
GetTime() <= RegularTradingEnd(GetYYYYMMDD());
plot "0" = if !RTH then Double.NaN else 0;
"0".SetDefaultColor(Color.MAGENTA);
"0".HideBubble();
"0".HideTitle();
plot avg = MovingAverage(AvgType, c, length);
avg.AssignValueColor(if avg < 0
then color.red
else color.green);
avg.HideBubble();
avg.HideTitle();
addCloud(0, avg, color.red, color.green);
plot upArrow = if avg crosses above 0
then l
else double.nan;
upArrow.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
upArrow.SetDefaultColor(Color.Green);
upArrow.SetLineWeight(3);
upArrow.HideBubble();
upArrow.HideTitle();
plot dnArrow = if avg crosses below 0
then h
else double.nan;
dnArrow.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
dnArrow.SetDefaultColor(Color.Red);
dnArrow.SetLineWeight(3);
dnArrow.HideBubble();
dnArrow.HideTitle();
def hh = if avg crosses above 0
then h
else if avg > 0 and h > hh[1]
then h
else hh[1];
def xh = if h == hh
then x
else xh[1];
plot hp = if x == highestAll(xh)
then hh
else nan;
hp.SetStyle(Curve.Points);
hp.SetDefaultColor(Color.red);
hp.SetLineWeight(3);
hp.HideBubble();
hp.HideTitle();
def xh1 = if !isNaN(dnArrow)
then getValue(xh, 2)
else xh1[1];
plot hp1 = if x == highestAll(xh1)
then h
else nan;
hp1.SetStyle(Curve.Points);
hp1.SetDefaultColor(Color.red);
hp1.SetLineWeight(3);
hp1.HideBubble();
hp1.HideTitle();
plot hd = if x == highestAll(xh1)
then h
else if x == highestAll(xh)
then h
else nan;
hd.EnableApproximation();
hd.SetDefaultColor(Color.red);
hd.HideBubble();
hd.HideTitle();
def ll = if avg crosses below 0
then l
else if avg < 0 and l < ll[1]
then l
else ll[1];
def xl = if l == ll
then x
else xl[1];
plot lp = if x == highestAll(xl)
then ll
else nan;
lp.SetStyle(Curve.Points);
lp.SetDefaultColor(Color.green);
lp.SetLineWeight(3);
lp.HideBubble();
lp.HideTitle();
def xl1 = if !isNaN(dnArrow)
then xl[1]
else xl1[1];
plot lp1 = if x == highestAll(xl1)
then l
else nan;
lp1.SetStyle(Curve.Points);
lp1.SetDefaultColor(Color.green);
lp1.SetLineWeight(3);
lp1.HideBubble();
lp1.HideTitle();
plot ld = if x == highestAll(xl1)
then l
else if x == highestAll(xl)
then l
else nan;
ld.EnableApproximation();
ld.SetDefaultColor(Color.green);
ld.HideBubble();
ld.HideTitle();
# End Code TICK Oscillator with Divergence
# AI Adds
def lastHP = if x == highestAll(xh)
then xh else lastHP[1];
def lastHP1 = if x == highestAll(xh1)
then xh1 else lastHP[1];
def lastLP = if x == highestAll(xl)
then xl else lastLP[1];
def lastLP1 = if x == highestAll(xl1)
then xl1 else lastLP1[1];
Addlabel(lastHP1<lastHP,"Divergent",color.red);
Addlabel(lastLP1>lastLP,"Divergent",color.green);