Skip to contents

moving_average_convergence_divergence() is a generic S3 function that preserves the input class: data.frame in, data.frame out; matrix in, matrix out.

moving_average_convergence_divergence() also accepts a double vector, in which case the indicator is calculated directly without column selection. When the result has a single column it is simplified to a double vector; otherwise the full n by k matrix is returned.

Handling of -values

Leading NAs are always produced for the initial lookback period where insufficient data is available. If the input itself contains NAs they are passed through to the underlying C routine, which can cause the entire output to be filled with NAs. Set na.ignore = TRUE to strip NAs before calculation and re-insert them at their original positions in the output.

Usage

moving_average_convergence_divergence(
  x,
  cols,
  fast = 12,
  slow = 26,
  signal = 9,
  na.ignore = FALSE,
  ...
)

Arguments

x

An OHLC-V series coercible to data.frame. Alternatively, x may also be supplied as a double vector.

cols

(formula). An optional 1-variable formula selecting columns from x via model.frame. Defaults to ~close.

fast

(integer). Period for the fast Moving Average (MA).

slow

(integer). Period for the slow Moving Average (MA).

signal

(integer). Period for the signal Moving Average (MA).

na.ignore

(logical). A logical of length 1. FALSE by default. If TRUE, NAs in the input are stripped before calculation and re-inserted at their original positions in the output.

...

Additional parameters passed into model.frame

Value

An object of same class and length of x:

MACD

double

MACDSignal

double

MACDHist

double

Author

Serkan Korkmaz

Examples

## load Bitcoin (BTC)
## series
data(BTC, package = "talib")

## calculate the indicator
## for Bitcoin (BTC)
output <- talib::moving_average_convergence_divergence(BTC)

## display the results
utils::tail(output)
#>                           MACD MACDSignal   MACDHist
#> 2024-12-26 01:00:00  608.68287  1590.9032  -982.2204
#> 2024-12-27 01:00:00  243.07106  1321.3368 -1078.2657
#> 2024-12-28 01:00:00   29.87501  1063.0444 -1033.1694
#> 2024-12-29 01:00:00 -261.68536   798.0985 -1059.7838
#> 2024-12-30 01:00:00 -561.79956   526.1189 -1087.9184
#> 2024-12-31 01:00:00 -729.69370   274.9564 -1004.6501

## visualize the indicator
## with talib::chart()
##
## see ?talib::chart or ?talib::indicator
## for more details
{
 ## chart OHLC-V
 ## series with talib::chart()
 talib::chart(BTC)

 ## chart indicator
 ## with default values
 talib::indicator(
     talib::moving_average_convergence_divergence
 )
}