在R中的烛台图案之外绘制函数

时间:2017-06-15 16:14:41

标签: r quantmod

我有两个xts对象:stockbase。我计算了相对强度(它只是股票收盘价和基准指数的比率),我想绘制烛台模式之外的每周相对强度。数据的链接为herehere

library(quantmod)
library(xts)

read_stock = function(fichier){ #read and preprocess data
  stock = read.csv(fichier, header = T)
  stock$DATE = as.Date(stock$DATE, format = "%d/%m/%Y") #standardize time format
  stock = stock[! duplicated(index(stock), fromLast = T),] # Remove rows with a duplicated timestamp, 
                                                      # but keep the latest one
  stock$CLOSE = as.numeric(stock$CLOSE) #current numeric columns are of type character
  stock$OPEN = as.numeric(stock$OPEN)   #so need to convert into double
  stock$HIGH = as.numeric(stock$HIGH)   #otherwise quantmod functions won't work
  stock$LOW = as.numeric(stock$LOW)
  stock$VOLUME = as.numeric(stock$VOLUME)
  stock = xts(x = stock[,-1], order.by = stock[,1]) # convert to xts class
  return(stock)
}

relative.strength = function(stock, base = read_stock("vni.csv")){
  rs = Cl(stock) / Cl(base)
  rs = apply.weekly(rs, FUN = mean)
}
stock = read_stock("aaa.csv")

candleChart(stock, theme='white')
addRS = newTA(FUN=relative.strength,col='red', legend='RS')
addRS()

但是R给我发回了这个错误:

 Error in `/.default`(Cl(stock), Cl(base)) : non-numeric argument to binary operator

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

一个问题是" vni.csv"包含一个" Ticker"柱。由于xts对象是其核心的矩阵,因此您不能拥有不同类型的列。因此,您需要做的第一件事是确保您只保留" vni.csv"的OHLC和音量列。文件。我已将您的read_stock函数重构为:

read_stock = function(fichier) {
  # read and preprocess data
  stock <- read.csv(fichier, header = TRUE, as.is = TRUE)
  stock$DATE = as.Date(stock$DATE, format = "%d/%m/%Y")
  stock = stock[!duplicated(index(stock), fromLast = TRUE),]
  # convert to xts class
  stock = xts(OHLCV(stock), order.by = stock$DATE)
  return(stock)
}

接下来,看起来relative.strength函数中addRS的第一个参数作为矩阵传递,而不是xts对象。所以你需要转换为xts,但要注意stock对象的索引类与base对象的索引类相同。

然后,您需要确保每周rs个对象对stock中的每一天进行观察。您可以通过将每周数据与包含stock对象的所有索引值的空xts对象合并来实现。

所以我将你的relative.strength函数重构为:

relative.strength = function(stock, base) {
  # convert to xts
  sxts <- as.xts(stock)
  # ensure 'stock' index class is the same as 'base' index class
  indexClass(sxts) <- indexClass(base)
  index(sxts) <- index(sxts)
  # calculate relative strength
  rs = Cl(sxts) / Cl(base)
  # weekly mean relative strength
  rs = apply.weekly(rs, FUN = mean)
  # merge 'rs' with empty xts object contain the same index values as 'stock'
  merge(rs, xts(,index(sxts)), fill = na.locf)
}

现在,这段代码:

stock = read_stock("aaa.csv")
base = read_stock("vni.csv")
addRS = newTA(FUN=relative.strength, col='red', legend='RS')
candleChart(stock, theme='white')
addRS(base)

制作此图表:

enter image description here

答案 1 :(得分:0)

read_stock函数中的以下行导致了问题:

stock = xts(x = stock[,-1], order.by = stock[,1]) # convert to xts class

vni.csv在您的数据的第三列中包含实际的符号名称,因此当您放置stock[,-1]时,您实际上包含了一个字符列,并xts强制所有其他列也是字符。然后,R会提醒您将数字除以Cl(stock) / Cl(base)处的字符。以下是带有除法的错误消息的简单示例:

> x <- c(1,2)
> y <- c("A", "B")
> x/y
Error in x/y : non-numeric argument to binary operator

我建议您删除每行中包含vni.csv的{​​{1}}中的字符列,或修改名为"VNIndex"的函数,以更好地防范此类问题。