From ea793769252491cce9a75f8ab7e4841b05662ae8 Mon Sep 17 00:00:00 2001 From: Martin Pelteshki <39273158+Al4ise@users.noreply.github.com> Date: Wed, 6 Nov 2024 12:11:34 +0200 Subject: [PATCH] Bots send data to cloud every 1 minute now --- lumibot/strategies/strategy_executor.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lumibot/strategies/strategy_executor.py b/lumibot/strategies/strategy_executor.py index fed49f902..80f2b34fe 100644 --- a/lumibot/strategies/strategy_executor.py +++ b/lumibot/strategies/strategy_executor.py @@ -34,6 +34,7 @@ def __init__(self, strategy): self._strategy_context = None self.broker = self.strategy.broker self.result = {} + self._in_trading_iteration = False # Create a dictionary of job stores. A job store is where the scheduler persists its jobs. In this case, # we create an in-memory job store for "default" and "On_Trading_Iteration" which is the job store we will @@ -303,6 +304,7 @@ def func_output(self, *args, **kwargs): self._before_lifecycle_method() result = func_input(self, *args, **kwargs) self._after_lifecycle_method() + return result return func_output @@ -389,6 +391,7 @@ def _before_starting_trading(self): def _on_trading_iteration(self): # Call the send_update_to_cloud method to send the strategy's data to the cloud. self.strategy.send_update_to_cloud() + self._in_trading_iteration = True # If we are running live, we need to check if it's time to execute the trading iteration. if not self.strategy.is_backtesting: @@ -442,6 +445,7 @@ def _on_trading_iteration(self): runtime = (end_dt - start_dt).total_seconds() # Variable Backup + self._in_trading_iteration = False self.strategy.backup_variables_to_db() # Update cron count to account for how long this iteration took to complete so that the next iteration will @@ -932,7 +936,7 @@ def _run_trading_session(self): should_continue = self.should_continue # Check if the strategy is 24/7 or if it's time to stop. is_247_or_should_we_stop = not is_247 or not should_we_stop - + if not jobs: print("Breaking loop because no jobs.") break @@ -945,7 +949,12 @@ def _run_trading_session(self): if not is_247_or_should_we_stop: print("Breaking loop because it's 24/7 and time to stop.") break - + + # Send data to cloud every minute. Ensure not being in a trading iteration currently as it can cause an incomplete data sync + if ((not hasattr(self, '_last_updated_cloud')) or (datetime.now() - self._last_updated_cloud >= timedelta(minutes=1))) and (not self._in_trading_iteration): + self.strategy.send_update_to_cloud() + self._last_updated_cloud = datetime.now() + # Handle LifeCycle methods current_datetime = self.strategy.get_datetime() current_date = current_datetime.date() @@ -1064,7 +1073,6 @@ def run(self): if not self._strategy_sleep(): self.result = self.strategy._analysis return False - try: self._on_strategy_end() except Exception as e: