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.