Sabeo Nunes
Sabeo Nunes

Reputation: 1

Getting wrong values for conversion rate EUR to USD and GBP to USD using forex_python package

I have written a python script for converting EUR to USD and GBP to USD based on today's value using python package forex_python, but I am getting incorrect conversion rates for both.

Here is my code -

from forex_python.converter import CurrencyRates
from datetime import datetime

# Get the current date in the format "YYYY-MM-DD"
current_date = datetime.now().strftime("%Y-%m-%d")

# Define the currency pairs
from_currency = 'GBP'
to_currency_usd = 'USD'
to_currency_eur = 'EUR'

# Initialize the CurrencyRates object
c = CurrencyRates()

# Fetch the exchange rates for today
y = c.get_rate(from_currency, to_currency_usd)
z = c.get_rate(to_currency_eur, to_currency_usd)

I am getting EUR to USD as 0.27 whereas today's value is 1.07 I am getting GBP to USD as 1.27 whereas today's value is 1.25

Any solution for it?

Upvotes: 0

Views: 254

Answers (2)

Daviid
Daviid

Reputation: 1586

It's broken?

from forex_python.converter import CurrencyRates
from datetime import datetime

# Get the current date in the format "YYYY-MM-DD"
current_date = datetime.now().strftime("%Y-%m-%d")

# Define the currency pairs
from_currency = 'GBP'
to_currency_usd = 'USD'
to_currency_eur = 'EUR'

# Initialize the CurrencyRates object
cr = CurrencyRates()

# Fetch the exchange rates for today
rates = cr.get_rates('EUR') # same as https://theforexapi.com/api/latest?base=EUR
eur_to_usd = cr.get_rate('EUR', 'USD') # same as https://theforexapi.com/api/latest?base=EUR&symbols=USD (Seems to be broken, shows rates for ILS)

print(rates)
print(eur_to_usd)

https://theforexapi.com/api/latest?base=EUR&symbols=USD returns ILS (Israeli new shekel) as base


{
  "_id": "6467476f7730823eedeb6748",
  "date": "2023-05-18",
  "base": "ILS",
  "rates": {
    "EUR": 0.25413606444890596,
    "USD": 0.274797326488602,
    "JPY": 37.886604488042906,
    "BGN": 0.49703931484917024,
    "CZK": 6.01870441434344,
    "DKK": 1.8927291671961168,
    "GBP": 0.2208188263996544,
    "HUF": 95.01385041551247,
    "PLN": 1.1545401407913798,
    "RON": 1.2632595491626217,
    "SEK": 2.887214414597576,
    "CHF": 0.24740145874100997,
    "ISK": 38.50161376400925,
    "NOK": 2.982794988436809,
    "TRY": 5.437596889374571,
    "AUD": 0.41386058095504336,
    "BRL": 1.3624742687234745,
    "CAD": 0.3702254186891662,
    "CNY": 1.9310020585021221,
    "HKD": 2.15100764949554,
    "IDR": 4088.8917126229385,
    "ILS": 1,
    "INR": 22.692190398739484,
    "KRW": 366.4819944598338,
    "MXN": 4.865689089938754,
    "MYR": 1.2466136369412184,
    "NZD": 0.4402399044448398,
    "PHP": 15.330503951815801,
    "SGD": 0.3696154921344888,
    "THB": 9.432514168085593,
    "ZAR": 5.329055376248443
  }
}

Wrong date too, maybe a hacker manipulating the market!! Puts on tinfoil hat

Upvotes: 0

You need to use a life Api tracker for exchange rate to get a right data, you may use Open exchange rates they have a documentation to how to use their api you can find them here Open exchange rate API

and then you can modify your code to look like this

from forex_python.converter import CurrencyRates
from datetime import datetime

current_date = datetime.now().strftime("%Y-%m-%d")

from_currency = 'GBP'
to_currency_usd = 'USD'
to_currency_eur = 'EUR'

c = CurrencyRates(api_key='YOUR_API_KEY')  # Replace 'YOUR_API_KEY' with 
your actual API key

y = c.get_rate(from_currency, to_currency_usd)
z = c.get_rate(to_currency_eur, to_currency_usd)

print(f'{from_currency} to {to_currency_usd}: {y}')
print(f'{to_currency_eur} to {to_currency_usd}: {z}')

Upvotes: 0

Related Questions