beanprice icon indicating copy to clipboard operation
beanprice copied to clipboard

Implement ECBrates source for exchange rates

Open mrlimacz opened this issue 2 years ago • 5 comments

This source used ECB datasets directly (daily average spot rates). As they only provide EUR as the base currency, other pairs need to be calculated as XXX -> EUR -> YYY

mrlimacz avatar Oct 12 '23 19:10 mrlimacz

Sounds like a good idea; I just tested this on my end and I'm getting empty response from the ECB API you're calling. I added the following check:

    if len(response.text) == 0:
        raise ECBRatesError(f"Empty response text received")

gpoul avatar Jan 07 '24 12:01 gpoul

Reason seems to be that I ran this on a day without data available (Sunday) and it takes the current date by default:

➜  ~ http GET https://data-api.ecb.europa.eu/service/data/EXR/D.USD.EUR.SP00.A startPeriod==2024-01-07 endPeriod==2024-01-07 format==jsondata detail==full --verbose
GET /service/data/EXR/D.USD.EUR.SP00.A?startPeriod=2024-01-07&endPeriod=2024-01-07&format=jsondata&detail=full HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: data-api.ecb.europa.eu
User-Agent: HTTPie/3.2.2



HTTP/1.1 200 
Cache-Control: max-age=30
Connection: keep-alive
Content-Disposition: attachment;filename=data.json
Content-Length: 0
Content-Type: application/vnd.sdmx.data+json;version=1.0.0-wd
Date: Sun, 07 Jan 2024 13:00:17 GMT
Expires: Sun, 07 Jan 2024 12:54:18 GMT
Last-Modified: Fri, 05 Jan 2024 14:57:01 GMT
Server: myracloud
Vary: accept


gpoul avatar Jan 07 '24 13:01 gpoul

@gpoul Actually, such instances are handled. When you run bean-price with an --update flag you want to skip a particular date and proceed to the next one. Your approach would derail the whole thing, ECBRatesError would finish the execution.

mrlimacz avatar Jan 17 '24 18:01 mrlimacz

I can only say that I get this error when running it unmodified from your branch:

(beancount-fava) ➜  financial git:(master) ✗ bean-price -e EUR:ecbrates/EUR-USD --update
ERROR   : Could not fetch for job: DatedPrice(base='EUR-USD', quote='EUR', date=None, sources=[PriceSource(module=<module 'beanprice.sources.ecbrates' from '/Users/gpoul/Projects/financial/beancount-fava/lib/python3.11/site-packages/beanprice/sources/ecbrates.py'>, symbol='EUR-USD', invert=False)])

If I supply a date that returns data, then yes; it works:

(beancount-fava) ➜  financial git:(master) ✗ bean-price -e EUR:ecbrates/EUR-USD -d 2024-01-26
2024-01-26 price EUR-USD                            1.0871 EUR

My expectation was that if there is no most recent price it should return the latest price it can fetch including its date, no? That's at least how I saw other modules behave so far, but maybe I'm misunderstanding how it works.

gpoul avatar Jan 27 '24 14:01 gpoul

Please add a test. In adding test, be mindful of people running them in different timezones. Thank you,

blais avatar Jun 16 '24 21:06 blais