For those of you who like displacing their moving averages to line up better here's a simple hack
to add the displacement back. This way your history lines up nicely but you get a decent prediction of what is not yet displaced.
########################################
# Displaced Extended Moving average
# Displaced Hull MA with an exponential
# extension to make it line up.
# MtxDev 2024 SLC
# Use. 20 and 4 or 10 and 2 give good charts.
# I like running two plots one for low and one for high.#
# that gives a really nice trading range. tighter than
# standard deviation.
########################################
input price = low;
input length = 20;
input displace = 4;
# Calculate moving averages
def HMA = MovingAverage(AverageType.HULL, price, length)[-displace];
def MA = MovingAverage(AverageType.EXPONENTIAL, price, displace);
# set up and calculate the extension starting at the last
# bar of the displaced HMA
def lastBar = HighestAll(if !IsNaN(price[-1]) then BarNumber() else Double.NaN);
def isLastBarOfHMA = BarNumber() == lastBar - displace;
#Shift the bar down to line up with the HMA
def MADiff = if BarNumber() <= lastBar - displace+1 then HMA - MA else MADiff[1];
#calculate the extension
def SMA_Extension = if BarNumber() > lastBar - displace then MA + MADiff else Double.NaN;
#do plotting
plot HMA_Plot = HMA;
HMA_Plot.DefineColor("Up", GetColor(1));
HMA_Plot.DefineColor("Down", GetColor(0));
HMA_Plot.AssignValueColor(if HMA > HMA[1] then HMA_Plot.Color("Up") else HMA_Plot.Color("Down"));
plot SMA_Extension_Plot = SMA_Extension;
SMA_Extension_Plot.SetDefaultColor(GetColor(4)); # Using a visible color to ensure it stands out
SMA_Extension_Plot.SetPaintingStrategy(PaintingStrategy.LINE);
SMA_Extension_Plot.SetLineWeight(2);
SMA_Extension_Plot.HideBubble();
SMA_Extension_Plot.HideTitle();
to add the displacement back. This way your history lines up nicely but you get a decent prediction of what is not yet displaced.
########################################
# Displaced Extended Moving average
# Displaced Hull MA with an exponential
# extension to make it line up.
# MtxDev 2024 SLC
# Use. 20 and 4 or 10 and 2 give good charts.
# I like running two plots one for low and one for high.#
# that gives a really nice trading range. tighter than
# standard deviation.
########################################
input price = low;
input length = 20;
input displace = 4;
# Calculate moving averages
def HMA = MovingAverage(AverageType.HULL, price, length)[-displace];
def MA = MovingAverage(AverageType.EXPONENTIAL, price, displace);
# set up and calculate the extension starting at the last
# bar of the displaced HMA
def lastBar = HighestAll(if !IsNaN(price[-1]) then BarNumber() else Double.NaN);
def isLastBarOfHMA = BarNumber() == lastBar - displace;
#Shift the bar down to line up with the HMA
def MADiff = if BarNumber() <= lastBar - displace+1 then HMA - MA else MADiff[1];
#calculate the extension
def SMA_Extension = if BarNumber() > lastBar - displace then MA + MADiff else Double.NaN;
#do plotting
plot HMA_Plot = HMA;
HMA_Plot.DefineColor("Up", GetColor(1));
HMA_Plot.DefineColor("Down", GetColor(0));
HMA_Plot.AssignValueColor(if HMA > HMA[1] then HMA_Plot.Color("Up") else HMA_Plot.Color("Down"));
plot SMA_Extension_Plot = SMA_Extension;
SMA_Extension_Plot.SetDefaultColor(GetColor(4)); # Using a visible color to ensure it stands out
SMA_Extension_Plot.SetPaintingStrategy(PaintingStrategy.LINE);
SMA_Extension_Plot.SetLineWeight(2);
SMA_Extension_Plot.HideBubble();
SMA_Extension_Plot.HideTitle();