[GNC-dev] Robust error handling and respecting the Apha Vantage API limits in gnc-fq-helper.

Mike Alexander mta at umich.edu
Wed Feb 12 01:43:57 EST 2020


On 11 Feb 2020, at 3:59, Edward d'Auvergne wrote:

> F::Q uses this URL for all queries:
>
> https://www.alphavantage.co/query?function=GLOBAL_QUOTE&datatype=json&symbol=IDRUSD&apikey=$MY_KEY
>
> The result I see is:
>
> {
>     "Global Quote": {
>         "01. symbol": "IDRUSD",
>         "02. open": "0.0001",
>         "03. high": "0.0001",
>         "04. low": "0.0001",
>         "05. price": "0.0001",
>         "06. volume": "0",
>         "07. latest trading day": "2020-02-10",
>         "08. previous close": "0.0001",
>         "09. change": "0.0000",
>         "10. change percent": "0.0000%"
>     }
> }

It doesn't use that URL for currency exchange rates.  It uses

https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=IDR&to_currency=USD&apikey=xxx

which returns

{
     "Realtime Currency Exchange Rate": {
         "1. From_Currency Code": "IDR",
         "2. From_Currency Name": "Indonesian Rupiah",
         "3. To_Currency Code": "USD",
         "4. To_Currency Name": "United States Dollar",
         "5. Exchange Rate": "0.00007297",
         "6. Last Refreshed": "2020-02-12 06:30:18",
         "7. Time Zone": "UTC",
         "8. Bid Price": "-",
         "9. Ask Price": "-"
     }
}

> It looks like this code then kicks in, from the "currency" subroutine
> in lib/Finance/Quote.pm:
>
> """
> if ( $exchange_rate < 0.001 ) {
>     # exchange_rate is too little. we'll get more accuracy by using
>     # the inverse rate and inverse it
>     my $inverse_rate = $this->currency( $to, $from );
>     {
>         local $^W = 0;
>         return undef unless ( $exchange_rate + 0 );
>     }
>     $exchange_rate = int( 100000000 / $inverse_rate + .5 ) / 
> 100000000;
> }
> """
>

That code should be removed.  It actually gets the same accuracy and it 
gets a different answer.  The reverse quote is 13683.5.  This inverts to 
a rate of 0.00007308 which is not the same as 0.00007297.  That's a 
trivial difference, but F::Q should really return the quote it was asked 
for if possible.

I noticed that there was some action on my pull request after you 
rattled their cage.  Thanks for doing that.  That may make this 
discussion largely irrelevant.

    Mike


More information about the gnucash-devel mailing list