Using quantmod: quantitative financial modeling for R. First load xts and quantmod packages. For example, let;s get ticker symbol for Yahoo.

library(xts)
## Warning: package 'xts' was built under R version 3.0.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.0.3
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(quantmod)
## Warning: package 'quantmod' was built under R version 3.0.3
## Loading required package: Defaults
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 3.0.3
## Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols("YHOO")
##     As of 0.4-0, 'getSymbols' uses env=parent.frame() and
##  auto.assign=TRUE by default.
## 
##  This  behavior  will be  phased out in 0.5-0  when the call  will
##  default to use auto.assign=FALSE. getOption("getSymbols.env") and 
##  getOptions("getSymbols.auto.assign") are now checked for alternate defaults
## 
##  This message is shown once per session and may be disabled by setting 
##  options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details
## [1] "YHOO"
chartSeries(YHOO, theme = "white")

plot of chunk unnamed-chunk-1

chartSeries(YHOO, subset = "last 4 months", theme = "white")

plot of chunk unnamed-chunk-1

chartSeries(YHOO, subset = "2007::2008-01", theme = "white")

plot of chunk unnamed-chunk-1

chartSeries(YHOO, theme = chartTheme("white"))

plot of chunk unnamed-chunk-1

chartSeries(YHOO, TA = c(addVo(), addBBands()), theme = "white")  #add volume and Bollinger Bands from TTR

plot of chunk unnamed-chunk-1

addMACD()  #  add MACD indicator to current chart

plot of chunk unnamed-chunk-1

setTA()

Now get symbol for IBM

getSymbols("IBM")
## [1] "IBM"
chartSeries(IBM, theme = "white")

plot of chunk unnamed-chunk-2

reChart(subset = "2009", theme = "white", type = "candles")

plot of chunk unnamed-chunk-2

candleChart(IBM, theme = "white", TA = NULL)

plot of chunk unnamed-chunk-2

reChart(subset = "2014")

plot of chunk unnamed-chunk-2


getSymbols("YHOO", src = "google")  # from google finance 
## [1] "YHOO"
getSymbols("GOOG", src = "yahoo")  # from yahoo finance 
## [1] "GOOG"
getSymbols("DEXJPUS", src = "FRED")  # FX rates from FRED 
## [1] "DEXJPUS"
getSymbols("XPT/USD", src = "Oanda")  # Platinum from Oanda 
## Error: could not find function "getSymbols.Oanda"
setSymbolLookup(YHOO = "google", GOOG = "yahoo")
setSymbolLookup(DEXJPUS = "FRED")
setSymbolLookup(XPTUSD = list(name = "XPT/USD", src = "oanda"))
saveSymbolLookup(file = "mysymbols.rda")
getSymbols(c("YHOO", "GOOG", "DEXJPUS", "XPTUSD"))
## [1] "YHOO"    "GOOG"    "DEXJPUS" "XPTUSD"
getSymbols("AAPL", src = "yahoo")
## [1] "AAPL"
barChart(AAPL, theme = "white")

plot of chunk unnamed-chunk-4

candleChart(AAPL, multi.col = TRUE, theme = "white")

plot of chunk unnamed-chunk-4

reChart(subset = "2014", theme = "white")

plot of chunk unnamed-chunk-4

Define the Guppy Multiple Moving Average function.

GMMA <- function(x) {
    fastMA <- c(3, 5, 8, 10, 12, 15)
    slowMA <- c(30, 35, 40, 45, 50, 60)
    x <- sapply(c(fastMA, slowMA), function(xx) EMA(x, xx))
    return(x)
}

create an addGuppy function with newTA

addGuppy <- newTA(FUN = GMMA, preFUN = Cl, col = c(rep(3, 6), rep("#333333", 
    6)), legend = "”GMMA")
class(addGuppy)
## [1] "function"
addGuppy <- newTA(FUN = GMMA, preFUN = Cl, col = c(rep(3, 6), rep("#333333", 
    6)), legend = "GMMA")
