2017-01-23 162 views
-1

我想將幾個xts對象合併到一個xts對象中。這樣我就可以得到收盤價格上的對象之間的相關矩陣。將多個XTS對象合併爲一個XTS對象

XTS objects in global environment

下面的代碼拉低外匯數據

require(xts) 

symbols <- c("AUDJPY", "AUDUSD", "CHFJPY", "EURCHF", "EURGBP", "EURJPY", 
      "EURUSD", "GBPCHF", "GBPJPY", "GBPUSD", "NZDUSD", "USDCAD", 
      "USDCHF", "USDJPY", "XAGUSD", "XAUUSD") 

fxhistoricaldata <- function(Symbol, timeframe, download = FALSE, bandwidth) { 

    # setup temp folder 
    temp.folder <- tempdir() 
    filename <- paste(temp.folder, '/',"fxhistoricaldata_",Symbol ,"_" ,timeframe,".csv", sep='') 

    if(download) { 
    downloadfile <- paste("http://api.fxhistoricaldata.com/v1/indicators?instruments=" ,Symbol ,"&expression=open,high,low,close&item_count=10000&format=csv&timeframe=" ,timeframe,sep='') 
    download.file(downloadfile, filename, mode = 'wb') 
    } 

    tempdf <- read.csv(filename) 
    colnames(tempdf) <- c("Curr","Date","Open","High","Low","Close") 
    tempdf <- tempdf[c("Date","Open","High","Low","Close")] 
    result <- xts(tempdf[,-1], order.by=as.POSIXct(tempdf[,1])) 

    return(result) 
} 

AUDJPY <- fxhistoricaldata('AUDJPY' ,'day',download=T,5) 
AUDUSD <- fxhistoricaldata('AUDUSD' ,'day',download=T,5) 
CHFJPY <- fxhistoricaldata('CHFJPY' ,'day',download=T,5) 
EURCHF <- fxhistoricaldata('EURCHF' ,'day',download=T,5) 
EURGBP <- fxhistoricaldata('EURGBP' ,'day',download=T,5) 
EURJPY <- fxhistoricaldata('EURJPY' ,'day',download=T,5) 
EURUSD <- fxhistoricaldata('EURUSD' ,'day',download=T,5) 
GBPCHF <- fxhistoricaldata('GBPCHF' ,'day',download=T,5) 
GBPJPY <- fxhistoricaldata('GBPJPY' ,'day',download=T,5) 
GBPUSD <- fxhistoricaldata('GBPUSD' ,'day',download=T,5) 
NZDUSD <- fxhistoricaldata('NZDUSD' ,'day',download=T,5) 
USDCAD <- fxhistoricaldata('USDCAD' ,'day',download=T,5) 
USDCHF <- fxhistoricaldata('USDCHF' ,'day',download=T,5) 
USDJPY <- fxhistoricaldata('USDJPY' ,'day',download=T,5) 
XAGUSD <- fxhistoricaldata('XAGUSD' ,'day',download=T,5) 
XAUUSD <- fxhistoricaldata('XAUUSD' ,'day',download=T,5) 

我想要的結果會是這個樣子與收盤價的符號名以下,但對於所有的符號名

  AUDJPY AUDUSD 
2016-01-01 1.200  1.300 
2016-01-02 1.21  1.31 
所述XTS之一的

實施例對象

Example of xts object

+1

請提供[可重現的示例](http://stackoverflow.com/q/5963269/271616)。數據圖片沒有幫助。 –

+0

更新後的代碼謝謝 –

回答

1

這可以通過使用quantmod附加qmao輕鬆完成。以幾個股票代碼爲例,下載數據(或從文件導入數據)爲xts-objects,然後一次創建一個包含價格或回報的對象。讓我們假設你想從日常的回報創建一個相關矩陣:

library(qmao) 
tickers <- c('MSFT','AAPL','AMZN') 
getsSymbols(tickers, from = '2010-01-01') 
# xts-object of daily returns (RF stands for `Return Frame`,PF returns a 
# Price Frame) 
returns <- RF(tickers, silent = TRUE, type = 'discrete') 

> tail(returns) 
        MSFT   AAPL   AMZN 
2017-01-10 -0.0003192848 0.001008513 -0.001279876 
2017-01-11 0.0091025233 0.005373176 0.003920085 
2017-01-12 -0.0091786360 -0.004175365 0.018297408 
2017-01-13 0.0014374700 -0.001760998 0.004301657 
2017-01-17 -0.0027113556 0.008064508 -0.009080505 
2017-01-18 -0.0004797537 -0.000083350 -0.002766377 

要獲得相關矩陣:

> cor(returns,use = 'pairwise.complete.obs') 
      MSFT  AAPL  AMZN 
MSFT 1.0000000 0.4655617 0.4701170 
AAPL 0.4655617 1.0000000 0.4390303 
AMZN 0.4701170 0.4390303 1.0000000 

看一看功能PFRF它們結構緊湊,非常有用。

0

考慮構建XTS對象的列表,用相應的符號,那麼在運行merge.xts前綴列名:

xtsList <- lapply(symbols, function(s) { 
    df <- get(s) 
    colnames(df) <- paste0(s, "_", colnames(df)) 
    return(df) 
}) 

masterxts <- do.call(merge, xtsList) 
0

發現使用getSymbols一個更好的解決方案。 感謝您的幫助。

require(lubridate) 
require(quantmod) 


symbols <- c("AUD/JPY", 
      "AUD/USD", 
      "CHF/JPY", 
      "EUR/CHF", 
      "EUR/GBP", 
      "EUR/JPY", 
      "EUR/USD", 
      "GBP/CHF", 
      "GBP/JPY", 
      "GBP/USD", 
      "NZD/USD", 
      "USD/CAD", 
      "USD/CHF", 
      "USD/JPY", 
      "XAG/USD", 
      "XAU/USD" 
) 

fixedsymbols <- lapply(symbols, function(x) { 
        gsub("/", "", x) 
       }) 

getSymbols(symbols,src="oanda", from="2014-08-05", to="2017-01-17") 

xtsList <- lapply(fixedsymbols, function(s) { 
    df <- get(s) 
    colnames(df) <- colnames(df) 
    return(df) 
}) 

masterxts <- do.call(merge, xtsList)