股票技术指标 MFI 错误系列包含非领先的 NA

Stock Technical Indicators MFI error Series contains non-leading NAs

我正在创建用于技术分析的股票技术指标。除 MFI 外,大多数指标都运行良好。当代码到达 MFI 时,我得到以下错误

系列包含非领先的 NA

我已确认库存中没有非领先的 NA series.Still 我收到此错误消息。下面是一个可重现的代码。感谢任何帮助。

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

问题似乎出在您的 hlac 对象上。使用调整后的收盘价而不是实际收盘价可能是原因。

问题代码在使用 quantmod::HLC() 对象时工作正常。

``` 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

reprex package (v0.3.0)

于 2020-12-22 创建