Computing Stats on MACD Histogram

TraderJim

New member
Hello everyone, I have a question about computing statistics on a MACD histogram. It would be great if I could learn how to compute the following:
1) Count only the bars that have crossed the zero axis from negative to positive (dark red to green) starting with the second green bar and counting until the first dark green bar. Ignore any green bars thereafter until the momentum crosses from negative to positive value.
2) Compute this count for every time the momentum crosses from negative to positive on the chart.
3) Compute the stats:
a) maximum number of bars counted i.e. if there were 5 crossings find the one with the highest number of green bars (will be put in a label on the chart)
b) The average number of green bars from all groups of bars counted (will also be placed in a label on the chart)

Here is my chart:
sRpPYMZ.png


and here is the ThinkScript I have been working on but I am not getting the correct numbers. I am completely confused with how to get the counts for the bars. I am trying to use the fold function to test back at least 15 bars for the constraints but it doesn't seem to work to the correct solution.


bn =BarNumber();

def risehistcnt = fold p = 0 to 15
with q = 0
while momentum[1] < momentum
do if bn == 1 then 0 else if momentum[2] < 0 and momentum > 0 then risehistcnt[1] + 1 else risehistcnt[1];

Thank you

Still working for a solution here is a screenshot of what I have so far:
Hs5fNeu.png


Sorry for the short snippet of code previously here is what I am working with (complete code), not quite there. The 1's are identifying the proper bars but having trouble in that I think it should be counting the bars.

# Stat MACD

declare upper;


input fastlength = 26;
input slowlength = 34;
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"));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

def pos = Diff > 0;
def neg = Diff < 0;
def up = Diff[1] < Diff;
def dn = Diff[1] > Diff;

# initialize bar number computation (counts the number of bars on the chart)
def bn = BarNumber();

# Compute number of bars that are showing on a chart (shows how many bars are on the chart)
def barcnt = HighestAll(if IsNaN(close) then 0 else bn);
AddLabel(1, "Chart Bar Count: " + barcnt, Color.GRAY);


#####################################
########## WORKING ON CODE ##########
#####################################

# Compute the number of green hist bars after momentum crosses into positive and the first down bar
#def risehistcnt = fold p = 0 to 15
#with q = 0
#while dn != 1
#do if bn == 1 then 0 else if pos[1] == 0 and pos == 1 then risehistcnt[1] + 1 else risehistcnt[1];

def risehistcnt = fold p = 0 to 15
with q = 0
while dn != 1
do if pos[1] != 1 and pos == 1 then risehistcnt[1] + 1 else risehistcnt[1];





####################################
####################################


# Debugging plots

plot risehistcntplot = risehistcnt;
risehistcntplot.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
risehistcntplot.SetDefaultColor(Color.GREEN);
 
Last edited:
Solution
Hello everyone, I have a question about computing statistics on a MACD histogram. It would be great if I could learn how to compute the following:
1) Count only the bars that have crossed the zero axis from negative to positive (dark red to green) starting with the second green bar and counting until the first dark green bar. Ignore any green bars thereafter until the momentum crosses from negative to positive value.
2) Compute this count for every time the momentum crosses from negative to positive on the chart.
3) Compute the stats:
a) maximum number of bars counted i.e. if there were 5 crossings find the one with the highest number of green bars (will be put in a label on the chart)
b) The average number of green bars...
Hello everyone, I have a question about computing statistics on a MACD histogram. It would be great if I could learn how to compute the following:
1) Count only the bars that have crossed the zero axis from negative to positive (dark red to green) starting with the second green bar and counting until the first dark green bar. Ignore any green bars thereafter until the momentum crosses from negative to positive value.
2) Compute this count for every time the momentum crosses from negative to positive on the chart.
3) Compute the stats:
a) maximum number of bars counted i.e. if there were 5 crossings find the one with the highest number of green bars (will be put in a label on the chart)
b) The average number of green bars from all groups of bars counted (will also be placed in a label on the chart)

Here is my chart:
sRpPYMZ.png


and here is the ThinkScript I have been working on but I am not getting the correct numbers. I am completely confused with how to get the counts for the bars. I am trying to use the fold function to test back at least 15 bars for the constraints but it doesn't seem to work to the correct solution.


bn =BarNumber();

