beancount / beanprice

Daily price quotes fetching library for plain-text accounting
GNU General Public License v2.0
69 stars 37 forks source link

Implement ECBrates source for exchange rates #82

Open mrlimacz opened 1 year ago

mrlimacz commented 1 year ago

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

gpoul commented 10 months ago

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 commented 10 months ago

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
mrlimacz commented 9 months ago

@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.

gpoul commented 9 months ago

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.

blais commented 4 months ago

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