trading_agent.scheduling.cron_job
¶
One scheduled job of a desk, and when it next runs.
A job either repeats on a five-field cron schedule in India time ("45 15 * * 1-5" is 15:45 on weekdays) or runs once at a given moment. Two options narrow a repeating schedule: trading_days_only skips weekends and exchange holidays, and market_hours_only keeps only the times when NSE equities are open.
There are three kinds of job:
- an agent job gives the desk a message, as if the user had sent it; with a subagent target, a subagent does the task instead and its report is published;
- a command job runs a shell command in the desk's sandbox, with no model;
- the built-in jobs, the heartbeat and the after-close rollover, come from trading_agent.toml rather than from the jobs file.
Typical usage example:
job = CronJob.repeating('Morning check', '0 9 * * 1-5', 'Review overnight news', created_by='user') moment = job.next_run_after(now, calendar, hours)
CronJobError
¶
Bases: ValueError
A job's schedule, time or fields are not valid.
CronJob
dataclass
¶
CronJob(job_id, name, kind, schedule, run_at, text, target=DESK_TARGET, trading_days_only=True, market_hours_only=False, deliver_url=None, created_by='user', created_at='', catch_up=True)
One scheduled job.
Attributes:
| Name | Type | Description |
|---|---|---|
job_id |
str
|
A short identifier, used to delete the job. |
name |
str
|
A name for people. |
kind |
str
|
"agent" or "command". |
schedule |
str | None
|
The cron expression, or None for a one-off job. |
run_at |
str | None
|
The moment of a one-off job, or None for a repeating one. |
text |
str
|
For an agent job, the message for the desk or the subagent's task; for a command job, the command. |
target |
str
|
"desk", or "subagent: |
trading_days_only |
bool
|
Whether to skip weekends and exchange holidays. |
market_hours_only |
bool
|
Whether to run only while NSE equities trade. |
deliver_url |
str | None
|
An address to POST the result to, or None. |
created_by |
str
|
"user", "desk" or "built-in". |
created_at |
str
|
When the job was created, as an ISO 8601 string. |
catch_up |
bool
|
Whether a run missed while the gateway was down is made up once after a restart. |
__post_init__
¶
Checks the fields.
Raises:
| Type | Description |
|---|---|
CronJobError
|
The job has neither or both of a schedule and a time, an invalid schedule or time, an unknown kind, or an invalid target. |
Source code in src/trading_agent/scheduling/cron_job.py
new
classmethod
¶
Creates a job with a fresh identifier and creation time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**fields
|
Any
|
The job's other fields. |
{}
|
Returns:
| Type | Description |
|---|---|
CronJob
|
The job. |
Raises:
| Type | Description |
|---|---|
CronJobError
|
A field is not valid. |
Source code in src/trading_agent/scheduling/cron_job.py
from_json_dictionary
classmethod
¶
Reads a job saved by to_json_dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
The saved fields. |
required |
Returns:
| Type | Description |
|---|---|
CronJob
|
The job. |
Raises:
| Type | Description |
|---|---|
CronJobError
|
A field is not valid. |
TypeError
|
A field is missing or unknown. |
Source code in src/trading_agent/scheduling/cron_job.py
to_json_dictionary
¶
Converts the job to a dictionary that json.dumps can write.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The fields. |
run_moment
¶
The moment of a one-off job, in India time.
Returns:
| Type | Description |
|---|---|
datetime
|
The moment. |
Raises:
| Type | Description |
|---|---|
CronJobError
|
The time is not ISO 8601. |
Source code in src/trading_agent/scheduling/cron_job.py
next_run_after
¶
The first moment after the given one when the job runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
moment
|
datetime
|
The moment to count from, not included. |
required |
calendar
|
MarketCalendar
|
Decides trading days. |
required |
hours
|
ExchangeHours
|
Decides market hours. |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
The next run in India time, or None when a one-off job's time has passed or no allowed time is found. |
Source code in src/trading_agent/scheduling/cron_job.py
previous_run_before
¶
The last moment before the given one when the job should have run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
moment
|
datetime
|
The moment to count back from. |
required |
calendar
|
MarketCalendar
|
Decides trading days. |
required |
hours
|
ExchangeHours
|
Decides market hours. |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
The previous run in India time, or None for a one-off job still to come or when none is found. |
Source code in src/trading_agent/scheduling/cron_job.py
describe
¶
The job in one line, for lists.
Returns:
| Type | Description |
|---|---|
str
|
Such as "Morning check (agent, 0 9 * * 1-5, trading days only)". |