MicroPyramid / forex-python

Foreign exchange rates, Bitcoin price index and currency conversion using ratesapi.io
http://forex-python.readthedocs.io/en/latest/usage.html
MIT License
664 stars 197 forks source link

SSLV3_ALERT_HANDSHAKE_FAILURE when making requests to theforexapi.com #155

Open KornelPudlo opened 2 months ago

KornelPudlo commented 2 months ago

I am using the forex_python library to convert currency values to USD using historical rates. However, I am encountering an SSL handshake failure when the library makes HTTPS requests to 'theforexapi.com'. The error message is as follows:

Error converting AUD for year 2022: HTTPSConnectionPool(host='theforexapi.com', port=443): Max retries exceeded with url: /api/2022-01-01?base=AUD&symbols=USD&rtype=fpy (Caused by SSLError(SSLError(1, '[SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:997)')))

I have a csv file that contain multiple columns such as: symbol, date, year, open, currency.

Currency columns contain values such as: AUD, EUR, NZD, USD, GBP etc and trying to convert the $$$ from 'open' column to $USD based on a 'date' column.

Can someone try to help me? :)

Code below:

` import pandas as pd from forex_python.converter import CurrencyRates from datetime import datetime

def convert_to_usd(row, c): try:

Constructing a date object with the year from the row and default month and day

    date_obj = datetime(row['year'], 1, 1)
    rate = c.get_rate(row['currency'], 'USD', date_obj)
    return round(row['open'] * rate, 2)
except Exception as e:
    print(f"Error converting {row['currency']} for year {row['year']}: {e}")
    return None

def main():

Load the CSV file

df = pd.read_csv('my_source_file_here_df.csv')

df['currency'] = df['currency'].str.strip().str.upper()

# Initialize currency converter
c = CurrencyRates()

# Convert prices to USD using historical rates
df['open_usd'] = df.apply(lambda row: convert_to_usd(row, c), axis=1)

# Save the updated dataframe to a new CSV file
df.to_csv('my_unpivoted_file_here_with_USD_df.csv', index=False)
print("Conversion completed and saved to 'converted_prices_with_historical_rates.csv'.")

if name == "main": main() `