BackTester
Reference information for the BackTester
class.
anterior.warp.backtester.BackTester
now
Returns the timezone-aware datetime of the backtester.
RETURNS | DESCRIPTION |
---|---|
datetime
|
The timezone-aware datetime of the backtester. |
Notes
- The datetime is timezone-aware and is in the timezone specified by the backtester's
timezone
attribute. - The datetime is the simulated time in the backtest if the backtester is being backtested.
- The datetime is the current system time if the backtester is running in live mode.
on_start
Gets called when the backtester starts.
Tip
- Override this method to add custom behavior when the backtester starts.
on_stop
Gets called when the backtester stops.
Tip
- Override this method to add custom behavior when the backtester stops.
do
Schedules a function to run immediately during the backtester's run.
PARAMETER | DESCRIPTION |
---|---|
function |
The function to schedule.
TYPE:
|
name |
The name of the function to schedule. Defaults to the function"s name.
TYPE:
|
log |
Whether to log the function to the backtester's logs. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Job
|
The job object running the specified function. |
See Also
Schedule.do
Defines the function to run on the schedule object.
Examples:
after
Creates a Schedule object that will trigger after the specified time interval.
PARAMETER | DESCRIPTION |
---|---|
delta |
The time interval to trigger after. Mutually exclusive with days, hours, minutes, and seconds.
TYPE:
|
days |
The number of days to trigger after. Mutually exclusive with delta.
TYPE:
|
hours |
The number of hours to trigger after. Mutually exclusive with delta.
TYPE:
|
minutes |
The number of minutes to trigger after. Mutually exclusive with delta.
TYPE:
|
seconds |
The number of seconds to trigger after. Mutually exclusive with delta.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Schedule
|
Schedule object that will trigger after the specified time interval. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If both a timedelta and individual time intervals are provided. |
See Also
do_after
Decorator to schedule a function to run after a time interval.
Info
The time interval can be specified either as a timedelta or as individual time intervals. For example, the following two calls are equivalent:
Examples:
between
between(years=None, months=None, days=None, weeks=None, days_of_week=None, hours=None, minutes=None, seconds=None, dates=None)
Creates a Schedule object that runs a function between a specified time range.
PARAMETER | DESCRIPTION |
---|---|
years |
The years to trigger between. Start and end values must represent integers.
TYPE:
|
months |
The months to trigger between. Start and end values must represent integers between 1 and 12.
TYPE:
|
days |
The days to trigger between. Start and end values must represent integers between 1 and 31.
TYPE:
|
weeks |
The weeks to trigger between. Start and end values must represent integers between 1 and 53.
TYPE:
|
days_of_week |
The days of the week to trigger between. Start and end values must represent integers between 0 and 6, or one of the following strings: "mon", "tue", "wed", "thu", "fri", "sat", "sun".
TYPE:
|
hours |
The hours to trigger between. Start and end values must represent integers between 0 and 23.
TYPE:
|
minutes |
The minutes to trigger between. Start and end values must represent integers between 0 and 59.
TYPE:
|
seconds |
The seconds to trigger between. Start and end values must represent integers between 0 and 59.
TYPE:
|
dates |
The dates to trigger between. Start and end values must be datetime objects.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Schedule
|
Schedule object that will trigger between the specified time intervals. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the time intervals are not in the correct format. |
See Also
do_between
Decorator to schedule a function to run between a specified time range.
Examples:
Running a function between a time range with tuples
Running a function between a time range with strings
cron
Creates a Schedule object that runs a function on a cron schedule.
PARAMETER | DESCRIPTION |
---|---|
expression |
The cron expression to trigger on.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Schedule
|
Schedule object that will trigger according to the specified cron expression. |
See Also
do_cron
Decorator to schedule a function to run on a cron schedule.
Info
Check out crontab.guru for more information on cron expressions.
Examples:
on
on(dt=None, year=None, month=None, day=None, weekday=None, day_of_week=None, hour=None, minute=None, second=None)
Creates a Schedule object that will trigger on a specific date or datetime.
PARAMETER | DESCRIPTION |
---|---|
dt |
The datetime to trigger on. Mutually exclusive with year, month, day, weekday, day_of_week, hour, minute, and second. Must be a string in the format "YYYY-MM-DD HH:MM:SS", a datetime object, or a date object.
TYPE:
|
year |
The year to trigger on. Mutually exclusive with dt.
TYPE:
|
month |
The month to trigger on. Mutually exclusive with dt.
TYPE:
|
day |
The day to trigger on. Mutually exclusive with dt.
TYPE:
|
weekday |
The weekday to trigger on. Mutually exclusive with dt.
TYPE:
|
day_of_week |
The day of the week to trigger on. Mutually exclusive with dt.
TYPE:
|
hour |
The hour to trigger on. Mutually exclusive with dt.
TYPE:
|
minute |
The minute to trigger on. Mutually exclusive with dt.
TYPE:
|
second |
The second to trigger on. Mutually exclusive with dt.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Schedule
|
Schedule object that will trigger on the specified date and time. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If both a dt and individual date components are provided. |
See Also
do_on
Decorator to schedule a function to run on a specific date or datetime.
every
Creates a Schedule object that will run every x years/months/days/weeks/hours/minutes/seconds.
PARAMETER | DESCRIPTION |
---|---|
years |
The number of years to trigger every. Defaults to None.
TYPE:
|
months |
The number of months to trigger every. Defaults to None.
TYPE:
|
days |
The number of days to trigger every. Defaults to None.
TYPE:
|
weeks |
The number of weeks to trigger every. Defaults to None.
TYPE:
|
hours |
The number of hours to trigger every. Defaults to None.
TYPE:
|
minutes |
The number of minutes to trigger every. Defaults to None.
TYPE:
|
seconds |
The number of seconds to trigger every. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Schedule
|
Schedule object that will trigger every x years/months/days/weeks/hours/minutes/seconds. |
See Also
do_every
Decorator to schedule a function to run every x years/months/days/weeks/hours/minutes/seconds.
Examples:
when
Creates a Schedule object that will trigger when the specified condition function returns True.
PARAMETER | DESCRIPTION |
---|---|
condition |
The condition function to trigger on. Must return a boolean value when called.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Schedule
|
Schedule object that will trigger when the specified condition function returns True. |
Info
-
By default,
once
checks the condition function every second. To override this, combine thewhen
schedule. with anevery
schedule:
Examples:
Running a function when a condition is met
once
Creates a Schedule object that will trigger only once when the specified condition function returns True.
PARAMETER | DESCRIPTION |
---|---|
condition_function |
The condition function to trigger on. Must return a boolean value when called.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Schedule
|
Schedule object that will trigger only once when the specified condition function returns True. |
Info
-
The difference between the
once
andwhen
schedules is that theonce
schedule will only trigger once, and then remove itself from the scheduler. Thewhen
schedule will continue to trigger as long as the condition function returns true. -
By default,
once
checks the condition function every second. To override this, combine thewhen
schedule. with anevery
schedule:
Examples:
Running a function once a condition is met
do_after
Decorator to schedule a function to run after the specified time interval. Equivalent to after
schedule function.
See Also
after
: Creates a Schedule object that will trigger after the specified time interval.
do_between
Decorator to schedule a function to run between a specified time range. Equivalent to between
schedule function.
See Also
between
: Creates a Schedule object that runs a function between a specified time range.
do_cron
Decorator to schedule a function to run on a cron schedule. Equivalent to cron
schedule function.
See Also
cron
: Creates a Schedule object that runs a function on a cron schedule.
do_on
Decorator to schedule a function to run on a specific date or datetime. Equivalent to on
schedule function.
See Also
on
: Creates a Schedule object that will trigger on a specific date or datetime.
do_every
Decorator to schedule a function to run every x years/months/days/weeks/hours/minutes/seconds.
Equivalent to every
schedule function.
See Also
every
: Creates a Schedule object that will run every x years/months/days/weeks/hours/minutes/seconds.
get_scheduler
Returns the scheduler object associated with the backtester.
RETURNS | DESCRIPTION |
---|---|
BlockingScheduler
|
The scheduler object associated with the backtester. |
pause
resume
stop
Stops the backtester.
PARAMETER | DESCRIPTION |
---|---|
wait |
The number of seconds to wait before stopping the backtester. Defaults to 0 (no wait).
TYPE:
|
See Also
Warning
We recommend specifying the wait
parameter to give the backtester time to stop gracefully.
run
run(start=None, end=None, start_buffer=datetime.timedelta(), end_buffer=datetime.timedelta(), log_level=logging.INFO)
Runs the backtester from the specified start datetime to the end datetime.
PARAMETER | DESCRIPTION |
---|---|
start |
Datetime or ISO-8601 string ("YYYY-MM-DD") to start the run from (inclusive).
TYPE:
|
end |
Datetime or ISO-8601 string ("YYYY-MM-DD") to end the run at (inclusive).
TYPE:
|
start_buffer |
The data buffer to add to the start date when caching datafeeds. Defaults to 0.
TYPE:
|
end_buffer |
The data buffer to add to the end date when caching datafeeds. Defaults to 0.
TYPE:
|
log_level |
The logging level to use for the backtest. Defaults to logging.INFO.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
RuntimeError
|
If the backtester is already running. |
See Also
Examples:
Backtest a backtester between two past dates
Provide both the start and end dates in the past:
Backtest a backtester from a past date until the present
Provide only the start date in the past:
Run a live backtester until a future date
Provide only the end date in the future:
Run a live backtester indefinitely until stopped
Leave both the start and end dates blank: