Repaints Sethide() for ZigZag Line

Repaints

british43

Member
VIP
I've been trying to use sethiding() to hide ZigZag plot that does not fall within a date range I specify but for some reason the plot will not hide in the specific date range set in the script. I duplicated the TOS zigzag script and added a date range to it. Is there a better way to do this?
 
@Svanoy Here is the code

Ruby:
def priceH = high;
def priceL =low;
def priceC = close;


def o = open;
def h = high;
def l = low;
def c = close;

input percentageReversal = 2;
input absoluteReversal = 0.0;
input atrLength = 2;
input atrReversal = 1;
input tickReversal = 0;

Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(tickReversal >= 0, "'ticks' must not be negative: " + tickReversal);
Assert(percentageReversal != 0 or absoluteReversal != 0 or atrReversal != 0 or tickReversal != 0, "Either 'percentage reversal' or 'absolute reversal' or 'atr reversal' or 'tick reversal' must not be zero");

def absReversal;
if (absoluteReversal != 0) {
    absReversal = absoluteReversal;
} else {
    absReversal =  tickReversal * TickSize();
}

def hlPivot;
if (atrReversal != 0) {
    hlPivot = percentageReversal / 100 + WildersAverage(TrueRange(high, close, low), atrLength) / close * atrReversal;
} else {
    hlPivot = percentageReversal / 100;
}


def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot - absReversal {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }
} else {
    if priceH >= prevMinL + prevMinL * hlPivot + absReversal {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}



def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - barNumber + 1;
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH) do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}


def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL) do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}


plot ZZ;
if barNumber == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP) do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH else Double.NaN;
} else if barNumber == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}

ZZ.SetDefaultColor(color.white);
ZZ.EnableApproximation();
ZZ.setHiding(between(getYYYYMMDD(), 20220101, 20220215));


input startdate = 20220208;
input enddate = 20220212;

def begin = getyyyyMMDD() >= startdate;
def end = getyyyyMMDD() <= enddate;
 
@british43
The function getyyyyMMDD() returns the date of the current bar, so Sethiding() is being calculated with todays date.
You want to use DayFromDate() and DaysTillDate() to define your date range.

More importantly, SetHiding(), stops the entire plot from displaying. (not what you want).
What you want to do is not calculate a plot if outside the defined date range.
Also, not every bar has a value in the plot of your line so if there are not a minimum of two points to draw a line between during that defined range then there will be no plot to display.

3NsI59M.png


Ruby:
def priceH = high;
def priceL =low;
def priceC = close;

def o = open;
def h = high;
def l = low;
def c = close;

input percentageReversal = 2;
input absoluteReversal = 0.0;
input atrLength = 2;
input atrReversal = 1;
input tickReversal = 0;


input startdate = 20220108;
input enddate = 20220212;
def ActivePeriod = if DaysFromDate(startdate)>=0 and DaysTillDate(enddate) >=0 then yes else Double.NaN;

Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(tickReversal >= 0, "'ticks' must not be negative: " + tickReversal);
Assert(percentageReversal != 0 or absoluteReversal != 0 or atrReversal != 0 or tickReversal != 0, "Either 'percentage reversal' or 'absolute reversal' or 'atr reversal' or 'tick reversal' must not be zero");

def absReversal;
if (absoluteReversal != 0) {
    absReversal = absoluteReversal;
} else {
    absReversal =  tickReversal * TickSize();
}

def hlPivot;
if (atrReversal != 0) {
    hlPivot = percentageReversal / 100 + WildersAverage(TrueRange(high, close, low), atrLength) / close * atrReversal;
} else {
    hlPivot = percentageReversal / 100;
}


def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot - absReversal {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }
} else {
    if priceH >= prevMinL + prevMinL * hlPivot + absReversal {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}



def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - barNumber + 1;
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH) do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}


def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL) do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}


plot ZZ;
if IsNaN(activeperiod){
zz = double.nan;
}else if barNumber == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP) do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH else Double.NaN;
} else if barNumber == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}

ZZ.SetDefaultColor(color.white);
ZZ.EnableApproximation();

It would probably be possible to plot what would appear to be line segments by defining a plot using the bar numbers and high/low where 'zz' is not Double.NaN and drawing a line based on a slope calculated from the rise and run. then there would be a value at every bar for the plotted line and then could use SetHiding() and see what would appear to be a line running from or to a point outside the defined date range.
 
Last edited:
@Svanoy Works like a champ. Thanks! I'm trying to use Support and Resistance to plot Wyckoff trading range but the lines are dynamic so even if I label each line with a chartbubble it changes as the chart goes on. I'm looking at DIS date range 20211028 to present. I think DIS is in an accumulation phase.


qn9kHQF.png
 
