diff --git a/src/investiny/historical.py b/src/investiny/historical.py index cb2a355..863d274 100644 --- a/src/investiny/historical.py +++ b/src/investiny/historical.py @@ -1,13 +1,10 @@ # Copyright 2022 Alvaro Bartolome, alvarobartt @ GitHub # See LICENSE for details. -from datetime import datetime +from datetime import datetime, timedelta from typing import Any, Dict, Literal, Union -import pytz - from investiny.config import Config -from investiny.info import investing_info from investiny.utils import calculate_date_intervals, request_to_investing @@ -50,9 +47,6 @@ def historical_data( Config.time_format if interval not in ["D", "W", "M"] else Config.date_format ) - info = investing_info(investing_id=investing_id) - tz = pytz.timezone(info["timezone"]) - for to_datetime, from_datetime in zip(to_datetimes, from_datetimes): params = { "symbol": investing_id, @@ -61,8 +55,12 @@ def historical_data( "resolution": interval, } data = request_to_investing(endpoint="history", params=params) + # Dates are shifted due to an Investing.com issue with returned timestamps + days_shift = (datetime.fromtimestamp(data["t"][0]) - from_datetime).days # type: ignore result["date"] += [ - datetime.fromtimestamp(t, tz=tz).strftime(datetime_format) + (datetime.fromtimestamp(t) - timedelta(days=days_shift)).strftime( + datetime_format + ) for t in data["t"] # type: ignore ] result["open"] += data["o"] # type: ignore diff --git a/src/investiny/utils.py b/src/investiny/utils.py index 77960ec..a5183f1 100644 --- a/src/investiny/utils.py +++ b/src/investiny/utils.py @@ -82,7 +82,7 @@ def calculate_date_intervals( } if not from_date: - to_datetimes = [datetime.now(tz=timezone.utc)] + to_datetimes = [datetime.now() + timedelta(days=1)] from_datetimes = [to_datetimes[0] - interval2timedelta[interval]] return (from_datetimes, to_datetimes) @@ -91,7 +91,7 @@ def calculate_date_intervals( to_datetimes = [ datetime.strptime(to_date, Config.date_format) + timedelta(days=1) if to_date - else datetime.now() + else datetime.now() + timedelta(days=1) ] limit_datetime = datetime(1970, 1, 1) except ValueError: