Draw Vertical Line for MACD crossover?

JP782

Active member
I want to have vertical lines drawn when the MACD crosses, I assumed the script would be similar to the one Im using..

def condM1CrossUndr0 = Value < 0 and Value > Avg;
Addlabel(condM1CrossUndr0, "M1”, Color.Green);

... to add a label when crossover happens: changing "addlabel" to "addverticalline" but of course not - clearly theres more to it, what am I missing?
 
Last edited by a moderator:
Im not having much luck with anyone answering any of the questions Ive been asking last couple weeks but Ill try again-

I want to have vertical lines drawn when the MACD crosses, I assumed the script would be similar to the one Im using..

def condM1CrossUndr0 = Value < 0 and Value > Avg;
Addlabel(condM1CrossUndr0, "M1”, Color.Green);

... to add a label when crossover happens: changing "addlabel" to "addverticalline" but of course not - clearly theres more to it, what am I missing?

The following script will work in the upper panel as well as the lower.

Capture.jpg
Ruby:
def condM1CrossUndr0 = reference macd().Value < 0 and reference macd().Value > reference
macd().Avg;
Addlabel(condM1CrossUndr0, "M1”, Color.Green);
addverticalLine(condM1CrossUndr0[1]==0 and condm1CrossUndr0,"",color.green);
 

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

anyway it can be coded where it gives a signal or label if not a vertical line,,, when macd cross and histogram are broth above or under the Zero Line? any assistance would be greatly appreciated
 
anyway it can be coded where it gives a signal or label if not a vertical line,,, when macd cross and histogram are broth above or under the Zero Line? any assistance would be greatly appreciated

Input options to show or not: signals (arrows), alerts, verticallines, bubbles and/or labels.

Capture.jpg
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;

def value = MACD(fastLength, slowLength, MACDLength, averageType);
def avg   = MACD(fastLength, slowLength, MACDLength, averageType).Avg;
def Diff  = MACD(fastLength, slowLength, MACDLength, averageType).Diff;

input signal = yes;
plot upsignal = if !signal then Double.NaN else value > 0 and Diff > 0 and value crosses avg;

upsignal.DefineColor("Negative to Positive", Color.GREEN);
upsignal.AssignValueColor(upsignal.Color("Negative to Positive"));
upsignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upsignal.SetLineWeight(3);

plot dnsignal = if !signal then Double.NaN else value < 0 and Diff < 0 and value crosses avg;

dnsignal.DefineColor("Positive to Negative", Color.RED);
dnsignal.AssignValueColor(dnsignal.Color("Positive to Negative"));
dnsignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
dnsignal.SetLineWeight(3);

input label =  yes;
AddLabel(label, if upsignal then "(Cross and Histogram) > 0" else "(Cross and Histgram) < 0", if upsignal then Color.GREEN else Color.RED);

input bubbles = yes;
input bubbleoffset = 3;
addchartBubble(bubbles and (upsignal or dnsignal), if upsignal then low - ticksize() * bubbleoffset else high + ticksize() * bubbleoffset, if upsignal then "Up" else "Dn", if upsignal then color.green else color.red, if upsignal then no else yes);

input verticalline = yes;
AddVerticalLine(verticalline and upsignal, "", Color.GREEN, stroke = Curve.LONG_DASH);
AddVerticalLine(verticalline and dnsignal, "", Color.RED, stroke = Curve.LONG_DASH);

input alert = yes;
Alert(alert, if upsignal then "(Cross and Histogram) > 0" else "(Cross and Histgram) < 0", Alert.BAR, Sound.Chimes);
 
maybe i need to try it when market is open,, the label seems to be staying as is,,, even when i switch TF and macd is over 0 line,

Try replacing the label code with this and the label will now only appear on the bar when a signal happens.

Code:
input label =  yes;
AddLabel(label and (upsignal or dnsignal), if upsignal then "(Cross and Histogram) > 0" else if dnsignal then "(Cross and Histgram) < 0" else "", if upsignal then Color.GREEN else Color.RED);
 
Any chance we can get signal line whenever the MACD crosses regardless if both are over/under zero line?

This will plot a vertical line and bubble at all crosses.
A label will depict the status of the most recent cross.
The colors of the label and bubble are based upon where the cross occurs in relation to the zero line
The colors can be adjusted at the input screen under globals
Optional alerts are included

Code:
#MACD_Crosses_Verticals
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;

def value = MACD(fastLength, slowLength, MACDLength, averageType);
def avg   = MACD(fastLength, slowLength, MACDLength, averageType).Avg;
def Diff  = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
def last_cross = if value crosses above avg then 1 else if value crosses below avg then 0 else last_cross[1];

DefineGlobalColor("Above-Above", Color.GREEN);
DefineGlobalColor("Above-Below", Color.LIME);
DefineGlobalColor("Below-Below", Color.RED);
DefineGlobalColor("Below-Above", Color.PINK);

input label =  yes;
AddLabel(label and last_cross == 1, "Value crosses ABOVE Avg when" + (if value > 0 or avg > 0 then  " Above 0 line" else " Below 0 line")
, if avg > 0 or value > 0 then GlobalColor("Above-Above") else GlobalColor("Above-Below"));
AddLabel(label and last_cross == 0, "Value crosses BELOW Avg when" + (if value > 0 or avg > 0 then  " Above 0 line" else " Below 0 line")
, if avg < 0 or value < 0 then GlobalColor("Below-Below") else GlobalColor("Below-Above"));

input bubbles = yes;
input bubbleoffset = 3;
AddChartBubble(bubbles and value crosses above avg, low - TickSize() * bubbleoffset, "Up" , if avg > 0 or value > 0 then GlobalColor("Above-Above") else GlobalColor("Above-Below"), no);
AddChartBubble(bubbles and value crosses below avg, high + TickSize() * bubbleoffset, "Dn" , if avg < 0 or value < 0 then GlobalColor("Below-Below") else GlobalColor("Below-Above"), yes);

input verticalline = yes;
AddVerticalLine(verticalline and value crosses above avg , "", if value > 0 or avg > 0 then GlobalColor("Above-Above") else GlobalColor("Above-Below"), Curve.LONG_DASH);
AddVerticalLine(verticalline and value crosses below avg, "", if value < 0 or avg < 0 then GlobalColor("Below-Below") else GlobalColor("Below-Above"), Curve.LONG_DASH);

input alert = yes;
Alert(alert, if value crosses above avg then "Cross ABOVE" else "Cross BELOW", Alert.BAR, Sound.Chimes);

#
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
253 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