I want to download stock data from different dates, normalize their returns and align the time to time 0 and plot the series in a single plot so that I can compare visually their performance over time.
I had the following scripts
library(quantmod)
library(ggplot2)
Symbols=spl('AAPL,C')
dates_from=spl('2000-1-1,2008-1-1')
dates_to=spl('2000-12-1,2008-12-1')
noS = length(Symbols)
dataEnv<-new.env()
for(i in 1:noS) getSymbols(Symbols[[i]], src = "yahoo", from=dates_from[[i]], to=dates_to[[i]],env=dataEnv, auto.assign = T, use.Adjusted=T)
for(i in ls(dataEnv)) dataEnv[[i]] = Ad(dataEnv[[i]])#Use adjusted data
# Normalize the series to start from the same point (time 0)
for (i in ls(dataEnv)) {
dataEnv[[i]] = dataEnv[[i]]/ coredata(dataEnv[[i]])[1]
}
for (i in ls(dataEnv)) {
dataEnv[[i]] = data.frame(Date = 1:length(dataEnv[[i]]), Price = coredata(dataEnv[[i]]))
}
# Ensure column names are correct and create a single plot for each symbol in dataEnv
for (symbol in ls(dataEnv)) {
df <- dataEnv[[symbol]]
names(df) <- c("Date", "Price") # Ensure correct column names
plot <- ggplot(df, aes(x = Date, y = Price)) +
geom_line() +
ggtitle(paste("Normalized Price Series for", symbol)) +
xlab("Time") +
ylab("Normalized Price") +
theme_minimal()
print(plot)
}
# I want to draw all the series into a single plot instead
I was able to plot multiple separate plots but I faced problems combining the different series into a single plot. Various solutions I tried using chatGPT sort of combine the dataframe with alot of NA because the series had different dates.
Hope some kind bros can help.
Thanks alot