有没有办法直接从yahoo finance导入时间序列数据到R而不是每次下载?现在我在excel下载每个时间序列,然后通过read.csv导入。
答案 0 :(得分:0)
查看包quantmod
,例如函数getSymbols('MSFT',src='yahoo')
相应的小插图。
答案 1 :(得分:0)
当然有!!
aapl_2 <- read.csv(file = "C:/Users/rshuell001/Desktop/Coding/R Programming/Stock Time Series Plots/aapl.csv", header = TRUE, stringsAsFactors = FALSE)
# Reverse the entries
aapl_2 <- aapl_2[rev(rownames(aapl_2)), ]
aapl_close <- aapl_2[, "Close"]
summary(aapl_close)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 12.94 24.69 38.13 46.80 53.61 199.80
############################
##############################
# Using the quantmod package #
##############################
# Load the quantmod packages after installing it locally.
library(quantmod)
AAPL <- getSymbols("AAPL", auto.assign=FALSE)
head(AAPL)
##########################
# Charting with quantmod #
##########################
# Adding some technical indicators on top of the original plot
chartSeries(AAPL, subset='2010::2010-04',
theme = chartTheme('white'),
TA = "addVo(); addBBands()")
reChart(subset='2009-01-01::2009-03-03')
chartSeries(AAPL, subset='2011::2012',
theme = chartTheme('white'),
TA = "addBBands(); addDEMA()")
addVo()
addDPO()
# Initial chart plot with no indicators
chartSeries(AAPL, theme = chartTheme('white'), TA = NULL)
# Custom function creation
my_indicator <- function(x) {
return(x + 90)
}
add_my_indicator <- newTA(FUN = my_indicator, preFUN=Cl,
legend.name = "My Fancy Indicator", on = 1)
add_my_indicator()
#########################
# Graphing wiht ggplot2 #
#########################
# Create a matrix with price and volume
df <- AAPL[, c("AAPL.Adjusted", "AAPL.Volume")]
names(df) <- c("price", "volume")