Hide vertical line

SJP07

Member
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!
 
Solution
@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.
@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.
 
Solution
@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.
I wish I knew I understand what you meant lol. Here's my code. Do you mind incorporating your suggestions?

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);

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), "Bullish", Color.Dark_Green, Curve.FIRM);
AddVerticalLine((Down_Trend && show_vertical_line), "Bearish", Color.Dark_Red, Curve.FIRM);
 
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!

Here is an example of one way to do it.

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.

Replace your vertical line code with the following instead

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);
 
@SJP07
@SleepyZ s way is better.

Code:
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
 

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
421 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