I wish I knew I understand what you meant lol. Here's my code. Do you mind incorporating your suggestions?@SJP07
Use a counter to assign a number to each vertical line.
Next, use a fold to send the counter value of the last bar back to the the previous counter value.
Then just add a hide condition in the plot of the vertical line to compare the two values and only show if they are equal.
Hi everyone,
I created a study that plots a vertical line whenever there is a ZigZag reversal. Is there a way to only show the most recent vertical line and hide all the vertical lines that occurred before it?
Thanks in advance!
Ruby:def cond = if close > open then BarNumber() else cond[1]; AddVerticalLine(BarNumber() == HighestAll(cond), "", stroke = Curve.FIRM);
Here is an example of one way to do it.
Ruby:input show_vertical_line = yes; #AddVerticalLine((Up_Trend && show_vertical_line), "Bullish", Color.Dark_Green, Curve.FIRM); #AddVerticalLine((Down_Trend && show_vertical_line), "Bearish", Color.Dark_Red, Curve.FIRM); def cond = if Up_Trend or Down_Trend then BarNumber() else cond[1]; AddVerticalLine(show_vertical_line and BarNumber() == HighestAll(cond), "", if Up_Trend then Color.GREEN else Color.RED, stroke = Curve.FIRM);
input price = close;
input reversalAmount = 1.0;
input showBubbles = no;
input showLabel = no;
Assert(reversalAmount > 0, "'reversal amount' should be positive: " + reversalAmount);
plot "ZZ$" = reference ZigZagHighLow(price, price, 0, reversalAmount, 1, 0);
"ZZ$".Hide();
def zzSave = if !IsNaN("ZZ$") then price else GetValue(zzSave, 1);
def chg = price - GetValue(zzSave, 1);
def isUp = chg >= 0;
def isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue("ZZ$", 1)) and GetValue(isConf, 1));
"ZZ$".EnableApproximation();
"ZZ$".DefineColor("Up Trend", Color.UPTICK);
"ZZ$".DefineColor("Down Trend", Color.DOWNTICK);
"ZZ$".DefineColor("Undefined", Color.DARK_ORANGE);
"ZZ$".AssignValueColor(if !isConf then "ZZ$".Color("Undefined") else if isUp then "ZZ$".Color("Up Trend") else "ZZ$".Color("Down Trend"));
DefineGlobalColor("Unconfirmed", Color.DARK_ORANGE);
DefineGlobalColor("Up", Color.UPTICK);
DefineGlobalColor("Down", Color.DOWNTICK);
def barNumber = BarNumber();
AddChartBubble(showBubbles and !IsNaN("ZZ$") and barNumber != 1, price, chg, if !isConf then GlobalColor("Unconfirmed") else if isUp then GlobalColor("Up") else GlobalColor("Down"), isUp);
AddLabel(showLabel and barNumber != 1, (if isConf then "Confirmed " else "Unconfirmed ") + "ZigZag: " + chg, if !isConf then GlobalColor("Unconfirmed") else if isUp then GlobalColor("Up") else GlobalColor("Down"));
################################################################################
def Up_Trend = isUp[1] is false && isUp is true;
def Down_Trend = isUp[1] is true && isUp is false;
plot ZZ_Buy = Up_Trend;
ZZ_Buy.Hide();
plot ZZ_Sell = Down_Trend;
ZZ_Sell.Hide();
AddLabel(yes, if isUp is true then "Bullish" else "", Color.UPTICK);
AddLabel(yes, if isUp is false then "Bearish" else "", Color.DOWNTICK);
#################################################################################
#################################################################################
def UpTrendCount = if Up_Trend then UpTrendCount[1] + 1 else UpTrendCount[1];
def DnTrendCount = if Down_Trend then DnTrendCount[1] + 1 else DnTrendCount[1];
def LastUpTrendCount = fold lutc=0 to AbsValue(BarNumber()) while !IsNaN(GetValue(close, -lutc)) do GetValue(UpTrendCount, -lutc);
def LastDnTrendCount = fold ldtc=0 to AbsValue(BarNumber()) while !IsNaN(GetValue(close, -ldtc)) do GetValue(UpTrendCount, -ldtc);
def ShowUpTrendVerticalLine = UpTrendCount == LastUpTrendCount;
def ShowDnTrendVerticalLine = DnTrendCount == LastDnTrendCount;
#################################################################################
#################################################################################
input show_Chart_Bubble = no;
AddChartBubble(Up_Trend && show_Chart_Bubble, low, "ZZ", Color.UPTICK, no);
AddChartBubble(Down_Trend && show_Chart_Bubble, high, "ZZ", Color.DOWNTICK, yes);
input show_vertical_line = no;
AddVerticalLine((Up_Trend && show_vertical_line && ShowUpTrendVerticalLine), "Bullish", Color.DARK_GREEN, Curve.FIRM);#added condition
AddVerticalLine((Down_Trend && show_vertical_line && ShowDnTrendVerticalLine), "Bearish", Color.DARK_RED, Curve.FIRM);#added condition
Wow - thanks both of you! Is there a way to show the last line for Up_trend and Down_Trend? The current code only shows the last line.Replace your vertical line code with the following instead
I think I got it to work. Thanks again! Using the zig zag on charts like Amazon and Tesla is too messy. This helps a lot.@SJP07
Mine shows both.
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
B | How to hide a Vertical Line after number of bars? | Questions | 2 | |
S | Hide / Show Study Switch | Questions | 0 | |
S | Hide plot show bubble | Questions | 2 | |
Hide plot for X number of bars | Questions | 4 | ||
S | Cannot Hide AssignPriceColor on Daily chart? | Questions | 5 |
Start a new thread and receive assistance from our community.
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.
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.