Пример #1
0
        def next_trigger_time(self, now: datetime) -> datetime:
            def _within_time_frame(_time):
                return (_time + RelativeDelta(**self._end_time) >= _time) and \
                       (_time + RelativeDelta(**self._start_time) <= _time)

            _start_time_rule = RegularDateTimeRule(**self._start_time)

            # Before midnight and after end time or after midnight and before start time (e.g. after the market
            # close and before the market open), the next trigger time should always point to the next time
            # triggered by the _start_time_rule (e.g. to the market open time)
            if not _within_time_frame(now):
                _next_trigger_time = _start_time_rule.next_trigger_time(now)
            else:
                _next_trigger_time = self._trigger_time_rule.next_trigger_time(
                    now)
                # If the next trigger time is outside the desired time frame, find the next event time moving
                # current time to the next start_time date.
                if not _within_time_frame(_next_trigger_time):
                    _next_trigger_time = _start_time_rule.next_trigger_time(
                        _next_trigger_time)

            # Check if the next trigger time points to Saturday or Sunday and if so, shift it to Monday
            if _next_trigger_time.weekday() in (5, 6):
                _next_trigger_time_shifted = _next_trigger_time + RelativeDelta(
                    weekday=0)
                assert _next_trigger_time_shifted >= _next_trigger_time
                _next_trigger_time = _next_trigger_time_shifted

            return _next_trigger_time
Пример #2
0
class AfterMarketCloseEvent(RegularTimeEvent):
    """
    Rule which is triggered every day when the market opens (18:00 for NASDAQ and NYSE).

    The listeners for this event should implement the on_after_market_close() method.
    """

    _trigger_time = {"hour": 18, "minute": 0, "second": 0, "microsecond": 0}
    _trigger_time_rule = RegularDateTimeRule(**_trigger_time)

    @classmethod
    def trigger_time(cls) -> RelativeDelta:
        return RelativeDelta(**cls._trigger_time)

    @classmethod
    def next_trigger_time(cls, now: datetime) -> datetime:
        next_trigger_time = cls._trigger_time_rule.next_trigger_time(now)
        return next_trigger_time

    def notify(self, listener) -> None:
        listener.on_after_market_close(self)
Пример #3
0
 def set_trigger_time(cls, trigger_time_dict: Dict[str, int]):
     cls._trigger_time = trigger_time_dict
     cls._trigger_time_rule = RegularDateTimeRule(**trigger_time_dict)
Пример #4
0
 def __init__(self, trigger_time, start_time, end_time):
     self._trigger_time = trigger_time
     self._start_time = start_time
     self._end_time = end_time
     self._trigger_time_rule = RegularDateTimeRule(**trigger_time)