#macd_period_middle
#https://usethinkscript.com/threads/macd-high-variable.19613/
#MACD High Variable
# call out the high of the period of time
# between when
# the MACD crosses under the signal line
# and when it crosses back above.
# MACD
declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;
plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);
plot Diff = Value - Avg;
plot ZeroLine = 0;
plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#----------------------
def na = double.nan;
def bn = barnumber();
# max bars between crossings
def n = 200;
def xup = value crosses above avg;
def xdwn = value crosses below avg;
# when a cross down happens, find highest high, during a period
def hihi;
def hibn;
#def lo;
if bn == 1 then {
hihi = 0;
hibn = 0;
#} else if xup then {
# hihi = 0;
# hibn = 0;
} else if xdwn then {
# find highest high between crossings
hihi = fold a = 1 to n
with b
# loop until a cross up
while getvalue(xup, -a) == 0
do max(b, getvalue(high, -a));
# find the highest high and read the barnumber
hibn = fold c = 1 to n
with d
while getvalue(xup, -c) == 0
do if hihi == getvalue(high,-c) then getvalue(bn,-c) else d;
} else {
hihi = hihi[1];
hibn = hibn[1];
}
# draw vert line on the highest high, during macd period
addverticalline(bn == hibn, "-");
#