การตั้งค่าอาร์กิวเมนต์ดีฟอลต์สำหรับ getSymbols()

การนำเข้าและจัดการข้อมูลทางการเงินใน R

Joshua Ulrich

Quantitative Analyst & quantmod Co-Author and Maintainer

"method" ของ getSymbols()

  • getSymbols() ไม่มีโค้ดสำหรับนำเข้าข้อมูลโดยตรง
  • โค้ดของแต่ละแหล่งข้อมูลอยู่ใน "method" getSymbols.[source]
  • ตัวอย่างเช่น:
# You call getSymbols()
getSymbols("GDP", src = "FRED")

# getSymbols() calls source "method"
getSymbols.FRED("GDP")
  • ไม่ควรเรียก "method" ของ getSymbols() โดยตรง
การนำเข้าและจัดการข้อมูลทางการเงินใน R

ใช้ setDefaults() เพื่อเปลี่ยนแหล่งข้อมูลดีฟอลต์

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"
การนำเข้าและจัดการข้อมูลทางการเงินใน R

setDefaults()

  • กำหนดอาร์กิวเมนต์ดีฟอลต์ใหม่ด้วยคู่ name = value
  • ส่งผลต่อพฤติกรรมของ getSymbols() เท่านั้น
  • จัดเก็บค่าไว้ใน options() ระดับ global
การนำเข้าและจัดการข้อมูลทางการเงินใน R

อาร์กิวเมนต์อื่น ๆ

  • ดูอาร์กิวเมนต์ของ source method ใน getSymbols() ได้ด้วย
    • args(): args(getSymbols.yahoo)
    • help(): help("getSymbols.yahoo")
การนำเข้าและจัดการข้อมูลทางการเงินใน R

Default from and to values

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"
การนำเข้าและจัดการข้อมูลทางการเงินใน R

getDefaults()

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

$to
"'2016-12-31'"
  • ค่าที่คืนกลับมาไม่ได้หมายความว่าฟังก์ชันเหล่านั้นรองรับดีฟอลต์ที่ผู้ใช้กำหนดได้
setDefaults(load, 
            file = "my_file.RData")

# Will not alter behavior
getDefaults(load)
$file
"'my_file.RData'"
การนำเข้าและจัดการข้อมูลทางการเงินใน R

มาฝึกกันเถอะ!

การนำเข้าและจัดการข้อมูลทางการเงินใน R

Preparing Video For Download...