Skip to content

Commit

Permalink
fixed a bug where at a specific time the bot will go in an endless loop
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesir committed Dec 16, 2023
1 parent 44b5d3b commit e9fd959
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
12 changes: 9 additions & 3 deletions lumibot/brokers/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
import pandas as pd
import pandas_market_calendars as mcal
from dateutil import tz
from termcolor import colored

from lumibot.data_sources import DataSource
from lumibot.entities import Asset, Order, Position
from lumibot.trading_builtins import SafeList
from termcolor import colored


class Broker(ABC):
Expand Down Expand Up @@ -551,7 +552,11 @@ def market_hours(self, market="NASDAQ", close=True, next=False, date=None):

market = self.market if self.market is not None else market
mkt_cal = mcal.get_calendar(market)
date = date if date is not None else datetime.now()

# Get the current datetime in UTC (because the market hours are in UTC)
dt_now_utc = datetime.now(timezone.utc)

date = date if date is not None else dt_now_utc
trading_hours = mkt_cal.schedule(start_date=date, end_date=date + timedelta(weeks=1)).head(2)

row = 0 if not next else 1
Expand Down Expand Up @@ -612,7 +617,8 @@ def get_time_to_open(self):

def get_time_to_close(self):
"""Return the remaining time for the market to close in seconds"""
close_time = self.utc_to_local(self.market_hours(close=True))
market_hours = self.market_hours(close=True)
close_time = self.utc_to_local(market_hours)
current_time = datetime.now().astimezone(tz=tz.tzlocal())
if self.is_market_open():
result = close_time.timestamp() - current_time.timestamp()
Expand Down
4 changes: 2 additions & 2 deletions lumibot/strategies/strategy_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ def _on_trading_iteration(self):
# If the cron count is not equal to the cron count target, return and do not execute the
# on_trading_iteration method.
self.strategy.log_message(
f"The cron count is only {self.cron_count} but the target is {self.cron_count_target}"
f", skipping this trading iteration because it's not time yet.",
"Skipping this iteration because it's not time yet. "
f"{self.cron_count_target - self.cron_count} iterations left before we run again",
color="blue",
)
return
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="lumibot",
version="2.9.5",
version="2.9.6",
author="Robert Grzesik",
author_email="[email protected]",
description="Backtesting and Trading Library, Made by Lumiwealth",
Expand Down

0 comments on commit e9fd959

Please sign in to comment.