I can download stock/mutual fund data in R using tidyquant/quantmod; however, I need it in a format that a 3rd party application can read.
The download comes in this format:
Date FSAIX.Open FSAIX.High FSAIX.Low FSAIX.Close FSAIX.Volume FSAIX.Adjusted
12/16/1985 10 10 10 10 0 1.995289
However, I need it in this format:
Date Open High Low Close
12/16/1985 1.995289 1.995289 1.995289 1.995289
Notice that "FSAIX.Adjusted" became the Open, High, Low, and Close price on this day. Which, is fine since FSAIX is a mutual fund and only "trades" at one price each day. (For those curious the "_______.Adjusted" price means the closing price adjusted for splits, dividends, or distributions)
I have been able to 'hard code' R to give me data for one stock/mutual fund at a time. Here is my code:
##################################
library(tidyquant)
library(data.table)
# symbol list to download
fid_symbol <- c("FSAIX","FSAVX")
# Download symbol's data from yahoo finance
getSymbols(fid_symbol,src = 'yahoo',from = "1970-01-01", to = "2020-07-22")
# Change XTS file into a data table for both formatting
## and easier to export with the date included
my_DT <- as.data.table(FSAIX)
# Make new columns in the data table
my_DT[,"Open":=FSAIX.Adjusted]
my_DT[,"High":=FSAIX.Adjusted]
my_DT[,"Low":=FSAIX.Adjusted]
my_DT[,"Close":=FSAIX.Adjusted]
# Delete old clumns in the data table
my_DT <- my_DT[,-c(2:7)]
#Change index column name to date
setnames(my_DT,old="index",new="Date")
# write file to working directory
fwrite(my_DT,file = "FSAIX.csv")
#################################
This does give the desired output for the hard coded symbol - FSAIX. Yeah me!, I thought at first. But then the problems began when I tried to loop through a list, more than the 2 in the example, of mutual fund (stock) symbols. :-/ I have hit a brick wall for the last 1.5 weeks...
I recently tried to "replicate" some code I found on quantstrattrader's website, https://quantstrattrader.wordpress.com/2018/09/17/principal-component-momentum/ after a number of failed attempts on my own, and this code worked:
require(PerformanceAnalytics)
require(quantmod)
require(stats)
require(xts)
symbols <- c("SPY", "EFA", "EEM", "DBC", "HYG", "GLD", "IEF", "TLT")
# get free data from yahoo
rets <- list()
getSymbols(symbols, src = 'yahoo', from = '1990-12-31')
for(i in 1:length(symbols)) {
returns <- Return.calculate(Ad(get(symbols[i])))
colnames(returns) <- symbols[i]
rets[[i]] <- returns
}
rets <- na.omit(do.call(cbind, rets))
However, when I tried modify this to my needs it did not work. Here is my latest example:
### Fidelity Funds Dowload Script...
library(tidyquant)
library(data.table)
# symbol list to download
fid_symbol <- c("FSAIX","FSAVX")
# Download symbol's data from yahoo finance
getSymbols(fid_symbol,src = 'yahoo',from = "1970-01-01", to = "2020-07-22")
# My 'loop' to get the symbols in fid_symbol
for (i in 1:length(fid_symbol) )
{
# make data table
my_DT<- as.data.table(fid_symbol[[i]])
# get symbol from fid_symbol
sym <- fid_symbol[[i]]
# make new columns and delete old
my_DT[,"Open":=sym.Adjusted]
my_DT[,"High":=sym.Adjusted]
my_DT[,"Low":=sym.Adjusted]
my_DT[,"Close":=sym.Adjusted]
my_DT <- my_DT[,-c(2:7)]
# Change index column name to date
setnames(my_DT,old="index",new="Date")
# write file to working directory
fwrite(my_DT,file = paste0(sym,'.csv'))
}
################################
This most recent failed_attempt_11.0 gave the following error when run: " Error in eval(jsub, SDenv, parent.frame()) : object 'sym.Adjusted' not found "
I have also tried to write this technique as a function and use the "apply" family and that also failed....
Thank you for any help or suggestions in advance! Cheers, Chris