@british43
As with your chart bubbles, any line plotted to automaticaly at a reversal point defined in the study would change.
There would have to be a defined plot variable for each reversal point, the number of which would be unknown beforehand.
The only way would be to pick a date range and manually plot each reversal with a static plot.
 
@Svanoy Works like a champ. Thanks! I'm trying to use Support and Resistance to plot Wyckoff trading range but the lines are dynamic so even if I label each line with a chartbubble it changes as the chart goes on. I'm looking at DIS date range 20211028 to present. I think DIS is in an accumulation phase.

here is one way to draw horizontal lines , from the 3 most recent peaks and valleys.
actually, it draws 5. it is determining the last bar as one of them.

this is based on the original ZigZagHighLow study, not a modified one.

this counts the peaks and valleys, then uses reverse counters to determine the 3 most recent.

as svanoy said, you have to have a plot command for every line. and we don't know how many will be needed.
there is a test label that can be turned on to display the quantity of peaks and valleys.


Ruby:
# zigzag_horz_sr_lines_0

# i used the original zigzag study, not a modified one

# draw horz lines from the 3 most recent peaks and valleys
#  use reverse counters to determine most recent

# ---------------------------------

# ZigZagHighLow
# TD Ameritrade IP Company, Inc. (c) 2013-2022

input priceH = high;
input priceL = low;
input percentageReversal = 5.0;
input absoluteReversal = 0.0;
input atrLength = 5;
input atrReversal = 1.5;
input tickReversal = 0;

Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(tickReversal >= 0, "'ticks' must not be negative: " + tickReversal);
Assert(percentageReversal != 0 or absoluteReversal != 0 or atrReversal != 0 or tickReversal != 0, "Either 'percentage reversal' or 'absolute reversal' or 'atr reversal' or 'tick reversal' must not be zero");

def absReversal;
if (absoluteReversal != 0) {
    absReversal = absoluteReversal;
} else {
    absReversal =  tickReversal * TickSize();
}

def hlPivot;
if (atrReversal != 0) {
    hlPivot = percentageReversal / 100 + WildersAverage(TrueRange(high, close, low), atrLength) / close * atrReversal;
} else {
    hlPivot = percentageReversal / 100;
}
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot - absReversal {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }
} else {
    if priceH >= prevMinL + prevMinL * hlPivot + absReversal {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}

def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - barNumber + 1;
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH) do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}

def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL) do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}

plot ZZ;
if barNumber == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP) do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH else Double.NaN;
} else if barNumber == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(GetColor(1));
ZZ.EnableApproximation();

# ---------------------------------

# draw horz lines from the 3 most recent peaks and valleys
#  use reverse counters to determine most recent

def na = double.nan;
def bn = BarNumber();
def lastbn = HighestAll( if !IsNaN(close) then bn else 0);

# zz is not error on peaks and valleys, price level,  of high or low
#addchartbubble(1, low, zz, color.yellow, no);

# convert na to 0, so following formulas dont err
def zz2 = if isnan(zz) then 0 else zz;

# count peaks and valleys
# peaks , R levels
# valleys, S levels
def rcnt = if bn == 1 then 0 else if zz2 == high then rcnt[1] + 1 else rcnt[1];
def scnt = if bn == 1 then 0 else if zz2 == low then scnt[1] + 1 else scnt[1];

def rttl = HighestAll(rcnt);
def sttl = HighestAll(scnt);

# rev counts
def revrcnt = rttl - rcnt + 1;
def revscnt = sttl - scnt + 1;

# ---------------------------

# horz lines

def r1_line = if bn == 1 then na else if revrcnt == 1 and zz2 == high then high else r1_line[1];
def r2_line = if bn == 1 then na else if revrcnt == 2 and zz2 == high then high else r2_line[1];
def r3_line = if bn == 1 then na else if revrcnt == 3 and zz2 == high then high else r3_line[1];

def s1_line = if bn == 1 then na else if revscnt == 1 and zz2 == low then low else s1_line[1];
def s2_line = if bn == 1 then na else if revscnt == 2 and zz2 == low then low else s2_line[1];
def s3_line = if bn == 1 then na else if revscnt == 3 and zz2 == low then low else s3_line[1];

plot z1 = r1_line;
plot z2 = r2_line;
plot z3 = r3_line;
z1.setdefaultcolor(color.pink);
z1.SetStyle(Curve.MEDIUM_DASH);
# z1.setlineweight(1);
z1.hidebubble();

z2.setdefaultcolor(color.pink);
z2.SetStyle(Curve.MEDIUM_DASH);
# z2.setlineweight(1);
z2.hidebubble();

z3.setdefaultcolor(color.pink);
z3.SetStyle(Curve.MEDIUM_DASH);
# z3.setlineweight(1);
z3.hidebubble();

plot w1 = s1_line;
plot w2 = s2_line;
plot w3 = s3_line;

