Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bots send data to cloud every 1 minute #614

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lumibot/strategies/strategy_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
Loading