def risehistcnt = fold p = 0 to 15
with q = 0
while momentum[1] < momentum
do if bn == 1 then 0 else if momentum[2] < 0 and momentum > 0 then risehistcnt[1] + 1 else risehistcnt[1];

Thank you

Still working for a solution here is a screenshot of what I have so far:
Hs5fNeu.png


Sorry for the short snippet of code previously here is what I am working with (complete code), not quite there. The 1's are identifying the proper bars but having trouble in that I think it should be counting the bars.

# Stat MACD

declare upper;


input fastlength = 26;
input slowlength = 34;
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"));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

def pos = Diff > 0;
def neg = Diff < 0;
def up = Diff[1] < Diff;
def dn = Diff[1] > Diff;

# initialize bar number computation (counts the number of bars on the chart)
def bn = BarNumber();

# Compute number of bars that are showing on a chart (shows how many bars are on the chart)
def barcnt = HighestAll(if IsNaN(close) then 0 else bn);
AddLabel(1, "Chart Bar Count: " + barcnt, Color.GRAY);


#####################################
########## WORKING ON CODE ##########
#####################################

# Compute the number of green hist bars after momentum crosses into positive and the first down bar
#def risehistcnt = fold p = 0 to 15
#with q = 0
#while dn != 1
#do if bn == 1 then 0 else if pos[1] == 0 and pos == 1 then risehistcnt[1] + 1 else risehistcnt[1];

def risehistcnt = fold p = 0 to 15
with q = 0
while dn != 1
do if pos[1] != 1 and pos == 1 then risehistcnt[1] + 1 else risehistcnt[1];





####################################
####################################


# Debugging plots

plot risehistcntplot = risehistcnt;
risehistcntplot.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
risehistcntplot.SetDefaultColor(Color.GREEN);

This might provide the stats you are requesting.

Screenshot 2023-11-11 064618.png


Code:
#MACD_STATS

#1) Count only the bars that have crossed the zero axis from negative to positive (dark red to green) starting with the second green bar and counting until the first dark green bar. Ignore any green bars thereafter until the momentum crosses from negative to positive value.
#2) Compute this count for every time the momentum crosses from negative to positive on the chart.
#3) Compute the stats:
#a) maximum number of bars counted i.e. if there were 5 crossings find the one with the highest number of green bars (will be put in a label on the chart)
#b) The average number of green bars from all groups of bars counted (will also be placed in a label on the chart)

# Stat MACD

declare upper;


input fastlength = 26;
input slowlength = 34;
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"));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

def pos = Diff > 0;
def neg = Diff < 0;
def up = Diff[1] < Diff;
def dn = Diff[1] > Diff;

# initialize bar number computation (counts the number of bars on the chart)
def bn = BarNumber();

# Compute number of bars that are showing on a chart (shows how many bars are on the chart)
def barcnt = HighestAll(if IsNaN(close) then 0 else bn);
AddLabel(1, "Chart Bar Count: " + barcnt, Color.GRAY);


#####################################
########## STATS ##########
#####################################
input bubbles = yes;
def risingcount = CompoundValue(1, if BarNumber() == 1 then Double.NaN else if neg[2] and pos[1] and pos then 1 else if risingcount[1] >= 1 and up then risingcount[1] + 1 else Double.NaN, 0);
def poscount = CompoundValue(1, if BarNumber() == 1 then 0 else if IsNaN(risingcount[1] > 0) and !IsNaN(risingcount > 0) then poscount[1] + 1 else poscount[1], 0);
def posbars = if !IsNaN(risingcount) then posbars[1] + 1 else posbars[1];

AddChartBubble(bubbles and risingcount > 0, high, risingcount, Color.GRAY);
AddLabel(1, "Highest Pos Bars " + HighestAll(risingcount), Color.WHITE);
AddLabel(1, "Total Pos Bars " + (HighestAll(posbars) - LowestAll(posbars)), Color.WHITE);
AddLabel(1, "Total Crosses " + (HighestAll(poscount) - LowestAll(poscount)), Color.WHITE);
AddLabel(1, "Avg Pos Bars per Cross " + Round((HighestAll(posbars) - LowestAll(posbars)) /  (HighestAll(poscount) - LowestAll(poscount)), 0), Color.WHITE);

#
 
Solution

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