From 001c4daa534d87a58db576c165de891db1d2f2e1 Mon Sep 17 00:00:00 2001 From: Serkan Korkmaz <77464572+serkor1@users.noreply.github.com> Date: Tue, 13 Feb 2024 23:59:07 +0100 Subject: [PATCH] Add missing colnames to MA functions The following functions didn't add column names: * HMA() * ALMA() * WMA() * EVWMA() EVWMA() and WMA() didn't reclass() before the if-statement, so the colnames weren't added because the dims were always NULL. The fix to WMA() meant we didn't have to add a reclass() to ALMA() and HMA(). We just had to set the column name. Closes #131. --------- Co-authored-by: Joshua Ulrich --- R/MovingAverages.R | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/R/MovingAverages.R b/R/MovingAverages.R index 0b71bbe..0bc31ca 100644 --- a/R/MovingAverages.R +++ b/R/MovingAverages.R @@ -278,11 +278,14 @@ function(x, n=10, wts=1:n, ...) { # replace 1:(n-1) with NAs and prepend NAs from original data ma[1:(n-1)] <- NA + # Convert back to original class + ma <- reclass(ma,x) + if(!is.null(dim(ma))) { colnames(ma) <- "WMA" } - reclass(ma,x) + return(ma) } #-------------------------------------------------------------------------# @@ -314,12 +317,14 @@ function(price, volume, n=10, ...) { # Call C routine ma <- .Call(C_evwma, pv[,1], pv[,2], n) + # Convert back to original class + ma <- reclass(ma, price) + if(!is.null(dim(ma))) { colnames(ma) <- "EVWMA" } - # Convert back to original class - reclass(ma, price) + return(ma) } #-------------------------------------------------------------------------# @@ -380,7 +385,11 @@ function(x, n=20, ...) { hma <- WMA(madiff, n = trunc(sqrt(n)), ...) - reclass(hma, x) + if(!is.null(dim(hma))) { + colnames(hma) <- "HMA" + } + + return(hma) } #-------------------------------------------------------------------------# @@ -409,5 +418,10 @@ function(x, n=9, offset=0.85, sigma=6, ...) { for(i in seq_len(NCOL(x))) { alma[,i] <- WMA(x[,i], n, wts) } + + if(!is.null(dim(alma))) { + colnames(alma) <- "ALMA" + } + reclass(alma, x) }