OPEN-SOURCE SCRIPT

MACD histogram relative open/close

Updated
Prelude

This script makes it easy to capture MACD Histogram open/close for automated trading.

There seems to be no "magic" value for MACD Histogram that always works as a cut-off for trade entry/exit, because of the variation in market price over time.

The idea behind this script is to replicate the view of the MACD graph we (humans) see on the screen, in mathematics, so the computer can approximately detect when the curve is opening/closing.

Math

The maths for this is composed of 2 sections -

1. Entry -

i. To trigger entry, we normalize the Histogram value by first determining the lowest and highest values on the MACD curves (MACD, Signal & Hist).
ii. The lowest and highest values are taken over the "Frame of reference" which is a hyperparameter.
iii. Once the frame of reference is determined, the entry cutoff param can be defined with respect to the values from (i) (10% by default)


2. Exit

To trigger an exit, a trader searches for the point where the Histogram starts to drop "steeply".

To convert the notion of "steep" into mathematics -

i. Take the max histogram value reached since last MACD curve flip
ii. Define the cutoff with reference to the value from (i) (30% by default)

Plots

Gray - Dead region
Blue - Histogram opening
Red - Histogram is closing


Notes

A good value for the frame of reference can be estimated by looking at the timescale of the graph you generally work with during manual trading.
For me, that turned out to be ~2.5 hours. (as shown in the above graph)
For a 3-minute ticker, frame of reference = 2.5 * 60 / 3 = 50
Which is the default given in this script.

Ultimately, it is up to you to do grid search and find these hyperparams for the stock and ticker size you're working with.

Also, this script only serves the purpose of detecting the Histogram curve opening/closing.
You may want to add further checks to perform proper trading using MACD.
Release Notes
MACD is a good way to confirm a trend and trade on it.

The problem is that often the trend is weak. When we are watching it manually, during the entry, we can see that that gap has sufficiently opened for confirmation of a possible potential entry. However, during the algo trading, the MACD will trigger a trade entry on a minuscule gap. Often such a scenario would result in sideways movement of the market. And a sideways movement might result in loss.

To prevent this, in this script there is a confirmation on the next candle after the MACD flip, that the gap is sufficiently wide. This gives you some safety from false entry signals and saves some losses. You can change the default values to suit your trade based on backtesting.

Same way, during the exit, if you wait for the MACD crossover flip, you lose some profit, often turning profit into a loss. So, you might want to exit just before the MACD trend reversal crossover. This script offers you to exit when the MACD divergence gap is the lowest just before a possible MACD trend reversal flip over. You can change the default values to suit your trade based on backtesting.
Moving Average Convergence / Divergence (MACD)

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer