def __init__(self, input_c, output_c, amount): # check if null self.input_c = self.get_always_currency_code(input_c) self.output_c = self.get_always_currency_code(output_c) amount = float(amount) self.amount = amount self.c = converter.CurrencyRates()
def refresh(): print("GATHERING DATA") df = pd.read_csv('FX.csv', parse_dates=['Date'], dtype=float) now = pd.Timestamp(dt.date.today()) date = now while True: if date in tuple(df['Date']): start = date break date = date - dt.timedelta(1) c = fx.CurrencyRates() currencies = ('EUR', 'GBP', 'JPY', 'CNY', 'INR', 'RUB', 'CHF', 'CAD', 'MXN') length = len(df) for x in range((now - start).days): try: rates = c.get_rates('USD', date + dt.timedelta(x + 1)) df.loc[length + x, 'Date'] = date + dt.timedelta(x + 1) df.loc[length + x, '(USD)'] = 1 for i in range(len(currencies)): df.loc[length + x, '({})'.format(currencies[i])] = rates[currencies[i]] print(date + dt.timedelta(x + 1)) except fx.RatesNotAvailableError: pass df.to_csv('FX.csv', index=False, float_format='%g')
def fetch_rates(self, base_currency: str = settings.BASE_CURRENCY, currency: str = None, date_obj: date = date.today(), to_obj: date = None) -> []: """ Get conversion rates between currency and base currency for a range of dates :param base_currency: currency to convert to :param currency: currency to convert from :param date_obj: beginning of range :param to_obj: end of range :return: List of conversion rates """ c = converter.CurrencyRates() rates = [] _rates = [] if currency: for i in range(((to_obj or date_obj) - date_obj).days + 1): _rates = self._fetch_single_rate(c, base_currency=base_currency, currency=currency, date_obj=date_obj + timedelta(i)) rates.extend(_rates) else: for i in range(((to_obj or date_obj) - date_obj).days + 1): _rates = self._fetch_all_rates(c, base_currency=base_currency, date_obj=date_obj + timedelta(i)) rates.extend(_rates) return rates
def default_converter(base): """ Return default converter which uses forex_python (frontend to fixer.io rates) """ try: return Converter(base, converter.CurrencyRates().get_rates(base)) except converter.RatesNotAvailableError: raise UnknownCurrencyCode(base)
def main(output, target_date): rates = converter.CurrencyRates() now = datetime.datetime.now() months = [(y, m) for y in YEARS for m in MONTHS if not (y >= now.year and m > now.month)] rows = [] for y, m in months: # calculate how much more dollars are worth today inflation = calculate_inflation(y, m, target_date) for c in Currency.ALL: # calculate how many dollars, 1 currency unit is worth dollar_equivalent = convert_to_dollars(rates, y, m, c) rows.append( ExchangeRateSnapshot(y, m, c, dollar_equivalent, inflation)._asdict()) with open(output, "w") as f: json.dump(rows, f)
def fetch_rates(self, base_currency: str = settings.BASE_CURRENCY, currency: str = None, date_obj: date = date.today(), to_obj: date = None) -> []: c = converter.CurrencyRates() rates = [] _rates = [] if currency: for i in range(((to_obj or date_obj) - date_obj).days + 1): _rates = self._fetch_single_rate(c, base_currency=base_currency, currency=currency, date_obj=date_obj + timedelta(i)) rates.extend(_rates) else: for i in range(((to_obj or date_obj) - date_obj).days + 1): _rates = self._fetch_all_rates(c, base_currency=base_currency, date_obj=date_obj + timedelta(i)) rates.extend(_rates) return rates
def __init__(self, bot: KBot) -> None: self.bot = bot self.ureg = UnitRegistry() self.ureg.default_format = "~P.3f" self.quantity = self.ureg.Quantity self.rates = forex_api.CurrencyRates()
import forex_python.converter as fxp import datetime import pandas as pd base_date = datetime.datetime.today() base_date = datetime.datetime(base_date.year, base_date.month, base_date.day) date_list = [base_date - datetime.timedelta(days=x) for x in range(366)] total = len(date_list) c = fxp.CurrencyRates() print('MXN to AUD rate: {}'.format(c.get_rate('AUD', 'MXN'))) currency_list = [] x = 0 for d in date_list: x += 1 print('processing {} out of {}'.format(x, total)) try: rates_list = c.get_rate('MXN', d) for r in rates_list: row = {} row['date'] = d row['currency'] = r row['rates'] = rates_list[r] currency_list.append(row) except Exception as e: print('Error: {} for day: {}'.format(e, d)) pd.DataFrame(currency_list).to_csv('mxn.csv', index=False)
import csv import sqlite3 import re from datetime import date from forex_python import converter from gdrived import download_file_from_google_drive _CURRENCY_FORMATTER = converter.CurrencyRates() get_rate = _CURRENCY_FORMATTER.get_rate FFPath = '/mnt/d/Projects/SourceCode/FlatFiles/' DBSave = '/mnt/d/Projects/SourceCode/Python/FinancialPy/DB/' CreateSchema = ''' create table if not exists Ledger( LID CHAR[5] PRIMARY KEY NOT NULL UNIQUE, Amount FLOAT NOT NULL, Note TEXT, SubCategoryID INTEGER, SpendID INTEGER, IncomeID INTEGER, DateID INTEGER, CurrencyID INTEGER, FOREIGN KEY (SubCategoryID) REFERENCES Subcategory(SubID), FOREIGN KEY (SpendID) REFERENCES Accounts(AccID), FOREIGN KEY (IncomeID) REFERENCES Accounts(AccID), FOREIGN KEY (DateID) REFERENCES Dates(DID), FOREIGN KEY (CurrencyID) REFERENCES Currencies(CurrId) ); create table if not exists Category(