2017-11-07 9 views
0

Yahooの財務APIの提供をやめさせるためにYahooが一方的に決定し、有名な「403禁じられた」メッセージを受け取ったとき、私たちの多くは大きな問題を抱えていました。将来の市場や株式データの調査については、finance.yahoo.comを参照してください」と述べています。「Yahooの利用規約に違反してこのサービスが使用されていることが注目されています。「403禁止」を解決するための通貨計算機

私の場合、私の主なスクリプトは、私がもう得られない通貨データに基づいていました。私は、パッケージ "quantmod"から "getQuote"という関数を使って、Yahooからデータを取得していました。

株式を探しているすべての人にとって、「quantmod」から「getSymbol.google」機能の情報を引き続き得ることができます。しかし、悲しいことに私のために、機能は通貨のために働かなかった(または、私はそれを把握することができなかった)。

Google Finance Calculator(https://finance.google.com/finance/converter?a=1&from=EUR&to=USD&meta=ei%3DEiYAWtCXJ5fBU7HYsig)からデータを取得するための関数を作成しました。

私は、他のアイデアの欠如としてCcyCalculatorと呼んでいます。私は、どの通貨からどの通貨で通貨レートを指定できるのかを指定できるという機能を作成しました。その後

CcyCalculator <- function(qty=1, convto="USD", QuoteCcy){ 
url <- paste("http://finance.google.com/finance/converter?a=", qty, "&from=", QuoteCcy, "&to=", convto, "&meta=ei%3DNR4AWtmFOojPUfiBisAE", sep = "") ## look for the web calculator and print it 
x <- read.csv(file=url) 
x <- x[364,1] ## look for the value 
x <- gsub("^.*?bld>", "", x) ## clean value 
x <- gsub(" .*$", "", x) ## more cleaning 
x <- print(t(as.matrix(c(QuoteCcy, x)))) ## print the value in a matrix 
} 

、私は多くの通貨値を必要とし、すべてのUSDの変換レートで、私はベクトルのすべての値を取得するためにループを作成しているよう。

入手したい通貨でベクトルを指定する必要があります。

CcyVector <- c("EUR", "GBP", "JPY", "CHF") 

次に、データを入力するために空のデータフレームを作成する必要があります。

CcyF <- data.frame(x= integer(0), y= numeric(0)) 

最後に、私たちのCcyCalculatorでループたちのCcyVectorを

for (i in CcyVector){ 
Ccy <- paste(CcyCalculator(QuoteCcy = i)) ## loop through CcyVector 
CcyF <- as.data.frame(rbind(CcyF, Ccy)) ## ad all the values into our empty data frame 
colnames(CcyF) <- c("CCY", "to_USD") 
} 

そして、あなたが見ることができるように、我々はUSD

CCY to_USD

で通貨の値を持つデータフレームで終わります

EUR 1.1588

GBP 1.3152

円0.0088

CHF 1.0030

私はこれは誰を助けるかどうかを知るドントが、I'veは数日のためにそれを探すためにしようとしてと私はそれのような何かを見つけるcouldn't 。

誰かが私の問題を解決する良い方法を持っている場合、または私の関数をより明確で清潔な方法で書き直すことができれば(つまり、ループはlapply(lcyly(CcyVector、CcyCalculator ))]しかし、私はそれを動作させることができませんでした)

ありがとう!

+0

は、私が使用している有用な技術でありますExcelのスプレッドシートhttps://office-watch.com/2016/excel-stock-prices-from-google-finance/でyahoo株価を置き換えてください。継続的に更新されるGoogleスプレッドシートの使い方は通貨でもうまくいくかもしれませんが、Excelと同じようにRに簡単にインポートできます。 –

+0

私はfinservタイプが常にExcelを実行している可能性が高いと認識していますが、それはしばしば危険に苦しむ不要な依存性です。 – hrbrmstr

答えて

1

私はあなたの疑問を解決する可能性が高いです(それはブログの投稿がSOのものよりも多いからです)。しかし、おそらくこのQ & Aつまずく他の人に有用であり得るの下にあり、いくつかの事柄:

#' Convert currencies using Google Finance calculator 
#' 
#' @note from, to and qty lengths must meet the following conditions 
#' 
#' - from, to and qty must all be length 1, *OR* 
#' - from, to and qty must all be the _same_ length, *OR* 
#' - from can be any length, to can be length 1 and qty can be length 1 or the length of from 
#' 
#' @param from,to character vectors of currencies to convert from and to. 
#' @param qty numeric vector of "from" quantities to convert to "to" 
currency_converter <- function(from, to = "USD", qty = 1) { 

    # require() only has "real" overhead the first time but we need to 
    # ensure these dependencies are loaded and this isn't in a package 

    require(httr, quietly=TRUE, warn.conflicts = FALSE) 
    require(xml2, quietly=TRUE, warn.conflicts = FALSE) 
    require(rvest, quietly=TRUE, warn.conflicts = FALSE) 
    require(purrr, quietly=TRUE, warn.conflicts = FALSE) 
    require(dplyr, quietly=TRUE, warn.conflicts = FALSE) 

    # Local environment variables we'll use liberally in the nested function 

    meta <- valid_froms <- valid_tos <- NULL 

    # Performs a single conversion 

    .currency_converter <- function(.from, .to, .qty) { 

    Sys.sleep(5) # just b/c *you* want something for free doesn't give you the right to abuse resources 

    if (is.na(.qty)) return(NULL) 

    # Prime "meta" & validators with an initial call to the calculator 
    # we'll keep updating "meta" in the calculator call until all 
    # conversions are done 

    if (is.null(meta)) { 

     prime <- GET("https://finance.google.com/finance/converter") 
     stop_for_status(prime) # shld only happen if you abuse the API or lose network 
     prime <- content(prime, as="parsed") 

     meta <<- html_attr(html_node(prime, "input[name='meta']"), "value") 

     # Get valid from/to & store for later use 

     valid_froms <<- html_attr(html_nodes(prime, "select[name='from'] > option"), "value") 
     valid_tos <<- html_attr(html_nodes(prime, "select[name='to'] > option"), "value") 

    } 

    # Make sure the currencies are valid but don't abort if not, just 
    # return a data frame with NA for the converted value 

    if (!(.from %in% valid_froms)) return(data_frame(from = .from, from_qty = .qty, to = .to, to_qty = NA_real_)) 
    if (!(.to %in% valid_tos)) return(data_frame(from = .from, from_qty = .qty, to = .to, to_qty = NA_real_)) 

    # Now make the API call 

    httr::GET(
     url = "https://finance.google.com/finance/converter", 
     query = list(a = .qty, from = .from, to = .to, meta = meta) 
    ) -> res 

    stop_for_status(res) # shld only happen if you abuse the API or lose network 

    res <- content(res, as="parsed") 
    res <- html_text(html_node(res, "div#currency_converter_result > span")) 
    res <- as.numeric(sub(" .*$", "", res)) 

    data_frame(from = .from, from_qty = .qty, to = .to, to_qty = res) 

    } 

    # Ensure valid params 

    if (length(from) > 1) { 

    if (length(to) > 1) { 
     if (length(to) != length(from)) { 
     stop("`from` and `to` must either be the same length or `to` must be a single currency", call.=FALSE) 
     } 
    } else { 
     to <- rep(to, length(from)) 
    } 

    if (length(qty) > 1) { 
     if (length(qty) != length(from)) { 
     stop("`from`, `to` and `qty` must either be the same length or `qty` must be a single currency", call.=FALSE) 
     } 
    } else { 
     qty <- rep(qty, length(from)) 
    } 

    } 

    pmap_dfr(
    data_frame(
     .from = trimws(toupper(from)), 
     .to = trimws(toupper(to)), 
     .qty = as.numeric(qty) 
    ), 
    .currency_converter 
) 

} 

は、それが動作するかどうか見てみましょう。ここでは

currency_converter("SOS") 
## # A tibble: 1 x 4 
## from from_qty to to_qty 
## <chr> <dbl> <chr> <dbl> 
## 1 SOS  1 USD 0.0017 

currency_converter(c("EUR", "GBP", "JPY", "CHF")) 
## # A tibble: 4 x 4 
## from from_qty to to_qty 
## <chr> <dbl> <chr> <dbl> 
## 1 EUR  1 USD 1.1570 
## 2 GBP  1 USD 1.3138 
## 3 JPY  1 USD 0.0087 
## 4 CHF  1 USD 0.9992 

currency_converter(
    c("EUR", "GBP", "JPY", "CHF"), 
    rev(c("EUR", "GBP", "JPY", "CHF")) 
) 
## # A tibble: 4 x 4 
## from from_qty to to_qty 
## <chr> <dbl> <chr> <dbl> 
## 1 EUR  1 CHF 1.1580 
## 2 GBP  1 JPY 150.2182 
## 3 JPY  1 GBP 0.0067 
## 4 CHF  1 EUR 0.8634 

set.seed(8675309) 
currency_converter(c("EUR", "GBP", "JPY", "CHF"), "ZAR", sample(20, 4)) 
## # A tibble: 4 x 4 
## from from_qty to to_qty 
## <chr> <dbl> <chr> <dbl> 
## 1 EUR  4 ZAR 65.6464 
## 2 GBP  10 ZAR 186.5290 
## 3 JPY  14 ZAR 1.7388 
## 4 CHF  18 ZAR 255.2202 

currency_converter(
    c("EUR", "GBP", "JPY", "ZZZ"), 
    rev(c("EUR", "ZZZ", "JPY", "CHF")), 
    sample(20, 4) 
) 
## # A tibble: 4 x 4 
## from from_qty to to_qty 
## <chr> <dbl> <chr> <dbl> 
## 1 EUR  6 CHF 6.948 
## 2 GBP  13 JPY 1952.600 
## 3 JPY  18 ZZZ  NA 
## 4 ZZZ  15 EUR  NA 
+0

ありがとう!それは本当に役に立ちます。私はここで新しいです、私は質問のブログの種類については知らなかった、私は次回にそれを念頭に置いておきます! –

関連する問題