backtrader.timer module

Timer Module - Time-based event scheduling.

This module provides the Timer class for scheduling time-based notifications during backtesting. Timers can trigger at specific times, session start/end, or at repeating intervals.

Constants:

SESSION_TIME: Timer triggers at a specific time. SESSION_START: Timer triggers at session start. SESSION_END: Timer triggers at session end.

示例

Creating a timer that triggers at session start: >>> timer = bt.Timer(when=bt.Timer.SESSION_START, weekdays=[0, 1, 2, 3, 4]) >>> cerebro.add_timer(timer)

class backtrader.timer.Timer[源代码]

基类:ParameterizedBase

Timer class for scheduling time-based notifications in backtrader.

Timers can trigger at specific times of day, session boundaries, or at repeating intervals. They can filter by weekdays and monthdays.

Params:

tid: Timer ID for identification. owner: Owner object of the timer. strats: Whether to notify strategies (default: False). when: When to trigger (time, SESSION_START, or SESSION_END). offset: Time offset for the trigger. repeat: Repeat interval for recurring timers. weekdays: List of weekdays when timer is active (0=Monday, 6=Sunday). weekcarry: Whether to carry over to next weekday if missed. monthdays: List of month days when timer is active. monthcarry: Whether to carry over to next month day if missed. allow: Callback function to allow/disallow timer on specific dates. tzdata: Timezone data for the timer. cheat: Whether timer can execute before broker.

示例

>>> timer = bt.Timer(when=datetime.time(9, 30), weekdays=[0, 1, 2, 3, 4])
>>> cerebro.add_timer(timer)
tid

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

owner

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

strats

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

when

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

offset

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

repeat

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

weekdays

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

weekcarry

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

monthdays

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

monthcarry

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

allow

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

tzdata

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

cheat

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

SESSION_TIME = 0
SESSION_START = 1
SESSION_END = 2
__init__(*args, **kwargs)[源代码]

Initialize the Timer instance.

参数:
  • *args -- Positional arguments.

  • **kwargs -- Keyword arguments for timer parameters.

start(data)[源代码]

Initialize the timer with the associated data source.

参数:

data -- Data source for time reference and session information.

check(dt)[源代码]

Check if the timer should trigger at the given time.

参数:

dt -- Numeric datetime to check.

返回:

True if timer should trigger, False otherwise.

返回类型:

bool