Indicators

MACD


Description

Moving average convergence divergence (MACD) 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 that 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. Traders may buy the security when the MACD crosses above its signal line and sell—or short—the security when the MACD crosses below the signal line. Moving average convergence divergence (MACD) indicators can be interpreted in several ways, but the more common methods are crossovers, divergences, and rapid rises/falls.

For more information, please visit Investopedia.


Params & Instance Methods

#initialize
@param series [Array] An array of prices, typically closing prices
@param fast_period [Integer] The number of periods to use in the fast calculation - default is 12
@param slow_period [Integer] The number of periods to use in the slow calculation - default is 26
@param signal_period [Integer] The number of periods to use in the signal calculation - default is 9

#call
@return [Array] An array containing the current MACD line, signal line, and histogram values

#valid?
@return [Boolean] Whether or not the object is valid

Example

require 'ruby-technical-analysis'

series = [166.23, 164.51, 162.41, 161.62, 159.78, 159.69, 159.22, 170.33,
        174.78, 174.61, 175.84, 172.9, 172.39, 171.66, 174.83, 176.28,
        172.12, 168.64, 168.88, 172.79, 172.55, 168.88, 167.3, 164.32,
        160.07, 162.74, 164.85, 165.12, 163.2, 166.56, 166.23, 163.17,
        159.3, 157.44, 162.95]
fast_period = 12
slow_period = 26
signal_period = 9

macd = 
  RubyTechnicalAnalysis::Macd.new(
    series: series,
    fast_period: fast_period,
    slow_period: slow_period,
    signal_period: signal_period
  )

p macd.valid? # true

p macd.call # [-1.934, -1.664, -0.27]