Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get historical currency pair data from a start date to end date? #70

Closed
fightthepower opened this issue Jul 12, 2019 · 5 comments
Assignees

Comments

@fightthepower
Copy link

I want historical data from 2018-05-01 to 2019-05-05 of USD/INR pair. How can I do it with forex-python?

@alexfilothodoros
Copy link

Hi! I don't know if this can be done automatically, but you could write a loop where the day will be set automatically and the currency will be calculated.

@udoedo
Copy link

udoedo commented Oct 1, 2019

Hi, is this issue still open ?

@dallinb
Copy link

dallinb commented Oct 1, 2019

As an attempt off the top of my head (and with a bit of help from Stackoverflow):

from datetime import datetime
from datetime import timedelta, date
from forex_python.converter import CurrencyRates


def daterange(start_date, end_date):
    for n in range(int((end_date - start_date).days)):
        yield start_date + timedelta(n)


c = CurrencyRates()
start_date = date(2018, 5, 1)
end_date = date(2019, 5, 5)

for single_date in daterange(start_date, end_date):
    date_obj = datetime.combine(single_date, datetime.min.time())
    value = c.get_rate('USD', 'INR', date_obj)
    print(single_date.strftime('%Y-%m-%d:'), value)

Please note that this will not be kind on API rates, so either:

  • Reduce the date range.
  • Insert a sleep to reduce the number of calls which will take longer.

@SpectralTacos
Copy link

I was thinking along those lines, and the solution I was thinking to the API calls is after every say 5 or10 iterations (just throwing numbers out there) rest for about 2 seconds:

`from datetime import datetime, date
from time import sleep

def get_rate_history(self, base_cur, dest_cur, date_start, date_end):
lst_results = []

    num_iter_count = 0
    
    for date_iter in range(date_start.toordinal(), date_end.toordinal()):
        date_obj = datetime.combine(date_iter, datetime.min.time())
        value = get_rate(base_cur, dest_cur, date_obj)

        lst_results.append([date_iter, value])
        
        num_iter_count += 1

        if num_iter_count == 5:    #   Rest every 5 iterations
            sleep(2)
            num_iter_count = 0
        #   END if 
    #   END for

    return lst_results`

@Karlheinzniebuhr
Copy link

This would be a great feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants