Skip to content

Commit

Permalink
Add missing colnames to MA functions
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
serkor1 authored Feb 13, 2024
1 parent 25e9249 commit 001c4da
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions R/MovingAverages.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

#-------------------------------------------------------------------------#
Expand Down Expand Up @@ -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)
}

#-------------------------------------------------------------------------#
Expand Down Expand Up @@ -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)
}

#-------------------------------------------------------------------------#
Expand Down Expand Up @@ -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)
}

0 comments on commit 001c4da

Please sign in to comment.