w1.setdefaultcolor(color.lime);
w1.SetStyle(Curve.MEDIUM_DASH);
# w1.setlineweight(1);
w1.hidebubble();

w2.setdefaultcolor(color.lime);
w2.SetStyle(Curve.MEDIUM_DASH);
# w2.setlineweight(1);
w2.hidebubble();

w3.setdefaultcolor(color.lime);
w3.SetStyle(Curve.MEDIUM_DASH);
# w3.setlineweight(1);
w3.hidebubble();

# ----------------------------

input test1_labels = no;
addlabel(test1_labels, "Rs " + rttl + " peaks", color.red);
addlabel(1, "Ss " + sttl + " valleys", color.green);

input test2_bubbles = no;
addchartbubble(test2_bubbles, low*0.98,
zz2 + "\n" +
high + "\n" +
low + "\n" +
rcnt + " r\n" +
revrcnt + " revr\n" +
scnt + " s\n" +
revscnt + "revs\n"
, ( if !isnan(zz) then color.yellow else color.gray), no);

#
 
@halcyonguy and @Svanoy This helps a lot. How would I go about extending the lines?

replace these 6 code lines with the ones below


to extend the lines, add this
if isnan(zz) then r1_line[1] else

Code:
# horz lines

def r1_line = if isnan(zz) then r1_line[1] else if bn == 1 then na else if revrcnt == 1 and zz2 == high then high else r1_line[1];
def r2_line = if isnan(zz) then r2_line[1] else if bn == 1 then na else if revrcnt == 2 and zz2 == high then high else r2_line[1];
def r3_line = if isnan(zz) then r3_line[1] else if bn == 1 then na else if revrcnt == 3 and zz2 == high then high else r3_line[1];

def s1_line = if isnan(zz) then s1_line[1] else if bn == 1 then na else if revscnt == 1 and zz2 == low then low else s1_line[1];
def s2_line = if isnan(zz) then s2_line[1] else if bn == 1 then na else if revscnt == 2 and zz2 == low then low else s2_line[1];
def s3_line = if isnan(zz) then s3_line[1] else if bn == 1 then na else if revscnt == 3 and zz2 == low then low else s3_line[1];
 
Are you looking for something like this?
It's not the cleanest look in the world but I believe it's what you are looking for.

Code:
#ZigZag
#TomSK

def PercentAmount = .01;
def RevAmount = .05;
def ATRreversal = 3.0;
def ATRlength = 5;

def zz = ZigZagHighLow("price h" = high, "price l" = low, "percentage reversal" = PercentAmount,
"absolute reversal" = RevAmount, "atr length" = ATRlength, "atr reversal" = ATRreversal);

def zzSave = if !IsNaN(zz) then zz else GetValue(zzSave, 1);
def chg = (if zzSave == high then high else low) - GetValue(zzSave, 1);
def isUp = chg >= 0;

# Zig Zag Plot

plot zzp = if isUp <= 1 then zz else Double.NaN;
zzp.AssignValueColor(if isUp then Color.Green else if !isUp then Color.Red else Color.Dark_Orange);
zzp.SetStyle(Curve.FIRM);
zzp.EnableApproximation();
zzp.HideBubble();

# Horizontal Lines

plot CrossZZBull = !isUP[1] and isup;
CrossZZBull.setPaintingStrategy(paintingStrategy.booLEAN_ARROW_UP);
CrossZZBull.setdefaultColor(color.cyan);
plot CrossZZBear = isUP[1] and !isup;
CrossZZBear.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
CrossZZBear.setdefaultColor(color.magenta);

#FFT/NFT
def SupportZZ = if CrossZZBull then low else SupportZZ[1];
def ResistZZ = if CrossZZBear then high else ResistZZ[1];

plot SZZ = SupportZZ;
plot RZZ = ResistZZ;

#Baseline
Def BaseSwitch = if CrossZZBull then low else if CrossZZBear then high else baseSwitch[1];
def BaselineSZZ = BaseSwitch;
#BaselineSZZ.setdefaultcolor (color.cyan);
 
Thanks for share this script. @Svanoy and @halcyonguy I'm trying to see the trend for specific bars between a date range and hide all other bars. I tried using the ActivePeriod but didn't work. Do I need to mess with the trend defs an how would like all the other bars?



Ruby:
#VSA

input startdate = 20210719;
input enddate = 20210620;

def ActivePeriod = if DaysFromDate(startdate)>=0 and DaysTillDate(enddate) >=0 then yes else Double.NaN;

def vbegin = getyyyyMMDD() == startdate;
def vend = getyyyyMMDD() == enddate;

AddVerticalLine(vbegin,"Range Starts",Color.GREEN,Curve.firm);
AddVerticalLine(vend,"Range Ends",Color.GREEN,Curve.firm);

input trendText = {false, default true};

#######
# Calculations

