Setarea argumentelor implicite pentru getSymbols()

Importul și gestionarea datelor financiare în R

Joshua Ulrich

Quantitative Analyst & quantmod Co-Author and Maintainer

„Metodele" getSymbols()

  • getSymbols() nu conține cod pentru importul datelor
  • Codul pentru fiecare sursă de date se află într-o „metodă" getSymbols.[source]
  • De exemplu:
# You call getSymbols()
getSymbols("GDP", src = "FRED")

# getSymbols() calls source "method"
getSymbols.FRED("GDP")
  • Utilizatorii nu ar trebui să apeleze direct „metodele" getSymbols()
Importul și gestionarea datelor financiare în R

Utilizarea setDefaults() pentru schimbarea sursei de date implicite

setDefaults(getSymbols, src = "FRED")
gdp <- getSymbols("GDP", auto.assign = FALSE) 
# Note the 'src' attribute
str(gdp)
 An 'xts' object on 1947-01-01/2016-10-01 containing:
   Data: num [1:280, 1] 243 246 250 260 266 ...
  - attr(*, "dimnames")=List of 2
   ..$ : NULL
   ..$ : chr "GDP"
   Indexed by objects of class: [Date] TZ: UTC
   xts Attributes:  
 List of 2
  $ src    : chr "FRED"
  $ updated: POSIXct[1:1], format: "2017-02-13 08:46:50"
Importul și gestionarea datelor financiare în R

setDefaults()

  • Setează noi argumente implicite folosind perechi name = value
  • Modifică comportamentul doar pentru getSymbols()
  • Stochează valorile în options() global
Importul și gestionarea datelor financiare în R

Alte argumente

  • Găsiți argumentele formale pentru o metodă sursă getSymbols()
    • Utilizați args(): args(getSymbols.yahoo)
    • Utilizați help(): help("getSymbols.yahoo")
Importul și gestionarea datelor financiare în R

Valorile implicite from și to

args(getSymbols.yahoo)
 function (Symbols, env, return.class = "xts", index.class = "Date", 
     from = "2007-01-01", to = Sys.Date(), ...)
setDefaults(getSymbols.yahoo, from = "2016-01-01", to = "2016-12-31") 
aapl <- getSymbols("AAPL", auto.assign = FALSE) 
str(aapl)
 An 'xts' object on 2016-01-04/2016-12-30 containing:
   Data: num [1:252, 1:6] 102.6 105.8 100.6 98.7 98.6 ...
  - attr(*, "dimnames")=List of 2
   ..$ : NULL
   ..$ : chr [1:6] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
   Indexed by objects of class: [Date] TZ: UTC
   xts Attributes:  
 List of 2
  $ src    : chr "yahoo"
  $ updated: POSIXct[1:1], format: "2017-02-13 08:46:50"
Importul și gestionarea datelor financiare în R

getDefaults()

getDefaults()
"getSymbols.yahoo"
getDefaults(getSymbols.yahoo)
$from
"'2016-01-01'"

$to
"'2016-12-31'"
  • Valorile returnate nu implică faptul că funcțiile acceptă valori implicite definite de utilizator
setDefaults(load, 
            file = "my_file.RData")

# Will not alter behavior
getDefaults(load)
$file
"'my_file.RData'"
Importul și gestionarea datelor financiare în R

Să exersăm!

Importul și gestionarea datelor financiare în R

Preparing Video For Download...