def session_date(self, dt): """ Given a datetime, returns the UTC-canonicalized date of the exchange session in which the time belongs. If the time is not in an exchange session (while the market is closed), returns the date of the next exchange session after the time. Parameters ---------- dt : Timestamp A timezone-aware Timestamp. Returns ------- Timestamp The date of the exchange session in which dt belongs. """ # Check if the dt is after the market close # If so, advance to the next day if self.is_open_on_day(dt): _, close = self._get_open_and_close(normalize_date(dt)) if dt > close: dt += Timedelta(days=1) while not self.is_open_on_day(dt): dt += Timedelta(days=1) return normalize_date(dt)
def session_date(self, dt): """ Given a datetime, returns the UTC-canonicalized date of the exchange session in which the time belongs. If the time is not in an exchange session (while the market is closed), returns the date of the next exchange session after the time. Parameters ---------- dt : Timestamp A timezone-aware Timestamp. Returns ------- Timestamp The date of the exchange session in which dt belongs. """ # Check if the dt is after the market close # If so, advance to the next day if self.is_open_on_day(dt): _, close = self._get_open_and_close(normalize_date(dt)) if dt > close: dt += Timedelta(days=1) while not self.is_open_on_day(dt): dt += Timedelta(days=1) return normalize_date(dt)
def is_open_on_day(self, dt): """ Is the exchange open (accepting orders) anytime during the calendar day containing @dt. Parameters ---------- dt : Timestamp Returns ------- bool True if exchange is open at any time during the day containing @dt """ dt_normalized = normalize_date(dt) return dt_normalized in self.schedule.index
def is_open_on_day(self, dt): """ Is the exchange open (accepting orders) anytime during the calendar day containing @dt. Parameters ---------- dt : Timestamp Returns ------- bool True if exchange is open at any time during the day containing @dt """ dt_normalized = normalize_date(dt) return dt_normalized in self.schedule.index