The MACD (Moving Average Convergence Divergence) is a trend-following momentum indicator that shows the relationship between two moving averages of a security’s price. The MACD is calculated by subtracting the 26-period Exponential Moving Average (EMA) from the 12-period EMA. The result of this calculation is the MACD line. A nine-day EMA of the MACD called the “signal line,” is then plotted on top of the MACD line, which can function as a trigger for buy and sell signals.
MACD[S, L, Si](price)
i1 = MACD[12,26,9](close)
FOR i = 20 DOWNTO 0 DO
IF(close[i] < close[i+1] AND i1 > i1[1]) THEN
result = 100
ELSE
result = 0
ENDIF
NEXT
RETURN result
This example calculates the MACD for the closing price with typical parameters (12, 26, 9). It then checks for a condition over the last 21 bars where the closing price is increasing and the MACD value is greater than its previous value. If both conditions are met, it assigns a result of 100; otherwise, it assigns a result of 0.
The MACD is a versatile tool in technical analysis, providing insights into both the momentum and direction of market prices.