Python Forum
currency converter using forex-python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
currency converter using forex-python
#4
Also if do some coding can extend exchange_rates to do your task.
from exchange_rates import get_exchange_rates

def conversion_rate(base_currency, target_currency, on_date=None):
    rates = get_exchange_rates(base_currency, target_currencies=[target_currency], on_date=on_date)
    return rates[target_currency]

def convert_currency(base_currency, target_currency, amount, on_date=None):
    rate = conversion_rate(base_currency, target_currency, on_date=on_date)
    return amount * rate

if __name__ == '__main__':
    base_currency = 'USD'
    target_currency = 'CAD'
    amount = 123
    date = '2024-03-07'
    converted_amount = convert_currency(base_currency, target_currency, amount, on_date=date)
    print(f"123 USD is equivalent to {converted_amount:.3f} {target_currency} on {date}")
Output:
123 USD is equivalent to 166.013 CAD on 2024-03-07
Reply


Messages In This Thread
RE: currency converter using forex-python - by snippsat - Mar-07-2024, 08:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,863 Jun-29-2023, 11:57 AM
Last Post: gologica
  Help with basic weight converter PythonSquizzel 3 1,618 Jun-29-2022, 02:42 PM
Last Post: deanhystad
  xml extract currency 3lnyn0 5 1,884 Apr-30-2022, 10:29 AM
Last Post: snippsat
  calculate daily return in percent in forex as to some strategy? alen 1 2,311 Mar-12-2021, 10:03 AM
Last Post: buran
  Converter Souls99 2 2,522 Aug-01-2020, 01:27 AM
Last Post: Souls99
  Help me get this image converter code working? NeTgHoSt 0 2,218 Jul-14-2020, 10:36 PM
Last Post: NeTgHoSt
  Currency converter Scott 5 3,258 Jun-14-2020, 11:59 PM
Last Post: Scott
  How to use 2to3.py converter under Windows OS samsonite 2 7,496 Mar-02-2019, 05:49 AM
Last Post: samsonite
  Currency formatting the third element in a 2D list RedSkeleton007 3 3,380 Mar-09-2018, 12:53 PM
Last Post: j.crater
  Can't get a downloaded file converter to work Godotisnothere 1 3,287 Jan-24-2017, 06:01 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020