Search code examples
rquantmodstocktechnical-indicator

Stock Technical Indicators MFI error Series contains non-leading NAs


I am creating stock technical indicators for technical analysis. Most of the indicators work fine except MFI. The moment code reaches to MFI, I get below error

Series contains non-leading NAs

I have verified that there are no non leading NAs in stock series.Still I get this error. Below is a reproducible code. Appreciate any help.

library(ggplot2) 
library(quantmod) 
library(xts) 
library(timeSeries) 
library(rpart)
library(ROCR)


get_indicators <- function(stock, period){
  stock <- na.locf(stock)
  price_change <- Ad(lag(stock,-period)) - Ad(stock)
  response <- ifelse(price_change > 0, "Up", "Down")
  
  #Simple Moving Average
  sma15 <- SMA(Ad(stock),n=15)
  sma50 <- SMA(Ad(stock),n=50)

  #Exponential Moving Average
  ema15 <- EMA(Ad(stock),n=15)
  ema50 <- EMA(Ad(stock),n=50)
  
  #Moving Average Convergence Divergence
  macd <- MACD(Ad(stock), nFast=12, nSlow=26, nSig=9, maType= "EMA")  

  #Relative Strength Index
  rsi <- RSI(Ad(stock), n=14, maType= "EMA")
  
  #High, Low, and Adjusted Close xts object
  hlac <- as.xts(data.frame(x=Hi(stock), y=Lo(stock), z=Ad(stock)))
  
  #Stochastic Oscillator
  sto <- stoch(hlac, nFastK = 14) *100
  
  #Commodity Channel Index
  cci <-CCI(hlac, n = 20, c = 0.015)
  
  #Price Rate of Change
  proc <- ROC(Ad(stock), n=14) *100
  
  #Momentum
  mom <- momentum(Ad(stock), n=14)
  
  #Bollinger Bands
  bb <- BBands(Ad(stock),n=20,sd = 2)
  
  #Average True Range
  atr <- ATR(hlac, n=14)
  
  #On Balance Volume
  obv <- OBV(Ad(stock), Vo(stock))
  
  print(anyNA(stock))
  print(sum(is.na(stock)))
  
  
  #Money Flow Index
  mfi <- MFI(hlac,Vo(stock),n=14)
  
  #Feature Aggregation: Combine all derived indicators for model buuilding
  indicators <- data.frame(sma15, sma50, ema15, ema50, macd, rsi, 
                           sto, cci, proc, mom, bb$mavg, atr$atr, obv,  response)
  colnames(indicators) <- c("SMA.15","SMA.50","EMA.15","EMA.50","MACD","MACDSignal",
                            "RSI","StoFASTK","StoFASTD","StoSLOWD", "CCI.20","PROCR",
                            "MOM.14", "BB","ATR", "OBV","Response")
  
 
  return(indicators)
}
start.date <-'2015-01-01'
end.date <-'2020-01-01'
quantmod::getSymbols("SBIN.NS",src="yahoo",from=start.date,to=end.date,periodicity="daily")
stoci <- get_indicators(SBIN.NS,20)
stoci

Solution

  • The problem seems to be with your hlac object. Using adjusted closing price instead of actual closing price might be the reason.

    The problem code works fine when using a quantmod::HLC() object instead.

    ``` r
    library(quantmod)
    #> Loading required package: xts
    #> Loading required package: zoo
    #> 
    #> Attaching package: 'zoo'
    #> The following objects are masked from 'package:base':
    #> 
    #>     as.Date, as.Date.numeric
    #> Loading required package: TTR
    #> Registered S3 method overwritten by 'quantmod':
    #>   method            from
    #>   as.zoo.data.frame zoo
    #> Version 0.4-0 included new data defaults. See ?getSymbols.
    
    from <-'2015-01-01'
    to <-'2020-01-01'
    stock <- "SBIN.NS"
    
    getSymbols(stock, from = from, to = to, src = 'yahoo')
    ### Messages removed ###
    #> [1] "SBIN.NS"
    
    SBIN.NS <- na.locf(SBIN.NS)
    sum(is.na(SBIN.NS))
    #> [1] 0
    
    #hlac <- as.xts(data.frame(x=Hi(SBIN.NS), y=Lo(SBIN.NS), z=Ad(SBIN.NS)))
    
    hlc <- HLC(SBIN.NS)
    
    mfi <- MFI(hlc,Vo(SBIN.NS),n=10)
    
    head(mfi, n = 20)
    #>                 mfi
    #> 2015-01-01       NA
    #> 2015-01-02       NA
    #> 2015-01-05       NA
    #> 2015-01-06       NA
    #> 2015-01-07       NA
    #> 2015-01-08       NA
    #> 2015-01-09       NA
    #> 2015-01-12       NA
    #> 2015-01-13       NA
    #> 2015-01-14       NA
    #> 2015-01-15 53.58373
    #> 2015-01-16 41.74790
    #> 2015-01-19 41.48900
    #> 2015-01-20 51.62824
    #> 2015-01-21 64.43703
    #> 2015-01-22 65.38013
    #> 2015-01-23 74.19176
    #> 2015-01-27 75.15463
    #> 2015-01-28 76.77622
    #> 2015-01-29 73.79739
    

    Created on 2020-12-22 by the reprex package (v0.3.0)