Skip to content

Commit

Permalink
timezone bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesir committed Dec 12, 2024
1 parent e494270 commit ef50e38
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lumibot/data_sources/tradier_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
import pandas as pd
import pytz

from lumibot import LUMIBOT_DEFAULT_TIMEZONE
from lumibot.entities import Asset, Bars
from lumibot.tools.helpers import create_options_symbol, parse_timestep_qty_and_unit, get_trading_days
from lumiwealth_tradier import Tradier

from .data_source import DataSource


class TradierAPIError(Exception):
pass


class TradierData(DataSource):

MIN_TIMESTEP = "minute"
Expand Down Expand Up @@ -240,12 +239,15 @@ def get_historical_prices(
if "timestamp" in df.columns:
df = df.drop(columns=["timestamp"])

# Check if the index contains dates and handle timezone
if isinstance(df.index[0], date): # Check if index contains date objects
if df.index.tz is None: # Check if the index is timezone-naive
df.index = pd.to_datetime(df.index).tz_localize("America/New_York")
else: # If the index is already timezone-aware
df.index = df.index.tz_convert("America/New_York")
# If the index contains date objects, convert and handle timezone
if isinstance(df.index[0], date): # Check if the index contains date objects
df.index = pd.to_datetime(df.index) # Always ensure it's a DatetimeIndex

# Check if the index is timezone-naive or already timezone-aware
if df.index.tz is None: # Naive index, localize to America/New_York
df.index = df.index.tz_localize(LUMIBOT_DEFAULT_TIMEZONE)
else: # Already timezone-aware, convert to America/New_York
df.index = df.index.tz_convert(LUMIBOT_DEFAULT_TIMEZONE)

# Convert the dataframe to a Bars object
bars = Bars(df, self.SOURCE, asset, raw=df, quote=quote)
Expand Down

0 comments on commit ef50e38

Please sign in to comment.