MACD peak/valley?

trashcompactor

New member
My question is if it doesn't exist, could someone code an indicator that counts the values of the MACD and identifies the peak and lows.

Above Zero:
The MACD would count the values as momentum increases then once the value decreases, it would create an alert. I'll use simple numbers for demonstration. So 1, 2,3,4,5,4,3, etc. In this sample, the 5 would be the peak and the alert would come when the 4 is completed. In scenarios where the value increases again, the counting would restart. So 1,2,3,4,5,4,3,4,3,2 means the peak would be at 5 and would alert at 4 like mentioned before. But then the value increases again with the third 4 in the sequence. In this case, you would get an alert once the following number, 3, is completed.

Below Zero:
Same protocols but with negative numbers. I'll give an example. -1,-2,-3,-4,-5,-4,-3, etc. In this set, the -5 would be the peak and the alert would trigger on the following number, -4.

Please point me in the right direction or provide the code.

Thank you.

Here is a visual on what I am describing. I have marked where the peaks are and the alert would follow right after.

MACD Example.png
 
Solution
My question is if it doesn't exist, could someone code an indicator that counts the values of the MACD and identifies the peak and lows.

Above Zero:
The MACD would count the values as momentum increases then once the value decreases, it would create an alert. I'll use simple numbers for demonstration. So 1, 2,3,4,5,4,3, etc. In this sample, the 5 would be the peak and the alert would come when the 4 is completed. In scenarios where the value increases again, the counting would restart. So 1,2,3,4,5,4,3,4,3,2 means the peak would be at 5 and would alert at 4 like mentioned before. But then the value increases again with the third 4 in the sequence. In this case, you would get an alert once the following number, 3, is completed...
My question is if it doesn't exist, could someone code an indicator that counts the values of the MACD and identifies the peak and lows.

Above Zero:
The MACD would count the values as momentum increases then once the value decreases, it would create an alert. I'll use simple numbers for demonstration. So 1, 2,3,4,5,4,3, etc. In this sample, the 5 would be the peak and the alert would come when the 4 is completed. In scenarios where the value increases again, the counting would restart. So 1,2,3,4,5,4,3,4,3,2 means the peak would be at 5 and would alert at 4 like mentioned before. But then the value increases again with the third 4 in the sequence. In this case, you would get an alert once the following number, 3, is completed.

Below Zero:
Same protocols but with negative numbers. I'll give an example. -1,-2,-3,-4,-5,-4,-3, etc. In this set, the -5 would be the peak and the alert would trigger on the following number, -4.

Please point me in the right direction or provide the code.

Thank you.

Here is a visual on what I am describing. I have marked where the peaks are and the alert would follow right after.

your opening statement is confusing. you say count, but i don't know what you are counting?
'count the values as momentum increases'
what values are to be counted?
how do i read momentum?
what variable/formula defines momentum?
how do i use this count number?


it seems you want to evaluate the values of the histogram, and look for when it changes direction?

this looks for a peak or valley on the histogram.

but, you can choose to draw the arrow 1 bar or 2 bars after the peak. 2 if you want to wait until the bar after peak is closed.
input bars_after = { one, default two };

i copied the MACD code and added to it.

this set to yes will draw dots on a peak bars
input test1_peaks = no;

Code:
# macd_histo_peaks


#https://usethinkscript.com/threads/does-a-macd-histogram-peak-counter-exist.17130/
#Does a MACD Histogram peak counter exist?
#trashcompactor  11/25
#My question is if it doesn't exist, could someone code an indicator that counts the values of the MACD and identifies the peak and lows.


declare lower;

def na = double.nan;
def bn = barnumber();

#-------------------------
# macd
# TD Ameritrade IP Company, Inc. (c) 2007-2023
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

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

input oooooo = 0;

input bars_after = { one, default two };

#input following_bars_are_less_than_peak = yes;

# find peaks and valleys , on the histo , diff
# wait for bar after peak to close, so read from 2nd bar after a peak
# ref bar - is after the 1st rev bar after a peak/valley

def bars;
switch(bars_after) {
case one:
 # 1 bar after peak
 bars = 1;
case two:
 # 2 bars after peak
 bars = 2;
}


def peak;
def valley;
switch(bars_after) {
case one:
 # 1 bar after peak
 peak = (diff[0] > 0 and diff[0] < diff[1] and diff[1] > diff[2]);
 valley = (diff[0] < 0 and diff[0] > diff[1] and diff[1] < diff[2]);
case two:
 # 2 bars after peak
 peak = (diff[0] > 0 and diff[0] < diff[1] and diff[1] < diff[2] and diff[2] > diff[3]);
 valley = (diff[0] < 0 and diff[0] > diff[1] and diff[1] > diff[2] and diff[2] < diff[3]);
}


input vert = 1.3;
plot zval = if valley then diff*vert else na;
zval.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zval.SetDefaultColor(Color.green);
zval.setlineweight(1);
zval.hidebubble();

plot zpk = if peak then diff*vert else na;
zpk.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zpk.SetDefaultColor(Color.red);
zpk.setlineweight(1);
zpk.hidebubble();

# =====================================
# alert(condition, text, alert type, sound);
alert(valley, "up" ,alert.BAR, sound.DING);
alert(peak, "down" ,alert.BAR, sound.bell);
# =====================================


input test1_peaks = no;
plot zp = if test1_peaks and peak[-bars] then diff*vert else na;
zp.SetPaintingStrategy(PaintingStrategy.POINTS);
zp.SetDefaultColor(Color.cyan);
zp.setlineweight(3);
zp.hidebubble();


plot zv = if test1_peaks and valley[-bars] then diff*vert else na;
zv.SetPaintingStrategy(PaintingStrategy.POINTS);
zv.SetDefaultColor(Color.cyan);
zv.setlineweight(3);
zv.hidebubble();

addchartbubble(0, diff,
peak + "\n" +
valley
, color.yellow, no);

addchartbubble(0, diff,
diff + "\n" +
(diff-diff[1])
, color.yellow,
(if diff > 0 then 1 else 0));
#

arrows 2 bars after a peak or valley
4rwn6u5.jpg
 
Solution

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

Need help for a peak/valley alert that would notify me of peaks and valleys of the MACD value line (blue line by default on TOS) ?
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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