-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from tarioch/feature/alphavantage_fx_rates
Also support fetching fx rates (including crypto) from alphavantage
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from beancount.prices import source | ||
from dateutil import tz | ||
from dateutil.parser import parse | ||
from beancount.core.number import D | ||
import requests | ||
from os import environ | ||
from time import sleep | ||
|
||
|
||
class Source(source.Source): | ||
def get_latest_price(self, ticker): | ||
params = { | ||
'function': 'CURRENCY_EXCHANGE_RATE', | ||
'from_currency': ticker, | ||
'to_currency': 'CHF', | ||
'apikey': environ['ALPHAVANTAGE_API_KEY'], | ||
} | ||
|
||
resp = requests.get(url='https://www.alphavantage.co/query', params=params) | ||
data = resp.json() | ||
if 'Note' in data: | ||
sleep(60) | ||
resp = requests.get(url='https://www.alphavantage.co/query', params=params) | ||
data = resp.json() | ||
|
||
priceData = data['Realtime Currency Exchange Rate'] | ||
|
||
price = D(priceData['5. Exchange Rate']) | ||
date = parse(priceData['6. Last Refreshed']) | ||
|
||
us_timezone = tz.gettz("Europe/Zurich") | ||
time = date.astimezone(us_timezone) | ||
return source.SourcePrice(price, time, 'CHF') | ||
|
||
def get_historical_price(self, ticker, time): | ||
return None |