File size: 35,084 Bytes
708a613 490e45e 6f41fff f3ca3ad 490e45e 708a613 490e45e 708a613 490e45e 708a613 490e45e f3ca3ad 708a613 490e45e 708a613 b7c060a 708a613 b7c060a 708a613 b7c060a 490e45e 708a613 490e45e 708a613 490e45e 708a613 490e45e 708a613 490e45e f3ca3ad 490e45e 708a613 490e45e 708a613 490e45e f3ca3ad 708a613 f3ca3ad 490e45e f3ca3ad 490e45e 708a613 490e45e b7c060a 490e45e f3ca3ad 490e45e 708a613 490e45e 708a613 490e45e f3ca3ad 490e45e f3ca3ad 708a613 f3ca3ad 490e45e f3ca3ad 708a613 490e45e 708a613 490e45e 708a613 490e45e 708a613 490e45e 708a613 490e45e f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 490e45e 708a613 f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 708a613 f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 490e45e 708a613 f3ca3ad 490e45e f3ca3ad 490e45e 708a613 f3ca3ad 708a613 f3ca3ad 490e45e 708a613 490e45e f3ca3ad 708a613 f3ca3ad 490e45e f3ca3ad 490e45e 708a613 490e45e 708a613 490e45e 708a613 490e45e 708a613 490e45e 708a613 b7c060a 708a613 b7c060a 708a613 f3ca3ad 490e45e b7c060a f3ca3ad 708a613 f3ca3ad 708a613 f3ca3ad 490e45e f3ca3ad 708a613 f3ca3ad 708a613 f3ca3ad b7c060a f3ca3ad b7c060a 490e45e 708a613 b7c060a f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 490e45e f3ca3ad 708a613 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 |
# trade_manager.py (محدث بالكامل مع محرك الخروج الديناميكي وإدارة التعارض)
import asyncio
import json
import time
import traceback
from datetime import datetime, timedelta
from helpers import safe_float_conversion, _apply_patience_logic
# 🔴 جديد: نحتاج إلى pandas لحساب ATR في المراقب
try:
import pandas as pd
import pandas_ta as ta
PANDAS_TA_AVAILABLE = True
except ImportError:
PANDAS_TA_AVAILABLE = False
print("⚠️ pandas_ta not available. ATR Trailing Stop will be disabled.")
# 🔴 جديد: محرك الخروج الديناميكي (كلاس داخلي أو مساعد)
class _DynamicExitEngine:
def __init__(self, data_manager):
self.data_manager = data_manager
async def evaluate_exit(self, trade: dict, current_price: float):
"""
يقيّم ما إذا كان يجب إغلاق الصفقة بناءً على ملف الخروج الخاص بها.
يرجع: (should_close, close_reason, updated_trade_object)
"""
if not PANDAS_TA_AVAILABLE:
# إذا لم تكن المكتبات متاحة، نعود إلى الوقف الثابت البسيط
return await self._evaluate_fixed_exit(trade, current_price)
try:
exit_profile = trade.get('decision_data', {}).get('exit_profile', 'FIXED_TARGET')
exit_params = trade.get('decision_data', {}).get('exit_parameters', {})
# 1. التحقق من الوقف الكارثي (Hard Stop) والهدف النهائي (Take Profit) دائماً
hard_stop = trade.get('stop_loss')
take_profit = trade.get('take_profit')
if hard_stop and current_price <= hard_stop:
return True, f"Hard Stop Loss hit: {current_price} <= {hard_stop}", trade
if take_profit and current_price >= take_profit:
return True, f"Final Take Profit hit: {current_price} >= {take_profit}", trade
# 2. التحقق من الوقف الديناميكي (المتحرك) إذا كان موجوداً
dynamic_stop = trade.get('dynamic_stop_loss')
if dynamic_stop and current_price <= dynamic_stop:
return True, f"Dynamic Trailing Stop hit: {current_price} <= {dynamic_stop}", trade
# 3. تنفيذ منطق ملف الخروج
if exit_profile == "ATR_TRAILING":
return await self._evaluate_atr_trailing(trade, current_price, exit_params)
elif exit_profile == "TIME_BASED":
return await self._evaluate_time_based(trade, current_price, exit_params)
elif exit_profile == "FIXED_TARGET":
# تم التعامل معه بالفعل (TP/SL)، لكن يمكن إضافة منطق نقطة التعادل
return await self._evaluate_break_even(trade, current_price, exit_params.get("break_even_trigger_percent", 0))
elif exit_profile == "SIGNAL_BASED":
# (منطق الطوارئ - يمكن توسيعه لاحقاً)
# مثال: التحقق من ارتفاع حجم البيع
# volume_spike = await self._check_emergency_signals(trade['symbol'])
# if volume_spike:
# return True, f"Emergency Signal Stop (Volume Spike)", trade
pass
# إذا لم يتم تشغيل أي شيء، تحقق من نقطة التعادل (إذا لم تكن جزءاً من ATR)
if exit_profile != "ATR_TRAILING":
return await self._evaluate_break_even(trade, current_price, exit_params.get("break_even_trigger_percent", 0))
return False, "No exit criteria met", trade
except Exception as e:
print(f"❌ Error in DynamicExitEngine for {trade.get('symbol')}: {e}")
traceback.print_exc()
# في حالة الخطأ، نعود إلى الوقف الثابت الآمن
return await self._evaluate_fixed_exit(trade, current_price)
async def _evaluate_fixed_exit(self, trade: dict, current_price: float):
"""الخروج الآمن (الافتراضي): الوقف الثابت والهدف الثابت فقط"""
hard_stop = trade.get('stop_loss')
take_profit = trade.get('take_profit')
if hard_stop and current_price <= hard_stop:
return True, f"Hard Stop Loss hit: {current_price} <= {hard_stop}", trade
if take_profit and current_price >= take_profit:
return True, f"Final Take Profit hit: {current_price} >= {take_profit}", trade
# التحقق من الوقف الديناميكي (إذا تم تعيينه مسبقاً)
dynamic_stop = trade.get('dynamic_stop_loss')
if dynamic_stop and current_price <= dynamic_stop:
return True, f"Dynamic Stop hit: {current_price} <= {dynamic_stop}", trade
return False, "No exit criteria met", trade
async def _evaluate_atr_trailing(self, trade: dict, current_price: float, params: dict):
"""حساب الوقف المتحرك بناءً على ATR"""
try:
atr_period = params.get("atr_period", 14)
atr_multiplier = params.get("atr_multiplier", 2.0)
break_even_trigger_percent = params.get("break_even_trigger_percent", 0) # مثال: 1.5%
# 1. جلب بيانات الشموع لحساب ATR (نستخدم إطار زمني قصير مثل 15m)
ohlcv_data = await self._fetch_ohlcv_for_atr(trade['symbol'], '15m', atr_period + 50) # +50 لتهيئة المؤشر
if ohlcv_data is None:
return False, "ATR data unavailable", trade # لا يمكن الحساب، استمر
# 2. حساب ATR
df = pd.DataFrame(ohlcv_data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['atr'] = ta.atr(df['high'], df['low'], df['close'], length=atr_period)
if df['atr'].dropna().empty:
return False, "ATR calculation failed", trade
current_atr = df['atr'].iloc[-1]
if current_atr is None or current_atr == 0:
return False, "Invalid ATR value", trade
# 3. حساب سعر الوقف الجديد بناءً على ATR
new_atr_stop = current_price - (current_atr * atr_multiplier)
# 4. التحقق من نقطة التعادل (Break Even)
current_dynamic_stop = trade.get('dynamic_stop_loss', 0)
entry_price = trade.get('entry_price')
if break_even_trigger_percent > 0:
trigger_price = entry_price * (1 + break_even_trigger_percent / 100)
if current_price >= trigger_price:
# إذا وصلنا لهدف نقطة التعادل، الوقف الجديد *على الأقل* سعر الدخول
new_atr_stop = max(new_atr_stop, entry_price)
# 5. تحديث الوقف الديناميكي
# الوقف المتحرك يجب أن يرتفع فقط، لا ينخفض
if new_atr_stop > current_dynamic_stop:
trade['dynamic_stop_loss'] = new_atr_stop
print(f"📈 {trade['symbol']}: ATR Trailing Stop updated to {new_atr_stop:.6f}")
# ملاحظة: هذا التحديث محلي للمهمة فقط (لا يحفظ في R2 كل دقيقة)
# سيتم التحقق من الوقف المحدث في الدورة التالية (أو في بداية هذه الدالة)
return False, "ATR Trailing logic executed", trade
except Exception as e:
print(f"❌ Error in ATR Trailing logic for {trade.get('symbol')}: {e}")
return False, "ATR logic failed", trade # فشل آمن، لا تغلق الصفقة
async def _evaluate_time_based(self, trade: dict, current_price: float, params: dict):
"""التحقق من الوقف الزمني"""
exit_after_minutes = params.get("exit_after_minutes", 0)
if exit_after_minutes <= 0:
return False, "Time stop not configured", trade
entry_time = datetime.fromisoformat(trade.get('entry_timestamp'))
elapsed_minutes = (datetime.now() - entry_time).total_seconds() / 60
if elapsed_minutes >= exit_after_minutes:
return True, f"Time Stop hit: {elapsed_minutes:.1f}m >= {exit_after_minutes}m", trade
return False, "Time stop not hit", trade
async def _evaluate_break_even(self, trade: dict, current_price: float, break_even_trigger_percent: float):
"""منطق نقطة التعادل (إذا لم يكن جزءاً من ATR)"""
if break_even_trigger_percent <= 0:
return False, "Break-even not configured", trade
entry_price = trade.get('entry_price')
current_dynamic_stop = trade.get('dynamic_stop_loss', 0)
# إذا كان الوقف الحالي أقل من سعر الدخول
if current_dynamic_stop < entry_price:
trigger_price = entry_price * (1 + break_even_trigger_percent / 100)
if current_price >= trigger_price:
trade['dynamic_stop_loss'] = entry_price # ارفع الوقف إلى سعر الدخول
print(f"📈 {trade['symbol']}: Break-Even Stop activated at {entry_price:.6f}")
return False, "Break-even logic executed", trade
async def _fetch_ohlcv_for_atr(self, symbol, timeframe, limit):
"""جلب بيانات الشموع لحساب ATR (دالة مساعدة)"""
try:
if not self.data_manager or not self.data_manager.exchange:
return None
# جلب البيانات بشكل متزامن (لأننا داخل مهمة asyncio بالفعل)
# هذه الدالة (fetch_ohlcv) في ccxt ليست async
loop = asyncio.get_event_loop()
ohlcv_data = await loop.run_in_executor(
None,
lambda: self.data_manager.exchange.fetch_ohlcv(symbol, timeframe, limit=limit)
)
if ohlcv_data and len(ohlcv_data) >= limit:
return ohlcv_data
else:
return None
except Exception as e:
print(f"⚠️ Failed to fetch OHLCV for ATR ({symbol}): {e}")
return None
class TradeManager:
def __init__(self, r2_service, learning_engine=None, data_manager=None, state_manager=None): # 🔴 جديد
self.r2_service = r2_service
self.learning_engine = learning_engine
self.data_manager = data_manager
self.state_manager = state_manager # 🔴 جديد: لإدارة التعارض
self.monitoring_tasks = {}
self.is_running = False
self.monitoring_errors = {}
self.max_consecutive_errors = 5
self.exit_engine = _DynamicExitEngine(self.data_manager) # 🔴 جديد: تهيئة محرك الخروج
async def open_trade(self, symbol, decision, current_price):
try:
trade_type = decision.get("trade_type", "LONG")
if trade_type == "SHORT":
print(f"⚠️ تم رفض فتح صفقة SHORT لـ {symbol}. النظام مصمم لـ SPOT فقط.")
await self.r2_service.save_system_logs_async({
"trade_open_rejected": True, "reason": "SHORT trade not allowed", "symbol": symbol
})
return None
portfolio_state = await self.r2_service.get_portfolio_state_async()
available_capital = portfolio_state.get("current_capital_usd", 0)
if available_capital < 1:
print(f"❌ رأس المال غير كافي لـ {symbol}: {available_capital}")
return None
expected_target_minutes = decision.get('expected_target_minutes', 15)
expected_target_minutes = max(5, min(expected_target_minutes, 45))
expected_target_time = (datetime.now() + timedelta(minutes=expected_target_minutes)).isoformat()
strategy = decision.get('strategy')
if not strategy or strategy == 'unknown':
strategy = 'GENERIC'
# 🔴 جديد: جلب ملف الخروج والمعاملات
exit_profile = decision.get('exit_profile', 'FIXED_TARGET') # الافتراضي هو الوقف الثابت
exit_parameters = decision.get('exit_parameters', {})
trades = await self.r2_service.get_open_trades_async()
new_trade = {
"id": str(int(datetime.now().timestamp())),
"symbol": symbol,
"entry_price": current_price,
"entry_timestamp": datetime.now().isoformat(),
"decision_data": decision, # 🔴 هذا يحتوي الآن على ملف الخروج
"status": "OPEN",
"stop_loss": decision.get("stop_loss"), # الوقف الكارثي
"take_profit": decision.get("take_profit"), # الهدف النهائي
"dynamic_stop_loss": decision.get("stop_loss"), # 🔴 جديد: يبدأ الوقف المتحرك عند الوقف الكارثي
"trade_type": "LONG",
"position_size_usd": available_capital,
"expected_target_minutes": expected_target_minutes,
"expected_target_time": expected_target_time,
"is_monitored": True,
"strategy": strategy,
"monitoring_started": False
}
trades.append(new_trade)
await self.r2_service.save_open_trades_async(trades)
portfolio_state["invested_capital_usd"] = available_capital
portfolio_state["current_capital_usd"] = 0.0
portfolio_state["total_trades"] = portfolio_state.get("total_trades", 0) + 1
await self.r2_service.save_portfolio_state_async(portfolio_state)
await self.r2_service.save_system_logs_async({
"new_trade_opened": True, "symbol": symbol, "position_size": available_capital,
"strategy": strategy, "exit_profile": exit_profile # 🔴 جديد: تسجيل ملف الخروج
})
print(f"✅ تم فتح صفقة جديدة (LONG) لـ {symbol} (استراتيجية: {strategy} | ملف خروج: {exit_profile})")
return new_trade
except Exception as e:
print(f"❌ فشل فتح صفقة لـ {symbol}: {e}")
await self.r2_service.save_system_logs_async({
"trade_open_failed": True, "symbol": symbol, "error": str(e)
})
raise
async def close_trade(self, trade_to_close, close_price, reason="إغلاق بالنظام"):
try:
symbol = trade_to_close.get('symbol')
trade_to_close['status'] = 'CLOSED'
trade_to_close['close_price'] = close_price
trade_to_close['close_timestamp'] = datetime.now().isoformat()
trade_to_close['is_monitored'] = False
entry_price = trade_to_close['entry_price']
position_size = trade_to_close['position_size_usd']
trade_type = "LONG"
strategy = trade_to_close.get('strategy', 'unknown')
pnl = 0.0
pnl_percent = 0.0
if entry_price and entry_price > 0 and close_price and close_price > 0:
try:
pnl_percent = ((close_price - entry_price) / entry_price) * 100
pnl = position_size * (pnl_percent / 100)
except (TypeError, ZeroDivisionError) as calc_error:
pnl = 0.0
pnl_percent = 0.0
trade_to_close['pnl_usd'] = pnl
trade_to_close['pnl_percent'] = pnl_percent
await self._archive_closed_trade(trade_to_close)
await self._update_trade_summary(trade_to_close)
portfolio_state = await self.r2_service.get_portfolio_state_async()
current_capital = portfolio_state.get("current_capital_usd", 0)
new_capital = current_capital + position_size + pnl
portfolio_state["current_capital_usd"] = new_capital
portfolio_state["invested_capital_usd"] = 0.0
if pnl > 0:
portfolio_state["winning_trades"] = portfolio_state.get("winning_trades", 0) + 1
portfolio_state["total_profit_usd"] = portfolio_state.get("total_profit_usd", 0.0) + pnl
elif pnl < 0:
portfolio_state["total_loss_usd"] = portfolio_state.get("total_loss_usd", 0.0) + abs(pnl)
await self.r2_service.save_portfolio_state_async(portfolio_state)
open_trades = await self.r2_service.get_open_trades_async()
trades_to_keep = [t for t in open_trades if t.get('id') != trade_to_close.get('id')]
await self.r2_service.save_open_trades_async(trades_to_keep)
if symbol in self.monitoring_tasks:
del self.monitoring_tasks[symbol]
if symbol in self.monitoring_errors:
del self.monitoring_errors[symbol]
await self.r2_service.save_system_logs_async({
"trade_closed": True, "symbol": symbol, "pnl_usd": pnl, "pnl_percent": pnl_percent,
"new_capital": new_capital, "strategy": strategy, "trade_type": trade_type, "reason": reason
})
if self.learning_engine and self.learning_engine.initialized:
# 🔴 نمرر الصفقة كاملة (تحتوي على decision_data -> exit_profile)
await self.learning_engine.analyze_trade_outcome(trade_to_close, reason)
print(f"✅ تم إغلاق صفقة {symbol} - السبب: {reason} - الربح: {pnl_percent:+.2f}%")
return True
except Exception as e:
print(f"❌ فشل إغلاق صفقة {trade_to_close.get('symbol')}: {e}")
await self.r2_service.save_system_logs_async({
"trade_close_failed": True, "symbol": trade_to_close.get('symbol'), "error": str(e)
})
raise
async def update_trade(self, trade_to_update, re_analysis_decision):
try:
symbol = trade_to_update.get('symbol')
# 🔴 جديد: تحديث ملف الخروج والمعاملات
if re_analysis_decision.get('action') == "UPDATE_TRADE":
trade_to_update['stop_loss'] = re_analysis_decision['new_stop_loss']
trade_to_update['take_profit'] = re_analysis_decision['new_take_profit']
# تحديث الوقف المتحرك ليبدأ من الوقف الجديد
trade_to_update['dynamic_stop_loss'] = re_analysis_decision['new_stop_loss']
trade_to_update['decision_data']['exit_profile'] = re_analysis_decision['new_exit_profile']
trade_to_update['decision_data']['exit_parameters'] = re_analysis_decision['new_exit_parameters']
print(f" 🔄 {symbol}: تم تحديث ملف الخروج إلى {re_analysis_decision['new_exit_profile']}")
new_expected_minutes = re_analysis_decision.get('new_expected_minutes')
if new_expected_minutes:
new_expected_minutes = max(5, min(new_expected_minutes, 45))
trade_to_update['expected_target_minutes'] = new_expected_minutes
trade_to_update['expected_target_time'] = (datetime.now() + timedelta(minutes=new_expected_minutes)).isoformat()
original_strategy = trade_to_update.get('strategy')
if not original_strategy or original_strategy == 'unknown':
original_strategy = re_analysis_decision.get('strategy', 'GENERIC')
trade_to_update['strategy'] = original_strategy
trade_to_update['decision_data']['reasoning'] = re_analysis_decision.get('reasoning') # تحديث سبب القرار
trade_to_update['is_monitored'] = True
trade_to_update['trade_type'] = "LONG"
open_trades = await self.r2_service.get_open_trades_async()
for i, trade in enumerate(open_trades):
if trade.get('id') == trade_to_update.get('id'):
open_trades[i] = trade_to_update
break
await self.r2_service.save_open_trades_async(open_trades)
await self.r2_service.save_system_logs_async({
"trade_updated": True, "symbol": symbol, "action": "UPDATE_TRADE",
"strategy": original_strategy,
"new_exit_profile": re_analysis_decision.get('new_exit_profile', 'N/A')
})
print(f"✅ تم تحديث صفقة {symbol}")
return True
except Exception as e:
print(f"❌ فشل تحديث صفقة {trade_to_update.get('symbol')}: {e}")
raise
async def immediate_close_trade(self, symbol, close_price, reason="المراقبة الفورية"):
try:
open_trades = await self.r2_service.get_open_trades_async()
trade_to_close = None
for trade in open_trades:
if trade['symbol'] == symbol and trade['status'] == 'OPEN':
trade_to_close = trade
break
if not trade_to_close:
print(f"⚠️ لم يتم العثور على صفقة مفتوحة لـ {symbol}")
return False
await self.close_trade(trade_to_close, close_price, reason)
return True
except Exception as e:
print(f"❌ فشل الإغلاق الفوري لـ {symbol}: {e}")
return False
async def start_trade_monitoring(self):
self.is_running = True
print(f"🔍 بدء مراقبة الصفقات (Dynamic Exit Engine) (Pandas: {PANDAS_TA_AVAILABLE})...")
while self.is_running:
try:
open_trades = await self.r2_service.get_open_trades_async()
current_time = time.time()
for trade in open_trades:
symbol = trade['symbol']
if self.monitoring_errors.get(symbol, 0) >= self.max_consecutive_errors:
if symbol in self.monitoring_tasks:
del self.monitoring_tasks[symbol]
continue
if symbol not in self.monitoring_tasks:
task = asyncio.create_task(self._monitor_single_trade(trade))
self.monitoring_tasks[symbol] = {
'task': task,
'start_time': current_time,
'trade_object': trade # 🔴 حفظ نسخة محلية من الصفقة
}
trade['monitoring_started'] = True
current_symbols = {trade['symbol'] for trade in open_trades}
for symbol in list(self.monitoring_tasks.keys()):
if symbol not in current_symbols:
task_info = self.monitoring_tasks[symbol]
if not task_info['task'].done():
task_info['task'].cancel()
del self.monitoring_tasks[symbol]
if symbol in self.monitoring_errors:
del self.monitoring_errors[symbol]
await asyncio.sleep(10) # الفحص الرئيسي كل 10 ثوانٍ
except Exception as error:
print(f"❌ خطأ في مراقبة الصفقات: {error}")
await asyncio.sleep(30)
async def _monitor_single_trade(self, trade_object):
"""
🔴 (أعيدت كتابته بالكامل)
المراقب التكتيكي (1 دقيقة) الذي ينفذ ملف الخروج الديناميكي.
"""
symbol = trade_object['symbol']
# 🔴 نستخدم النسخة المحلية من الصفقة لتتبع الوقف المتحرك
local_trade = trade_object.copy()
print(f"🔍 بدء مراقبة الصفقة (تكتيكي): {symbol} (ملف: {local_trade.get('decision_data', {}).get('exit_profile', 'N/A')})")
while (symbol in self.monitoring_tasks and
self.is_running and
self.monitoring_errors.get(symbol, 0) < self.max_consecutive_errors):
try:
# 🔴 1. التحقق من قفل التحليل الاستراتيجي (منع التعارض)
if self.state_manager and self.state_manager.trade_analysis_lock.locked():
print(f"⏸️ [Monitor] إيقاف مؤقت لـ {symbol} (التحليل الاستراتيجي يعمل...)")
await asyncio.sleep(10) # انتظار انتهاء التحليل
continue # تخطي هذه الدورة
if not self.data_manager:
print(f"⚠️ DataManager غير متوفر لـ {symbol}")
await asyncio.sleep(15)
continue
# 🔴 2. جلب السعر الحالي
try:
current_price = await asyncio.wait_for(
self.data_manager.get_latest_price_async(symbol),
timeout=10
)
except asyncio.TimeoutError:
print(f"⏰ مهلة انتظار السعر لـ {symbol}")
self._increment_monitoring_error(symbol)
await asyncio.sleep(15)
continue
if not current_price:
print(f"⚠️ لم يتم الحصول على سعر لـ {symbol}")
self._increment_monitoring_error(symbol)
await asyncio.sleep(15)
continue
# 🔴 3. تنفيذ محرك الخروج الديناميكي
# نمرر النسخة المحلية من الصفقة (local_trade)
should_close, close_reason, updated_local_trade = await self.exit_engine.evaluate_exit(
local_trade,
current_price
)
# تحديث النسخة المحلية (تحتوي على dynamic_stop_loss المحدث)
local_trade = updated_local_trade
# 🔴 4. اتخاذ قرار الإغلاق
if should_close:
print(f"🛑 [Monitor] قرار إغلاق لـ {symbol}: {close_reason}")
if self.r2_service.acquire_lock(): # محاولة قفل R2 للإغلاق
try:
# قبل الإغلاق، تأكد من جلب أحدث نسخة من R2
latest_trade_data = await self.r2_service.get_trade_by_symbol_async(symbol)
if latest_trade_data and latest_trade_data['status'] == 'OPEN':
await self.immediate_close_trade(symbol, current_price, f"Tactical Monitor: {close_reason}")
else:
print(f"⚠️ الصفقة {symbol} لم تعد مفتوحة، تم إلغاء الإغلاق التكتيكي.")
except Exception as close_error:
print(f"❌ فشل الإغلاق التلقائي لـ {symbol}: {close_error}")
finally:
self.r2_service.release_lock()
break # الخروج من حلقة المراقبة بعد محاولة الإغلاق
# 🔴 5. إعادة تعيين عداد الأخطاء والانتظار
if symbol in self.monitoring_errors:
self.monitoring_errors[symbol] = 0
# انتظار 60 ثانية (دورة المراقب التكتيكي)
await asyncio.sleep(60)
except Exception as error:
error_count = self._increment_monitoring_error(symbol)
print(f"❌ خطأ في مراقبة {symbol} (الخطأ #{error_count}): {error}")
traceback.print_exc()
if error_count >= self.max_consecutive_errors:
print(f"🚨 إيقاف مراقبة {symbol} بسبب الأخطاء المتتالية")
await self.r2_service.save_system_logs_async({
"monitoring_stopped": True, "symbol": symbol, "error_count": error_count, "error": str(error)
})
break # الخروج من الحلقة بسبب الأخطاء
await asyncio.sleep(30) # انتظار أطول بعد الخطأ
print(f"🛑 توقيف مراقبة الصفقة (تكتيكي): {symbol}")
if symbol in self.monitoring_tasks:
del self.monitoring_tasks[symbol]
if symbol in self.monitoring_errors:
del self.monitoring_errors[symbol]
def _increment_monitoring_error(self, symbol):
if symbol not in self.monitoring_errors:
self.monitoring_errors[symbol] = 0
self.monitoring_errors[symbol] += 1
return self.monitoring_errors[symbol]
def stop_monitoring(self):
self.is_running = False
print("🛑 إيقاف جميع مهام المراقبة...")
for symbol, task_info in self.monitoring_tasks.items():
if not task_info['task'].done():
task_info['task'].cancel()
print(f"✅ تم إلغاء مهمة مراقبة {symbol}")
self.monitoring_tasks.clear()
self.monitoring_errors.clear()
async def _archive_closed_trade(self, closed_trade):
try:
key = "closed_trades_history.json"
try:
response = self.r2_service.s3_client.get_object(Bucket="trading", Key=key)
history = json.loads(response['Body'].read())
except Exception:
history = []
history.append(closed_trade)
if len(history) > 1000:
history = history[-1000:]
data_json = json.dumps(history, indent=2).encode('utf-8')
self.r2_service.s3_client.put_object(
Bucket="trading", Key=key, Body=data_json, ContentType="application/json"
)
except Exception as e:
print(f"❌ فشل أرشفة الصفقة: {e}")
async def _update_trade_summary(self, closed_trade):
try:
key = "trade_summary.json"
try:
response = self.r2_service.s3_client.get_object(Bucket="trading", Key=key)
summary = json.loads(response['Body'].read())
except Exception:
summary = {
"total_trades": 0, "winning_trades": 0, "losing_trades": 0,
"total_profit_usd": 0.0, "total_loss_usd": 0.0, "win_percentage": 0.0,
"avg_profit_per_trade": 0.0, "avg_loss_per_trade": 0.0,
"largest_win": 0.0, "largest_loss": 0.0,
"last_updated": datetime.now().isoformat()
}
pnl = closed_trade.get('pnl_usd', 0.0)
summary['total_trades'] += 1
if pnl >= 0:
summary['winning_trades'] += 1
summary['total_profit_usd'] += pnl
if pnl > summary.get('largest_win', 0):
summary['largest_win'] = pnl
else:
summary['losing_trades'] += 1
summary['total_loss_usd'] += abs(pnl)
if abs(pnl) > summary.get('largest_loss', 0):
summary['largest_loss'] = abs(pnl)
if summary['total_trades'] > 0:
summary['win_percentage'] = (summary['winning_trades'] / summary['total_trades']) * 100
if summary['winning_trades'] > 0:
summary['avg_profit_per_trade'] = summary['total_profit_usd'] / summary['winning_trades']
if summary['losing_trades'] > 0:
summary['avg_loss_per_trade'] = summary['total_loss_usd'] / summary['losing_trades']
summary['last_updated'] = datetime.now().isoformat()
data_json = json.dumps(summary, indent=2).encode('utf-8')
self.r2_service.s3_client.put_object(
Bucket="trading", Key=key, Body=data_json, ContentType="application/json"
)
except Exception as e:
print(f"❌ فشل تحديث ملخص التداول: {e}")
async def get_open_trades(self):
try:
return await self.r2_service.get_open_trades_async()
except Exception as e:
print(f"❌ فشل جلب الصفقات المفتوحة: {e}")
return []
async def get_trade_by_symbol(self, symbol):
try:
open_trades = await self.get_open_trades()
for trade in open_trades:
if trade['symbol'] == symbol and trade['status'] == 'OPEN':
return trade
return None
except Exception as e:
print(f"❌ فشل البحث عن صفقة {symbol}: {e}")
return None
def get_monitoring_status(self):
return {
'is_running': self.is_running,
'active_tasks': len(self.monitoring_tasks),
'monitoring_errors': dict(self.monitoring_errors),
'max_consecutive_errors': self.max_consecutive_errors
}
print(f"✅ Trade Manager loaded - V2 (Dynamic Exit Engine & Lock-Aware Monitor / Pandas: {PANDAS_TA_AVAILABLE})") |