Strategy Creation

What is MACD and How to Use It in Tuned Script

Mr. Plippy

What Is Moving Average Convergence Divergence (MACD)?

Moving Average Convergence Divergence (MACD) is a trend-following momentum indicator that shows the relationship between two moving averages of an assets price. The MACD is calculated by subtracting the 26-period exponential moving average (EMA) from the 12-period EMA. The outcome of that calculation is the MACD line. A 9-period 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_1.PNG

Key points about the MACD

  1. MACD triggers technical signals when it crosses above (to buy) or below (to sell) its signal line.
  2. The speed of crossovers is also taken as a signal of whether a market is overbought or oversold.
  3. MACD also helps investors understand whether the bullish or bearish movement in the price is strong or weak.

How to add the MACD to my script?

Below you will find a short example of using the MACD in your script.

// Inputs
    // We will define 3 integer inputs, Fast line (12), Slow line (26)
    // and signal Line (9)
    fastLen = intInput("Fast Length", 12)
    slowLen = intInput("Slow Length", 26)
    signalLen = intInput("Signal Length", 9)
    src = close

//indicator
    // Define the MACD conditions
    fast_ma = ema(src, fastLen)
    slow_ma = ema(src, slowLen)
    macd = fast_ma - slow_ma
    signal = ema(macd, signalLen)
    hist = macd - signal

// Macd colors
    col_grow_above = seriesOf("#26A69A")
    col_grow_below = seriesOf("#FFCDD2")
    col_fall_above = seriesOf("#B2DFDB")
    col_fall_below = seriesOf("#EF5350")
    col_macd = seriesOf("#0094ff")
    col_signal = seriesOf("#ff6a00")

// Histogram Colors
    Histocolor = (iff(gte(hist, seriesOf(0.0)), 
        (iff(lt(hist[1], hist), col_grow_above, col_fall_above)), 
        (iff(lt(hist[1], hist), col_grow_below, col_fall_below))))

// Plot
    // Plot the MACD in 3 different lines
    plot(hist, "histogram", Histocolor, 4, PlotStyle.HISTOGRAM, 0, false)
    plot(macd, "macd line", col_macd, 1, PlotStyle.LINE, 0, false)
    plot(signal, "signal line", col_signal, 1, PlotStyle.LINE, 0, false)

// Signal Push
    // NEVER_ORDER means that no orders will be placed
    out(NEVER_ORDER)
MACD_2.PNG

The above image is how the plot should look on the chart.

How to use the MACD to generate buy and sell signals?

For the example I will use the default settings, Fast Length = 12, Slow Length = 26 and Signal Length = 9

The most commonly used method for the MACD is to buy when the MACD line crosses up through the Signal Line, and to sell when the MACD line crosses down through the Signal Line.

This generally indicates a strong momentum change.

//Inputs
    // We will define 3 integer inputs, Fast line (12), Slow line (26), 
    //and signal Line (9)
    fastLen = intInput("Fast Length", 12)
    slowLen = intInput("Slow Length", 26)
    signalLen = intInput("Signal Length", 9)
    src = close

//indicator
    // Define the MACD conditions
    fast_ma = ema(src, fastLen)
    slow_ma = ema(src, slowLen)
    macd = fast_ma - slow_ma
    signal = ema(macd, signalLen)
    hist = macd - signal

// Macd colors
    col_grow_above = seriesOf("#26A69A")
    col_grow_below = seriesOf("#FFCDD2")
    col_fall_above = seriesOf("#B2DFDB")
    col_fall_below = seriesOf("#EF5350")
    col_macd = seriesOf("#0094ff")
    col_signal = seriesOf("#ff6a00")

// Histogram Colors
    Histocolor = (iff(gte(hist, seriesOf(0.0)), 
        (iff(lt(hist[1], hist), col_grow_above, col_fall_above)), 
        (iff(lt(hist[1], hist), col_grow_below, col_fall_below))))

//Plot
    // Plot the MACD in 3 different lines
    plot(hist, "histogram", Histocolor, 4, PlotStyle.HISTOGRAM, 0, false)
    plot(macd, "macd line", col_macd, 1, PlotStyle.LINE, 0, false)
    plot(signal, "signal line", col_signal, 1, PlotStyle.LINE, 0, false)

// Entry and Exit conditions
    // Define the entry of the macd line crossing over the signal line
    buy = crossover(macd, signal)

    // Define the entry of the macd line crossing under the signal line
    sell = crossunder(macd, signal)

//Signal Push
    // Here you will output your Entry and Exit signals
    out(signalIf(buy, sell))

Below is a screenshot of how your chart should look

MACD_3.PNG


So now you have learnt what MACD is, how to use it and how to put it in a script and generate signals from it.
Have fun scripting!


Disclaimer: The opinions expressed here are for general informational purposes only and are not intended to provide specific advice or recommendations for any specific security or investment product. You should never invest money that you cannot afford to lose. Before trading using complex financial products, please ensure to understand the risks involved. Past performance is no guarantee of future results.

All investing involves risk, including the possible loss of all the money you invest, and past performance does not guarantee future performance. Historical returns expected returns, and probability projections are provided for informational and illustrative purposes, and may not reflect actual future performance.

Tuned is not a broker-dealer, exchange, custodian, wallet provider, or counterparty. Investing is highly speculative and volatile. Tuned is only suitable for investors who fully understand the risk of loss and may experience large drawdowns. Investors should never invest more than they can afford to lose.