Portfolio Optimization going wrong (GA)

library("quantmod")
myStocks <- c("AAPL", "PLTR", "TSLA", "SBUX", "BA", "SLV", "GDX", "ATVI", "BAC")
getSymbols(myStocks, src="yahoo", from="2020-01-01", to="2022-01-01")
myRetData <- data.frame(as.xts(merge(dailyReturn(AAPL), dailyReturn(TSLA), dailyReturn(SBUX), 
                                     dailyReturn(BA), dailyReturn(SLV), dailyReturn(PLTR), 
                                     dailyReturn(GDX), dailyReturn(ATVI), dailyReturn(BAC))))
colnames(myRetData) <- myStocks
myRetData

My issue is that SLV return values all appear as N/A. If I change the year from 2020 - 2022 to 2021-2022 everything works fine but for some reason it won't work over a two year time frame. If I remove SLV from the portfolio, a different column (eg - BA) will change all the return values to N/A instead. Is there any way around this and what is wrong with my code? Thanks for any help

you will find it hard to diagnose your issue because the order you list and name your stocks

myStocks <- c("AAPL", "PLTR", "TSLA", "SBUX", "BA", "SLV", "GDX", "ATVI", "BAC")

differs from how you collect the data

dailyReturn(AAPL), dailyReturn(TSLA), dailyReturn(SBUX), 
                                     dailyReturn(BA), dailyReturn(SLV), dailyReturn(PLTR), 
                                     dailyReturn(GDX), dailyReturn(ATVI), dailyReturn(BAC)

and later

colnames(myRetData) <- myStocks

Wow, it was that simple. Thanks for your help!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.