class(addGuppy)
## [1] "function"
addGuppy()

plot of chunk unnamed-chunk-7

addGuppy(on = -1, col = c(rep("blue", 6), rep("black", 6)))

plot of chunk unnamed-chunk-7

Now get all symbols.

search()
##  [1] ".GlobalEnv"        "package:quantmod"  "package:TTR"      
##  [4] "package:Defaults"  "package:xts"       "package:zoo"      
##  [7] "package:knitr"     "package:stats"     "package:graphics" 
## [10] "package:grDevices" "package:utils"     "package:datasets" 
## [13] "package:methods"   "Autoloads"         "package:base"
attachSymbols(prefix = "E.")
search()
##  [1] ".GlobalEnv"        "DDB:Yahoo"         "package:quantmod" 
##  [4] "package:TTR"       "package:Defaults"  "package:xts"      
##  [7] "package:zoo"       "package:knitr"     "package:stats"    
## [10] "package:graphics"  "package:grDevices" "package:utils"    
## [13] "package:datasets"  "package:methods"   "Autoloads"        
## [16] "package:base"
str(ls("DDB:Yahoo"))
##  chr [1:7444] "E.A" "E.AA" "E.AADR" "E.AAIT" "E.AAL" ...
str(E.A)
## An 'xts' object on 2007-01-03/2014-05-20 containing:
##   Data: num [1:1858, 1:6] 35 34.3 34.3 34 34.1 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "A.Open" "A.High" "A.Low" "A.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2014-05-20 23:22:47"
system.time(E.AKAM)
##    user  system elapsed 
##    0.09    0.00    0.71
str(quantmod:::DDB_Yahoo())
## List of 3
##  $ name: chr "DDB:Yahoo"
##  $ src : chr "yahoo"
##  $ db  : chr [1:7444] "AAIT" "AAL" "AAME" "AAOI" ...
##  - attr(*, "class")= chr "DDB"

Another example on charting MOT

getSymbols("MOT", src = "google")
## [1] "MOT"
barChart(MOT, theme = "white")

plot of chunk unnamed-chunk-9

reChart(subset = "2014", theme = "white")

plot of chunk unnamed-chunk-9

candleChart(MOT, theme = "white")

plot of chunk unnamed-chunk-9

reChart(subset = "2013/2014")
reChart(subset = "2013/2014", theme = "white")

plot of chunk unnamed-chunk-9

addGuppy()

plot of chunk unnamed-chunk-9

reChart(subset = "2014", theme = "white")

plot of chunk unnamed-chunk-9

lineChart(MOT, subset = "2014", theme = "white", TA = NULL)

plot of chunk unnamed-chunk-9

lineChart(MOT, subset = "2014", theme = "white")

plot of chunk unnamed-chunk-9

chartSeries(MOT, subset = "2014", theme = "white")

plot of chunk unnamed-chunk-9

Yahoo, Google, Facebook and twitter

chartSeries(YHOO, theme = "white")

plot of chunk unnamed-chunk-10

reChart(subset = "2014", theme = "white")
chartSeries(GOOGL, theme = "white")
## Error: object 'GOOGL' not found
reChart(subset = "2014", theme = "white")

plot of chunk unnamed-chunk-10

chartSeries(FB, theme = "white")
## Error: object 'FB' not found
reChart(subset = "2014", theme = "white")
chartSeries(TWT, theme = "white")
## Error: object 'TWT' not found
reChart(subset = "2014", theme = "white")
addGuppy()

plot of chunk unnamed-chunk-10

Candle chart FB

candleChart(FB, theme = "white")
## Error: object 'FB' not found
reChart(subset = "2014", theme = "white")

plot of chunk unnamed-chunk-11

Chart Fedilty retirement 2015 mutual fund.

getSymbols("FFVFX")
## [1] "FFVFX"
candleChart(FFVFX, theme = "white")

plot of chunk unnamed-chunk-12

reChart(subset = "2014", theme = "white")

plot of chunk unnamed-chunk-12