使用R进行财务管理

时间:2017-05-05 21:08:36

标签: r quantmod quantitative-finance

我正在尝试使用:

library(pdfetch)
tickers <- c('FB','YHOO'.....etc long list of tickers)
pdfetch_YAHOO(tickers, fields = c("adjclose"),
    from = as.Date("2017-04-01"),
    to = as.Date("2017-04-30"))

我的代码不起作用。如果有人可以告诉我如何将这些信息输出到图表,我将非常感激!

2 个答案:

答案 0 :(得分:0)

好的,所以我不确定这项练习的目标是什么,但我会采取行动。因为我知道,我将使用quantmod库。我在这里做的是设置一个代码清单,获取那些代码,创建一个功能,用于管理数据,将该数据转换为关闭值的xts对象,并将其插入到dygraphs中,自动解释xts数据。

library(dygraphs)
library(quantmod)
library(data.table)
library(magrittr)

tickers <- c('FB','YHOO')

temp <- getSymbols(tickers,
          from = as.Date("2001-04-01"),
          to = as.Date("2017-04-30"))

munge_data <- function(tickers){

  data <- lapply(tickers, get) %>% 
    lapply(as.data.table) %>% 
    lapply(function(df) setnames(df, colnames(df),
                                 c("Date","Open","High",
                                   "Low","Close","Volume",
                                   "Adjusted")))
  names(data) <- tickers

  rbindlist(data,idcol = T)

}

my_data <- tickers %>% 
  munge_data %>% 
  .[,.(Date,.id,Close)] %>% 
  dcast(...~.id, value.var = "Close") %>% 
  as.xts.data.table()

dygraph(my_data)

这将采用任意数量的代码并绘制它们(尽管在图表上区分7000个代码符号是不可能的。)

答案 1 :(得分:0)

这是使用ggplot2进行绘图的方法。我们将数据下载到一个列表中,其中每个元素都是一个股票代码。我们将其转换为数据框。在对ggplot的调用中,我们将数据从宽格式转换为长格式并缩放数据,以便所有接近的值都相对于句点第一天的接近值。这使得每只股票的规模相同:

library(pdfetch)
library(tidyverse)
library(xts)

tickers = c('FB','YHOO','GENE','AAPL','MSFT','LUV','AMZN','AIV','BUD','HPQ','XOM','UAL')

# Download tickers into a list
df = map(tickers, function(tt) {
  pdfetch_YAHOO(tt, fields = c("adjclose"),
                from = as.Date("2017-04-01"),
                to = as.Date("2017-04-30")) 
}) 

# Convert to a data frame
df = do.call(cbind, df)
df = data.frame(date=as.Date(index(df)), df)

# Convert data frame to long format and plot
ggplot(gather(df, ticker, close, -date) %>% 
         group_by(ticker) %>%
         mutate(close=close/close[1]), 
       aes(date, close)) +
  geom_hline(yintercept=1, colour="grey30", size=0.2) +
  geom_line(size=0.4) +
  facet_grid(~ ticker) +
  theme_classic(base_size=9) +
  theme(strip.background=element_blank(),
        axis.text.x=element_blank()) +
  scale_y_continuous(limits=c(0.94, 1.06), breaks=seq(0,2,0.05)) +
  labs(x="April") +
  guides(colour=FALSE)

enter image description here

您还可以使用颜色显示股票在月初时低于/高于其值。为此我们使用插值使颜色总是在一条线交叉时改变1.我们也使用更小的主题。

# Dates to interpolate at
dts = seq(min(df$date), max(df$date), length=500)

# Create an interpolated data frame (note this data frame will have 7000*500 rows if
# you do this for 7000 symbols (not that you'd want to plot that many stocks 
# at once anyway)
df.interp = gather(df, ticker, close, -date) %>%
  split(. , .$ticker) %>%
  map_df(~ data.frame(approx(.x$date, .x$close, xout=dts), ticker=.x$ticker[1])) %>%
  rename(date=x, close=y)

ggplot(df.interp %>% 
         group_by(ticker) %>%
         mutate(close=close/close[1]), 
       aes(date, close, colour=close > 1, group=ticker)) +
  geom_hline(yintercept=1, colour="grey30", size=0.2) +
  geom_line(size=0.6) +
  facet_grid(~ ticker) +
  theme_classic(base_size=9) +
  theme(strip.background=element_blank(),
        axis.text.x=element_blank(),
        axis.line.x=element_blank(),
        axis.ticks.x=element_blank()) +
  scale_y_continuous(limits=c(0.94, 1.06), breaks=seq(0,2,0.05)) +
  labs(x="April 2017") +
  guides(colour=FALSE)

enter image description here