# Trend Definitions
rec fiveDaysSma = compoundValue(5, Average(close, 5)[0], Double.NaN);
def LongTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 40)[0];
def MiddleTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 15)[0];
def ShortTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 5)[0];

#LABELS

AddLabel(trendText, Concat("",Concat("Short Term Trend is ", if (ShortTermTrendSlope > 0) then "Up" else "Down")), Color.dark_orange);
AddLabel(trendText, Concat("Midterm Trend is ", if (MiddleTermTrendSlope > 0) then "Up" else "Down"), Color.green);
AddLabel(trendText, Concat("Long term is ", if (LongTermTrendSlope > 0) then "Up" else "Down"), Color.lime);
 
Last edited:
Thanks for share this script. @Svanoy and @halcyonguy I'm trying to see the trend for specific bars between a date range and hide all other bars. I tried using the ActivePeriod but didn't work. Do I need to mess with the trend defs an how would like all the other bars?



Ruby:
#VSA

input startdate = 20210719;
input enddate = 20210620;

def ActivePeriod = if DaysFromDate(startdate)>=0 and DaysTillDate(enddate) >=0 then yes else Double.NaN;

def vbegin = getyyyyMMDD() == startdate;
def vend = getyyyyMMDD() == enddate;

AddVerticalLine(vbegin,"Range Starts",Color.GREEN,Curve.firm);
AddVerticalLine(vend,"Range Ends",Color.GREEN,Curve.firm);

input trendText = {false, default true};

#######
# Calculations

# Trend Definitions
rec fiveDaysSma = compoundValue(5, Average(close, 5)[0], Double.NaN);
def LongTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 40)[0];
def MiddleTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 15)[0];
def ShortTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 5)[0];

#LABELS

AddLabel(trendText, Concat("",Concat("Short Term Trend is ", if (ShortTermTrendSlope > 0) then "Up" else "Down")), Color.dark_orange);
AddLabel(trendText, Concat("Midterm Trend is ", if (MiddleTermTrendSlope > 0) then "Up" else "Down"), Color.green);
AddLabel(trendText, Concat("Long term is ", if (LongTermTrendSlope > 0) then "Up" else "Down"), Color.lime);

Here are 2 options for not displaying price bars outside the activeperiod.

The first is #assignpricecolor() where the bars outsiide the activeperiod are colored the same as the chart background.

The second one only displays the activperiod bars and hides the others through hiding all the price bars and using addchart() to display the activeperiod bars

The from and to dates are changed below to create the active period

Capture.jpg
Ruby:
#VSA

input startdate = 20210719;
input enddate = 20210820;

def ActivePeriod = if DaysFromDate(startdate)>=0 and DaysTillDate(enddate) >=0 then yes else Double.NaN;

def vbegin = getyyyyMMDD() == startdate;
def vend = getyyyyMMDD() == enddate;

AddVerticalLine(vbegin,"Range Starts",Color.GREEN,Curve.firm);
AddVerticalLine(vend,"Range Ends",Color.GREEN,Curve.firm);

input trendText = {false, default true};

#######
# Calculations

# Trend Definitions
rec fiveDaysSma = compoundValue(5, Average(close, 5)[0], Double.NaN);
def LongTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 40)[0];
def MiddleTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 15)[0];
def ShortTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 5)[0];

#LABELS

AddLabel(trendText, Concat("",Concat("Short Term Trend is ", if (ShortTermTrendSlope > 0) then "Up" else "Down")), Color.dark_orange);
AddLabel(trendText, Concat("Midterm Trend is ", if (MiddleTermTrendSlope > 0) then "Up" else "Down"), Color.green);
AddLabel(trendText, Concat("Long term is ", if (LongTermTrendSlope > 0) then "Up" else "Down"), Color.lime);

#assignpriceColor(if !isnan(activeperiod) then color.current else color.black);

input active_candles_only = yes;
hidepricePlot(active_candles_only);

input charttype=chartType.CANDLE;
def o = if active_candles_only and isnan(activeperiod) then double.nan else open;
def h = if active_candles_only and isnan(activeperiod) then double.nan else high;
def l = if active_candles_only and isnan(activeperiod) then double.nan else low;
def c = if active_candles_only and isnan(activeperiod) then double.nan else close;

def o1 = if o<c then o else double.nan;
def c1 = if o<c then c else double.nan;
def h1 = if o<c then h else double.nan;
def l1 = if o<c then l else double.nan;

AddChart(growColor = color.green, fallColor = color.red, neutralColor = color.gray, high = h1, low = l1, open = c1, close = o1, type = chartType);

def o2 = if o >=c then o else double.nan;
def h2 = h;
def l2 = l;
def c2 = if o >=c then c else double.nan;
AddChart(growColor = color.red, fallColor = color.green, neutralColor = color.gray, high = h2, low = l2, open = o2, close = c2, type = chartType);
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
410 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top