Ich habe zwei XTS-Objekte: stock
und base
. Ich berechne die relative Stärke (das ist einfach das Verhältnis von Schlusskurs der Aktie und des Basisindex) und ich möchte die wöchentliche relative Stärke außerhalb des Candlestick-Musters darstellen. Die Links für die Daten sind here und here.Plot-Funktion außerhalb der Candlestick-Muster in 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()
jedoch R gibt mir diese Fehlermeldung:
Error in `/.default`(Cl(stock), Cl(base)) : non-numeric argument to binary operator
Wie kann ich dieses Problem beheben?
Wo sind 'stock' und' Basis "kommt aus? Machen Sie bitte ein vollständig reproduzierbares Beispiel. – Spacedman
Bitte warten Sie, ich lade die Dateien und alle meinen Code – SiXUlm
Ich habe meine Frage aktualisiert. – SiXUlm