In an attempt to get more comfortable with tidyverse and functional programming, here is a quick post to demonstrated the much sleeker and explainable code using these concepts.

This post is motivated by this awesome talk by Hadley Wickham:


## Obtain symbol data
getSymbols(ticker, from="2010-01-01")

# apply functional programming concepts
# credit:
# credit:

tickers <- c("FB", "AMZN", "AAPL", "NFLX", "GOOG", "SPY")

# get the data
map(tickers, ~ assign(.x, getSymbols(.x, env=.GlobalEnv)))

# this also works
# walk(tickers, ~ assign(.x, getSymbols(.x, env=.GlobalEnv)))

# plot all the charts
map(tickers, ~ plot(Cl(get(.x)), type = "l", main = .x)

# merge the data
stocks <- map(tickers, ~ Cl(get(.x)) %>% set_names(.x))
stocks <-'cbind', stocks)

plot.zoo(stocks, main = "All stocks")