Riy777 commited on
Commit
6f7ea8e
·
verified ·
1 Parent(s): 6537653

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -19
app.py CHANGED
@@ -167,39 +167,48 @@ def calculate_duration_str(timestamp_str):
167
  # ==============================================================================
168
  async def auto_pilot_loop():
169
  """
170
- Background task that triggers scanning cycles automatically based on logic.
 
171
  """
172
- logger.info("🤖 [Auto-Pilot] Daemon started.")
173
  while True:
174
  try:
175
- await asyncio.sleep(10) # Check every 10 seconds
176
 
177
- # Safety check: System must be ready
178
  if not sys_state.ready: continue
179
 
180
- # Update training status occasionally
181
  if hub_manager and int(time.time()) % 60 == 0:
182
  is_ready, msg = await hub_manager.check_training_readiness()
183
  sys_state.training_status_msg = msg
184
 
185
- # Trigger Cycle if Auto-Pilot is ON and system is IDLE
 
 
 
 
 
 
 
 
 
 
 
 
186
  if sys_state.auto_pilot and not sys_state.cycle_running and not sys_state.training_running:
187
- # Only scan if no active trades (Safety First Logic - Spot Mode)
188
- if trade_manager and len(trade_manager.open_positions) == 0:
189
-
190
- # Check time since last cycle (Throttling)
191
- if sys_state.last_cycle_time:
192
- elapsed = (datetime.now() - sys_state.last_cycle_time).total_seconds()
193
- if elapsed < 300: # Minimum 5 minutes wait between cycles
194
- continue
195
-
196
- logger.info("🤖 [Auto-Pilot] Triggering scan...")
197
- asyncio.create_task(run_unified_cycle())
198
- await asyncio.sleep(5)
199
 
200
  except Exception as e:
201
  logger.error(f"⚠️ [Auto-Pilot Error] {e}")
202
- await asyncio.sleep(30) # Backoff on error
203
 
204
  # ==============================================================================
205
  # 🚀 Lifespan (Server Startup/Shutdown & Injection)
 
167
  # ==============================================================================
168
  async def auto_pilot_loop():
169
  """
170
+ Background task that manages scanning AND monitoring.
171
+ Fixes the 'Blind Spot' where Guardians were not checked if a trade was open.
172
  """
173
+ logger.info("🤖 [Auto-Pilot] Daemon started (Monitoring Mode Enabled).")
174
  while True:
175
  try:
176
+ await asyncio.sleep(5) # Check every 5 seconds (Faster response)
177
 
 
178
  if not sys_state.ready: continue
179
 
180
+ # 1. حالة التدريب
181
  if hub_manager and int(time.time()) % 60 == 0:
182
  is_ready, msg = await hub_manager.check_training_readiness()
183
  sys_state.training_status_msg = msg
184
 
185
+ # 2. فحص حالة الصفقات المفتوحة (Watchdog Check)
186
+ # هذا هو الجزء الذي كان مفقوداً! نتأكد من أن الحراس يعملون.
187
+ if trade_manager and len(trade_manager.open_positions) > 0:
188
+ # التأكد من عمل الحلقات الخلفية
189
+ wd_status = await trade_manager.ensure_active_guardians()
190
+
191
+ # تحديث واجهة المستخدم برسائل الحارس
192
+ # إذا كانت الرسالة "No active trades" نتجاهلها، غير ذلك نعرضها
193
+ if "No active" not in wd_status:
194
+ sys_state.last_cycle_logs = trade_manager.latest_guardian_log
195
+ continue # ننتقل للدورة التالية ولا نقوم بمسح جديد
196
+
197
+ # 3. Trigger Cycle (فقط إذا لم تكن هناك صفقات)
198
  if sys_state.auto_pilot and not sys_state.cycle_running and not sys_state.training_running:
199
+ # Check time since last cycle
200
+ if sys_state.last_cycle_time:
201
+ elapsed = (datetime.now() - sys_state.last_cycle_time).total_seconds()
202
+ if elapsed < sys_state.scan_interval:
203
+ continue
204
+
205
+ logger.info("🤖 [Auto-Pilot] Triggering scan...")
206
+ asyncio.create_task(run_unified_cycle())
207
+ await asyncio.sleep(5)
 
 
 
208
 
209
  except Exception as e:
210
  logger.error(f"⚠️ [Auto-Pilot Error] {e}")
211
+ await asyncio.sleep(30)
212
 
213
  # ==============================================================================
214
  # 🚀 Lifespan (Server Startup/Shutdown & Injection)