Skip to content

Commit

Permalink
Merge pull request #412 from Lumiwealth/adjust_datetime_for_delay
Browse files Browse the repository at this point in the history
get_datetime: added in optional delay accounting
  • Loading branch information
grzesir authored Apr 1, 2024
2 parents dfe6e45 + c7b45ce commit 7a5df2d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lumibot/data_sources/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,23 @@ def get_last_price(self, asset, quote=None, exchange=None):

# ========Python datetime helpers======================

def get_datetime(self):
def get_datetime(self, adjust_for_delay=False):
"""
Returns the current datetime in the default timezone
Parameters
----------
adjust_for_delay : bool
Whether to adjust the current time for the delay. This is useful for paper trading data sources that
provide delayed data.
Returns
-------
datetime
"""
current_time = self.to_default_timezone(datetime.now())
if adjust_for_delay and self._delay:
current_time -= self._delay
return current_time

def get_timestamp(self):
Expand Down
15 changes: 14 additions & 1 deletion lumibot/data_sources/data_source_backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,20 @@ def __init__(
# catch it here and ignore it in this class. Child classes that need it should error check it themselves.
self._config = config

def get_datetime(self):
def get_datetime(self, adjust_for_delay=False):
"""
Get the current datetime of the backtest.
Parameters
----------
adjust_for_delay: bool
Not used for backtesting data sources. This parameter is only used for live data sources.
Returns
-------
datetime
The current datetime of the backtest.
"""
return self._datetime

def get_datetime_range(self, length, timestep="minute", timeshift=None):
Expand Down
4 changes: 2 additions & 2 deletions lumibot/strategies/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2319,7 +2319,7 @@ def pytz(self):
"""
return self.broker.data_source.DEFAULT_PYTZ

def get_datetime(self):
def get_datetime(self, adjust_for_delay=False):
"""Returns the current datetime according to the data source. In a backtest this will be the current bar's datetime. In live trading this will be the current datetime on the exchange.
Returns
Expand All @@ -2333,7 +2333,7 @@ def get_datetime(self):
>>> datetime = self.get_datetime()
>>> self.log_message(f"The current datetime is {datetime}")
"""
return self.broker.data_source.get_datetime()
return self.broker.data_source.get_datetime(adjust_for_delay=adjust_for_delay)

def get_timestamp(self):
"""Returns the current timestamp according to the data source. In a backtest this will be the current bar's timestamp. In live trading this will be the current timestamp on the exchange.
Expand Down

0 comments on commit 7a5df2d

Please sign in to comment.