0

I am trying to retreiving historical daily data from an API that requires the start date and current date, in Epoch Unix timestamp format. The API requires this:

data = client.get_historical_data('A-B', '1day', startDate, endDate)

The endDate has to be now (the moment I run the script) and the startDate has to be x-months before the endDate.

I am definitely not a pro in Python, the script works but I need help to understand how to declare the endDate and startDate variables correctly using Epoch Unix timestamp, so that I can retrieve the data of the specific period I need.

Many thanks in advance!

Il_Marti
  • 19
  • 3

1 Answers1

1

Use python datetime https://docs.python.org/3/library/datetime.html

from datetime import datetime
from dateutil.relativedelta import relativedelta

end = datetime.today()
start = end - relativedelta(months=2)

startDate = int(start.timestamp())
endDate = int(end.timestamp())

Look at the datetime and timedelta objects, are very usefull.

I replaced the use of timedelta by relativedelta as MrFuppes suggested

Gonzalo Odiard
  • 1,238
  • 12
  • 19