設定每檔商品的預設引數

在 R 匯入與管理金融資料

Joshua Ulrich

Quantitative Analyst & quantmod Co-Author and Maintainer

用 setSymbolLookup() 設定資料來源

setSymbolLookup(AAPL = "google")
aapl <- getSymbols("AAPL", auto.assign = FALSE)
str(aapl)  # 注意 'src' 屬性
An 'xts' object on 2007-01-03/2017-02-22 containing:
  Data: num [1:2552, 1:5] 12.3 12 12.2 12.3 12.3 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "google"
 $ updated: POSIXct[1:1], format: "2017-02-23 14:16:55"

在 R 匯入與管理金融資料

用 setSymbolLookup() 設定其他引數

setSymbolLookup(MSFT = list(src = "google", from = "2016-01-01"))
msft <- getSymbols("MSFT", auto.assign = FALSE)
str(msft)  # 注意 'src' 屬性與第一筆日期
An 'xts' object on 2016-01-04/2017-02-27 containing:
  Data: num [1:290, 1:5] 54.3 54.9 54.3 52.7 52.4 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "MSFT.Open" "MSFT.High" "MSFT.Low" "MSFT.Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "google"
 $ updated: POSIXct[1:1], format: "2017-02-23 14:20:21"
在 R 匯入與管理金融資料

儲存與還原預設(1)

# 設定預設值
setSymbolLookup(AAPL = "google")
# 驗證預設已變更
getSymbolLookup()
$AAPL
$AAPL$src
"google"
# 儲存對應表
saveSymbolLookup("symbol_lookup.rda")
# 移除對應表
setSymbolLookup(AAPL = NULL)
在 R 匯入與管理金融資料

儲存與還原預設(2)

# 驗證預設已移除
getSymbolLookup()
named list()
# 載入對應表
loadSymbolLookup("symbol_lookup.rda")
# 驗證預設已還原
getSymbolLookup()
$AAPL
$AAPL$src
"google"
在 R 匯入與管理金融資料

一起來練習吧!

在 R 匯入與管理金融資料

Preparing Video For Download...