Sessions, Timezones, and Bar Building
Algolang provides sophisticated handling of trading sessions, timezones, and bar construction – features that are notoriously difficult to manage on other platforms.
Trading sessions
A session defines the trading hours and days for a data series.
Session hours
Specify trading hours with the --session-hours flag:
algo run --strategy kbt --symbols @ES --session-hours "9:30am-4:00pm"Hours can be specified in 12-hour (9:30am-4:00pm) or 24-hour (09:30-16:00) format. Sessions that cross midnight use times where the end time is earlier than the start time (e.g., 6:00pm-5:00pm for nearly-24-hour futures sessions).
Session days
Specify trading days with the --session-days flag:
algo run --strategy kbt --symbols @ES --session-days Mon-FriDays can be full names (Monday-Friday), abbreviations (Mon-Fri), or numeric (1-5).
Timezones
Algolang uses IANA timezone names (e.g., America/New_York, America/Chicago, Europe/London, Asia/Tokyo). By default, each symbol uses the timezone of its exchange. You can override this:
algo run --strategy kbt --symbols @ES --timezone "America/New_York"Automatic DST handling
Algolang automatically handles daylight savings transitions. When a session spans a DST boundary, the engine adjusts so that each bar represents the correct wall-clock time at the exchange. This is critical for strategies that trade across timezones – for example, a strategy trading Australian Dollar futures on the CME with session hours defined in Sydney time. The time difference between Chicago and Sydney fluctuates by up to two hours during DST transitions. Algolang handles this transparently.
Note on the “fall back” transition: When clocks move backward (e.g., from 3:00am back to 2:00am), the wall-clock hour between 2:00am and 3:00am occurs twice. In this case, Algolang will produce two sets of bars for that hour. There is currently no special handling to disambiguate the repeated interval. In practice this is unlikely to affect real trading sessions, but it is worth being aware of as a known limitation.
Bar building modes
| Mode | Description |
|---|---|
natural | Bars are built using the natural trading hours of the instrument. This is the default. |
session | Bars are built strictly within the defined session hours. Bars do not span session boundaries. |
Specify the mode with --bar-building-mode:
algo run --strategy kbt --symbols @ES --bar-interval 60m \
--bar-building-mode session --session-hours "9:30am-4:00pm"Out-of-session order execution
When a strategy uses defined sessions (e.g., 9:30am-4:00pm for equities), the market often continues trading outside those hours (e.g., futures trade nearly 24 hours). A stop placed at 4:00pm would normally not be evaluated until 9:30am the next day.
Algolang addresses this by loading “shadow bars” – raw 1-minute bars covering all trading hours outside the session. After each session close, these shadow bars are processed through the order execution system to check whether outstanding orders would be triggered.
Key points:
- Shadow bar processing is enabled by default
- All outstanding orders (including entries) are evaluated on shadow bars by default (
EnterNow). Strategy code is never called on shadow bars – only order evaluation runs. - Entry orders can opt out of shadow bar evaluation by passing
EnterNextSession, which holds the order until the next session opens - Fills on shadow bars are annotated with
*in the trade list - Disable with
--no-out-of-session-execution
Time-related RunState functions
| Function | Returns |
|---|---|
Time(dataN) | Ending time of the current bar |
TimeAtExchange(dataN) | Time in the exchange timezone |
Epoch(dataN) | Unix epoch timestamp |
Weekday(dataN, loc) | Day of the week |
IsFirstBarOfSession(dataN) | True on the first bar of the session |
IsLastBarOfSession(dataN) | True on the last bar of the session |
IsFirstBarOfDay(dataN, loc) | True on the first bar of the calendar day |
IsLastBarOfDay(dataN, loc) | True on the last bar of the calendar day |
IsFirstBarOfWeek(dataN, loc) | True on the first bar of the week |
IsLastBarOfWeek(dataN, loc) | True on the last bar of the week |
IsLastBarOfMonth(dataN, loc) | True on the last bar of the month |
SessionBarNumber(dataN) | Bar number within the current session |
MinutesSinceSessionStart() | Minutes elapsed since session start |