indie.schedule.ScheduleRule, indie.schedule.Schedule and indie.TradingSession classes.
ScheduleRule
The ScheduleRule class defines the specific time window and list of weekdays during which it applies.
To create a ScheduleRule you need to specify:
- start time of the rule
- end time of the rule
- list of weekdays: we have constants
indie.schedule.WORKDAYS,indie.schedule.WEEKENDandindie.schedule.ALL_DAYS. You can also create a custom list of weekdays as a regular list of enums, e.g.[indie.schedule.week_day.SATURDAY, indie.schedule.week_day.SUNDAY](which is exactly the same asindie.schedule.WEEKENDby the way).
Types of ScheduleRule
Depending on the start and end time values, there are three types of ScheduleRules.
Simple basic rule
Simple basic rule is aScheduleRule object with start < end. If end is a midnight, then end = time(0, 0, 0, 0) This type of rule applies from start time to end time on specified weekdays.
Example:
Simple 24h rule
Simple 24-hours rule is aScheduleRule object with start == end == time(hour=0, minute=0, second=0, microsecond=0). This type of rule applies 24 hours on specified weekdays.
Example:
Overnight rule
Overnight rule is aScheduleRule object with start > end or start == end != time(hour=0, minute=0, second=0, microsecond=0). This type of rule applies from start time of the day before specified to end time of specified day.
Example:
time(0, 0, 0, 0), the second rule will have the start time equal to time(0, 0, 0, 0) and the end time will be the same as the original rule.
Schedule
Each Schedule instance defines specific time windows and exceptions to a regular schedule. To create your own schedule, you need to define:
rules- a list of ScheduleRulesexcept_once- a list of specific dates for one-time exceptions (e.g., special events or emergencies) that deviate from the usual scheduleexcept_yearly- a list of dates representing annual exceptions (e.g., holidays)timezone- the time zone in which the schedule is defined. The time zone should be indicated according to the IANA library standards (e.g., “America/New_York” or “Europe/London”).
Schedule objects within the Context.calc method, as this would instantiate a new object on each bar, resulting in a high number of short-lived objects. Therefore, it is recommended to declare them in the __init__ function in the class definition, which will create them once during the indicator’s initialization.
Example:
self.info.timezone as the timezone in a Schedule(), you need to define a pre_calc function in your class and initialize the field inside this function. This is necessary because in the __init__ method, self.info and self.trading_session data are not yet available.
Example:
Schedule class provides several key functionalities for working with schedules. One of the primary actions is checking whether the schedule is empty using the is_empty method. Additionally, the in operator allows you to verify if a given timestamp falls within the time periods defined by the schedule. Another useful method is is_same_period, which checks whether two timestamps belong to the same schedule period or fall on different days.
TradingSession
The TradingSession class represents the different phases of market trading. These phases correspond to specific trading windows with unique rules that vary depending on the market. It is not allowed to create objects of the TradingSession class in Indie code; an already created instance of the class is available in self.trading_session.
There are three fields in TradingSession class:
pre_marketregularafter_hours
Schedule class. The pre_market and after_hours schedules can be empty, which can be verified using the is_empty method.
The main operations with TradingSession objects include using the in operator to check if a specific timestamp falls within any of the trading session periods, and the is_same_period method to determine whether two timestamps belong to the same trading period.
The Context class provides four methods to determine whether the current bar is the first or last bar of a trading session:
is_first_in_session()is_first_in_regular_session()is_last_in_session()is_last_in_regular_session()
pre_market is empty, the is_first_in_session and is_first_in_regular_session methods will return the same value. Similarly, if after_hours is empty, is_last_in_session and is_last_in_regular_session will return the same value. If the first or last bar of the session does not exist or is unavailable, these methods will not return True.