Skip to contents

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

mesa_adaptive_moving_average() 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 NA values

Every indicator always emits leading NAs for the initial lookback period - positions where there is not yet enough data to produce a result. This is separate from how NAs already present in the input are handled, which is controlled by the na.bridge argument:

na.bridge = FALSE (default)

The input is passed to the underlying TA-Lib C routine as-is. Because most indicators smooth across time (EMA, RSI, MACD, Bollinger Bands, ...), a single NA in the input typically propagates forward and poisons every subsequent value - it is common for one missing observation to produce an output that is entirely NA from that position onward. This mode is the right choice when you want to see the missing data in the output rather than silently compute around it.

na.bridge = TRUE

NA rows are stripped from the input before the C routine runs; the indicator is computed on the resulting dense series; results are then re-expanded to the original length with NA inserted at every position the input had NA. Output length always matches input length, so the result can be joined back to the source data.frame by row.

Consequence to understand before enabling: bridging causes the indicator to treat non-consecutive observations as consecutive. A 14-period RSI with na.bridge = TRUE over a series containing a month-long gap will compute using 14 observations that span several real-world months as if they were 14 adjacent trading days. For sparse missing values (e.g. a single missing tick) this is harmless; for clustered gaps (e.g. a delisted period, a weekend encoded as NA) the output is correctly aligned by position but economically meaningless across the gap. Inspect gap structure with which(is.na(x)) before enabling on low-quality time series.

Usage

mesa_adaptive_moving_average(
  x,
  cols,
  n = 30,
  fast = 0.5,
  slow = 0.05,
  na.bridge = 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.

n

(integer). Lookback period (window size). A positive integer of length 1.

fast

(double). Upper limit of the adaptive smoothing factor (alpha) used in the MESA algorithm. A double in [0.01, 0.99]. 0.5 by default.

slow

(double). Lower limit of the adaptive smoothing factor (alpha) used in the MESA algorithm. A double in [0.01, 0.99]. 0.05 by default.

na.bridge

(logical). A logical of length 1. FALSE by default. When FALSE, input NAs propagate through the TA-Lib C routine (most indicators will fill the remaining output with NA). When TRUE, input NA rows are stripped before computation and re-inserted at the original positions in the output, causing the indicator to treat non-consecutive non-NA observations as if they were adjacent — see the Handling of NA values section above for the consequences.

...

Additional parameters passed into model.frame.

Value

An object of same class and length of x:

MAMA

double

FAMA

double

Details

When passed without 'x', mesa_adaptive_moving_average functions as an 'Moving Average'-specification which is used in, for example, stochastic when constructing the smoothing lines.

When called without 'x' it will return a named list which is used for the indicators that supports various Moving Average specifications.

Author

Serkan Korkmaz

Examples

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

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

## display the results
utils::tail(output)
#>                         MAMA     FAMA
#> 2024-12-26 01:00:00 98504.62 99138.86
#> 2024-12-27 01:00:00 98287.78 99117.58
#> 2024-12-28 01:00:00 98129.43 99092.88
#> 2024-12-29 01:00:00 97901.16 99063.08
#> 2024-12-30 01:00:00 95264.58 98113.46
#> 2024-12-31 01:00:00 95170.89 98039.89

## 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::mesa_adaptive_moving_average
 )
}