Skip to content
All library documents

Using Strategy Date and Time Functions in Backtests and Live Trading

Article Lumibot

Summary

This documentation explains why a trading strategy may need its own view of the current date and time. The strategy's clock reflects the simulated point in time during a backtest and the relevant time during live trading. This matters when historical logic depends on dates or times: a backtest set in an earlier period should behave as though the strategy is operating in that period, rather than using the machine's present-day clock.

The page lists functions for retrieving a datetime or timestamp, rounding or finding the last minute or day, building a datetime range, and localizing or converting datetimes to the default timezone. It does not give code examples, describe the exact behavior of each function, or discuss timezone edge cases. Its main practical lesson is to use the strategy's time-aware methods when logic must follow simulated or trading time, then consult the individual function references for implementation details.

Key ideas

  • A strategy's current time can reflect the simulated date in a backtest or the time during live trading.
  • Time-aware methods help historical logic avoid relying on the computer's present-day clock.
  • The listed functions cover timestamps, rounded or prior time intervals, datetime ranges, and timezone conversion.
  • The overview does not explain function signatures or timezone edge cases, so the detailed references are needed for implementation.

Tags

Full text
# strategy methods.datetime


DateTime
===================================

.. meta::
   :description: DataTime functions are made so that you can get the current date and time that your strategy thinks it is, regardless of whether you are backtesting or trading live.

DataTime functions are made so that you can get the current date and time that your strategy thinks it is, regardless of whether you are backtesting or trading live. These can be especially useful when you're backtesting so that you can see what date/time it is according to the backtest (eg. if you're backtesting in the 1990s it can tell you that the strategy thinks it is Jan 10, 1991 rather than today's date). You can see a list of them below:

.. currentmodule:: lumibot.strategies.strategy


.. autosummary::
    :toctree: strategy_methods.datetime
    :template: strategy_methods_template.rst

        Strategy.get_datetime
        Strategy.get_timestamp
        Strategy.get_round_minute
        Strategy.get_last_minute
        Strategy.get_round_day
        Strategy.get_last_day
        Strategy.get_datetime_range
        Strategy.localize_datetime
        Strategy.to_default_timezone

Shown in full with attribution under the source's licence. Licence: GPL-3.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.