wilsonfreitas / rbcb

R interface to Brazilian Central Bank web services
https://wilsonfreitas.github.io/rbcb/
Other
91 stars 22 forks source link

Duplicated currencies in PTAX form #19

Closed sauloguerra closed 6 years ago

sauloguerra commented 6 years ago

Hello,

First of all congratulations for the package, it's excellent!

I was using the package to retrieve time series for all currencies in a loop, and I noticed that there is a problem when it comes to querying certain currencies.

The function uses the PTAX forms and the select reference (ChkMoeda) to perform the query. The problem: there are, for some 3 letter ISO codes of the currency, more than one reference in the form list for the same currency.

Exemplifying, the following query does not quote for the period, even if there is some data in the PTAX:

mxn <- get_currency ("MXN", "2018-05-21", "2018-05-22")

Error in get_currency("MXN", "2018-05-21", "2018-05-22") : 
  BCB API returned error: • Não existe informação para a pesquisa efetuada!•

There are two references to PESO MEXICO in the form (I imagine an old one - no / in the text - that does not work and a recent one - with / in the text - that works), and I believe that when the parser is being made to translate 'MXN' for ChkMoeda from Mexico is catching the very first entry of PESO MEXICO (no /) that does not work. If you try the PTAX form with PESO/MEXICO, it works.

It seems to me that some adjustment in the following function would solve the question:

  id_list <- suppressMessages(currency_id_list())
  all_currencies <- suppressMessages(get_currency_list())
  x <- merge(id_list, all_currencies)
  x[x$symbol == symbol,]$id[1] # <<< aqui pode ter mais de uma ocorrência. Das que mapeei que tem duas, a primeira não funciona e a segunda sim
}

I did some workarounds but it's not cool to propose as a solution. Once I get a better time window I can make a proposal for improvement!

Thanks again for the package!

wilsonfreitas commented 6 years ago

Hi @sauloguerra

Thanks for reporting this problem and file this issue. As you've mentioned rbcb doesn't use an API, it fetch the data from a FORM and this form has its own issues. BCB has released a brand new API to access the currency data. I am working on it and, I hope, soon a new rbcb version will be released using this API.

wilsonfreitas commented 6 years ago

@sauloguerra I implemented a workaround and I guess that will work with the major currencies.

I was studying the API and I have seen that it won't solve this particular issue.

I am closing a new release by the end of this week so I guess this fix will be available on CRAN in the next week.

thanks.