我有兩個xts對象:stock
和base
。我計算相對強弱(這只是股票收盤價和基礎指數的比率),我想繪製蠟燭圖模式外的每週相對強度。數據的鏈接是here和here。在R中蠟燭圖模式外的繪圖功能
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
我該如何解決這個問題?
哪裏'stock'和'基地'來自?請做一個完全可重複的例子。 – Spacedman
請稍候,我上傳的文件和我所有的代碼 – SiXUlm
我已更新我的問題。 – SiXUlm