The zigzag uses a moving average for pivots to minimize repainting. If there are not enough bars to compute the moving average, then the prior version's logic failed. This uses the prior pivot if the last pivot is within the moving average.
Code:
#Anchored_LRL_using_ZigZag_Anchor
#ZigZag Basis
#Since Priceh/Pricel are based upon a movingAverage to minimize repainting
input atrreversal = 2.0;
input ma_length = 5;
input atr_length = 5;
def priceh = MovingAverage(AverageType.EXPONENTIAL, high, ma_length);
def pricel = MovingAverage(AverageType.EXPONENTIAL, low , ma_length);
def EIL = ZigZagHighLow("price h" = priceh, "price l" = pricel, "percentage reversal" = .01, "absolute reversal" = .05, "atr length" = atr_length, "atr reversal" = atrreversal).lastL;
def EIH = ZigZagHighLow("price h" = priceh, "price l" = pricel, "percentage reversal" = .01, "absolute reversal" = .05, "atr length" = atr_length, "atr reversal" = atrreversal).lastH;
def lastbar = HighestAll(if !IsNaN(close) then BarNumber() else 0);
def signalhigh = if !IsNaN(EIH) then BarNumber() else signalhigh[1];
def signallow = if !IsNaN(EIL) then BarNumber() else signallow[1];
def lasthigh = if (lastbar - signalhigh) >= ma_length then signalhigh else 0;
def lastlow = if (lastbar - signallow) >= ma_length then signallow else 0;
def cond = Max(lasthigh, lastlow);
input header_label = yes;
AddLabel(header_label, "Anchored LRL @Last Pivot " + (if cond == signalhigh then "High" else "Low"), Color.WHITE);
input test = no;
AddLabel(test, lastbar + " " + signalhigh + " " + (signallow) + " " + cond);
################
# Time Anchored Regression Channel
# Mobius
# V01.01.2016
#Modified to use zigzagHighLow as a cond anchor
#08:35 Mobius: Here's the anchored Regression Channel I use
#02.12.2019 10:49 Mobius: Just a note to the Anchored LRC. I usually change to 930 from 1800 about 11:00 EST. I don't change to 1800 till 2000 if I still have the program up and running. I could program it to do that automatically but am too lazy.
input y = close;
input Channel_1_Width = 0.70;
input Channel_2_Width = 1.00;
input LineWeight = 1;
script E
{
input y = close;
input n = 20;
def s = fold i = 0 to n
with j
do j + GetValue(y, i);
plot D = s;
}
# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def x = BarNumber();
def nan = Double.NaN;
def xx = if IsNaN(c[-1]) and !IsNaN(c) then x else xx[1];
## code comment for line immediately above
## thinking from left to right.. xx = 0 until the curren tbar is reached
## at the current bar xx = x
## beyond the current bar xx = xx[1] the value of xx one bar back
def firstBar = if BarNumber() == cond
then x
else firstBar[1];
def S_y = if x == HighestAll(firstBar)
then y
else if x > HighestAll(firstBar)
then S_y[1] + y
else S_y[1];
def S_x = if x == HighestAll(firstBar)
then 1
else if x > HighestAll(firstBar)
then S_x[1] + 1
else S_x[1];
def x0_ = HighestAll(xx) - firstBar;
def x1 = HighestAll(if !IsNaN(y) and IsNaN(y[-1])
then x
else nan);
def x0 = HighestAll(if GetValue(x, -x0_) == x1
then x
else nan);
def x_ = if GetValue(x, -x0_) >= x1
then x - x0
else x_[1];
def Ex = E(x_, x0_);
def Ey = E(y, x0_);
def Exy = E(x_ * y, x0_);
def Exsq = E(Sqr(x_), x0_);
def b = (x0_ * Exy - (Ex * Ey)) / (x0_ * Exsq - (Ex * Ex));
def a = (GetValue(Ey, x - x1) - GetValue(b, x - x1) * GetValue(Ex, x - x1)) / x0_;
def LR = a + (GetValue(b, x - x1) * x_);
def r = Max(h, c[1]) - Min(l, c[1]);
def Er = E(r, x0_) / x0_;
def mean = S_y / S_x;
def SD = Sqrt((1 / S_x) * (E(Sqr(y - mean), S_x)));
# Plots
plot LRL = if x > x1 + 5 then nan else
if x >= x0 # x is defined as barnumber()
then LR
else nan; # from nube
#plot LRL = if x >= x0 # x is defined as barnumber()
# then LR
# else nan; # mobius original
LRL.SetStyle(Curve.FIRM);
LRL.SetLineWeight(LineWeight + 1);
LRL.AssignValueColor(if GetValue(a, x - x1) < LR
then Color.CYAN
else Color.ORANGE);
LRL.HideBubble();
LRL.HideTitle();
plot upper1 = LRL + (HighestAll(SD) * Channel_1_Width);
upper1.SetLineWeight(LineWeight);
upper1.SetDefaultColor(Color.LIGHT_GRAY);
upper1.HideBubble();
upper1.HideTitle();
plot lower1 = LRL - (HighestAll(SD) * Channel_1_Width);
lower1.SetLineWeight(LineWeight);
lower1.SetDefaultColor(Color.LIGHT_GRAY);
lower1.HideBubble();
lower1.HideTitle();
plot upper2 = LRL + (HighestAll(SD) * Channel_2_Width);
upper2.SetLineWeight(LineWeight);
upper2.SetDefaultColor(Color.LIGHT_GRAY);
upper2.HideBubble();
upper2.HideTitle();
plot lower2 = LRL - (HighestAll(SD) * Channel_2_Width);
lower2.SetLineWeight(LineWeight);
lower2.SetDefaultColor(Color.LIGHT_GRAY);
lower2.HideBubble();
lower2.HideTitle();
# End Code Time Anchored Regression Channel
#
Last edited by a moderator: