如何获取R货币汇率

问题描述 投票:0回答:3

是否有任何R包/函数可以实时获取汇率,例如来自谷歌财经?如果已经有东西的话,宁愿避免 RCurl 或其他解析器。

具体来说,给定“从”和“到”货币符号的向量,我想知道汇率。比如:

IdealFunction(c("CAD", "JPY", "USD"), c("USD", "USD", "EUR"))
r currency
3个回答
29
投票

您可以使用 quantmod 获取雅虎报价。 (我不确定雅虎外汇报价有多延迟,或者更新频率如何。)

library(quantmod)
from <- c("CAD", "JPY", "USD")
to <- c("USD", "USD", "EUR")
getQuote(paste0(from, to, "=X"))
#                  Trade Time   Last Change % Change Open High Low Volume
#CADUSD=X 2014-11-01 08:23:00 0.8875    N/A      N/A  N/A  N/A N/A    N/A
#JPYUSD=X 2014-11-01 08:23:00 0.0089    N/A      N/A  N/A  N/A N/A    N/A
#USDEUR=X 2014-11-01 08:23:00 0.7985    N/A      N/A  N/A  N/A N/A    N/A

如果您注册了免费帐户,则可以使用 TFX 获取实时、毫秒级时间戳报价。 (请注意,您必须使用市场惯例;即美元/日元而不是日元/美元)

library(TFX)
pairs <- paste(to, from, sep="/")
QueryTrueFX(ConnectTrueFX(pairs, "validUser", "anytext"))
#   Symbol Bid.Price Ask.Price      High       Low               TimeStamp
#1 USD/CAD   1.12651   1.12665   1.12665   1.12651 2014-10-31 20:45:00.559
#2 USD/JPY 112.34600 112.35900 112.35900 112.34600 2014-10-31 20:45:00.134
#3 EUR/USD   1.25234   1.25253   1.25253   1.25234 2014-10-31 20:45:00.598

或者,如果您有盈透证券账户,您可以使用 IBrokers 包,或我的 twsInstrument 包(基本上只是 IBrokers 函数的包装器)

library(twsInstrument)
getQuote(paste0(to, from), src="IB") # only works when market is open.

7
投票

您可以使用

historical_exchange_rates()
 库中的 
priceR

例如获取 2010 年至 2020 年每日澳元兑美元汇率:

# install.packages("priceR")
library(priceR)

cur <- historical_exchange_rates("AUD", to = "USD",
                          start_date = "2010-01-01", end_date = "2020-06-30")
tail(cur)

       date one_AUD_equivalent_to_x_USD
 2020-06-25                    0.688899
 2020-06-26                    0.686340
 2020-06-27                    0.686340
 2020-06-28                    0.685910
 2020-06-29                    0.687335
 2020-06-30                    0.690166

dim(cur)
[1] 3834    2


# Plot USD vs AUD last 10 years
library(ggplot2)
library(tidyverse)

cur %>% 
  tail(365 * 10) %>% 
  rename(aud_to_usd = one_AUD_equivalent_to_x_USD) %>%  
  mutate(date = as.Date(date)) %>% 
  ggplot(aes(x = date, y = aud_to_usd, group = 1)) +
  geom_line() +
  geom_smooth(method = 'loess') + 
  theme(axis.title.x=element_blank(),
        axis.ticks.x=element_blank()) + 
  scale_x_date(date_labels = "%Y", date_breaks = "1 year")

更多示例可以在这里这里找到。


6
投票

看起来

TFX
quantmod
有此功能(感谢 @RStudent 和 @KFB 的提示)。我更喜欢
quantmod
,因为它不需要设置帐户,但 AFAICT 没有像我正在寻找的矢量化当前快照功能。这个函数
GetExchangeRates
的作用是:

GetExchangeRates <- function(from_curr, to_curr, dt) {
  require(quantmod)
  exchanges <- paste0(from_curr, "/", to_curr)
  result <- mapply(
    function(from_curr, to_curr) {      
      getFX(paste0(from_curr, "/", to_curr),
        from = dt,
        to = dt,
        src = "yahoo",
        auto.assign = FALSE
      )
    },
    from_curr, to_curr
  )
  names(result) <- exchanges
  return(result)
}

TestExchangeRates <- function() {
  from_curr <- c("CAD", "JPY", "USD")
  to_curr <- c("USD", "USD", "EUR")
  dt <- Sys.Date() -1
  GetExchangeRates(from_curr, to_curr, dt)
}

TestExchangeRates()
#    CAD/USD    JPY/USD    USD/EUR 
# 0.72915600 0.00667232 0.94388600 
© www.soinside.com 2019 - 2024. All rights reserved.