如何获得R中的货币汇率 [英] How to get currency exchange rates in R

查看:210
本文介绍了如何获得R中的货币汇率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何R包/功能可以实时获取汇率,例如来自Google财经?如果有的话,宁愿避免使用RCurl或其他解析器。

Are there are any R packages/functions to get exchange rates in real time, e.g. from Google Finance? Would prefer to avoid RCurl or other parsers if something's already out there.

具体来说,给定 from和 to货币符号的向量,我想知道费率。

Specifically, given vectors of "from" and "to" currency symbols, I'd like to know the rates. Something like:

IdealFunction(c("CAD", "JPY", "USD"), c("USD", "USD", "EUR"))


推荐答案

您可以使用 quantmod 来获取雅虎报价。 (我不确定yahoo FX报价的延迟时间或更新频率)。

You can use quantmod to get yahoo quotes. (I'm not sure how delayed yahoo FX quotes are, or how often they're updated.)

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进行实时,毫秒时间戳的报价。 (请注意,您必须使用市场惯例;例如,USD / JPY而不是JPY / USD)

Or TFX for real-time, millisecond timestamped quotes if you sign up for a free account. (note you have to use market convention; i.e. USD/JPY instead of JPY/USD)

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的包装器函数)

Or if you have an Interactive Brokers account, you can use the IBrokers package, or my twsInstrument package (which is basically just wrappers for IBrokers functions)

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

这篇关于如何获得R中的货币汇率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