Spaces:
Paused
Paused
Update r2.py
Browse files
r2.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# r2.py (
|
| 2 |
import os
|
| 3 |
import traceback
|
| 4 |
import json
|
|
@@ -17,7 +17,7 @@ R2_ACCESS_KEY_ID = os.getenv("R2_ACCESS_KEY_ID")
|
|
| 17 |
R2_SECRET_ACCESS_KEY = os.getenv("R2_SECRET_ACCESS_KEY")
|
| 18 |
BUCKET_NAME = "trading"
|
| 19 |
|
| 20 |
-
# 🔴 الرصيد الافتراضي
|
| 21 |
INITIAL_CAPITAL = 10.0
|
| 22 |
|
| 23 |
# 📁 مفاتيح الملفات في R2
|
|
@@ -58,7 +58,7 @@ class R2Service:
|
|
| 58 |
raise RuntimeError(f"Failed to initialize S3 client: {e}")
|
| 59 |
|
| 60 |
# ==============================================================================
|
| 61 |
-
# 🔴 [هام جداً] دالة التصفير الشامل
|
| 62 |
# ==============================================================================
|
| 63 |
async def reset_all_stats_async(self):
|
| 64 |
"""تصفير المحفظة والسجلات التاريخية والعودة لنقطة الصفر"""
|
|
@@ -69,7 +69,7 @@ class R2Service:
|
|
| 69 |
initial_state = {
|
| 70 |
"current_capital_usd": INITIAL_CAPITAL,
|
| 71 |
"invested_capital_usd": 0.0,
|
| 72 |
-
"initial_capital_usd": INITIAL_CAPITAL,
|
| 73 |
"total_trades": 0,
|
| 74 |
"winning_trades": 0,
|
| 75 |
"losing_trades": 0,
|
|
@@ -216,6 +216,35 @@ class R2Service:
|
|
| 216 |
)
|
| 217 |
except Exception:
|
| 218 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
# ==============================================================================
|
| 221 |
# 🧠 بيانات التعلم العام
|
|
@@ -464,4 +493,4 @@ class R2Service:
|
|
| 464 |
async def load_whale_learning_config_async(self) -> Dict[str, Any]:
|
| 465 |
return await self._load_json_file_from_r2(WHALE_LEARNING_CONFIG_KEY, default={})
|
| 466 |
|
| 467 |
-
print("✅ Full R2 Service Loaded - Accounting, History &
|
|
|
|
| 1 |
+
# r2.py (V13.0 - GEM-Architect: Full Code with DeepSteward Audit)
|
| 2 |
import os
|
| 3 |
import traceback
|
| 4 |
import json
|
|
|
|
| 17 |
R2_SECRET_ACCESS_KEY = os.getenv("R2_SECRET_ACCESS_KEY")
|
| 18 |
BUCKET_NAME = "trading"
|
| 19 |
|
| 20 |
+
# 🔴 الرصيد الافتراضي
|
| 21 |
INITIAL_CAPITAL = 10.0
|
| 22 |
|
| 23 |
# 📁 مفاتيح الملفات في R2
|
|
|
|
| 58 |
raise RuntimeError(f"Failed to initialize S3 client: {e}")
|
| 59 |
|
| 60 |
# ==============================================================================
|
| 61 |
+
# 🔴 [هام جداً] دالة التصفير الشامل
|
| 62 |
# ==============================================================================
|
| 63 |
async def reset_all_stats_async(self):
|
| 64 |
"""تصفير المحفظة والسجلات التاريخية والعودة لنقطة الصفر"""
|
|
|
|
| 69 |
initial_state = {
|
| 70 |
"current_capital_usd": INITIAL_CAPITAL,
|
| 71 |
"invested_capital_usd": 0.0,
|
| 72 |
+
"initial_capital_usd": INITIAL_CAPITAL,
|
| 73 |
"total_trades": 0,
|
| 74 |
"winning_trades": 0,
|
| 75 |
"losing_trades": 0,
|
|
|
|
| 216 |
)
|
| 217 |
except Exception:
|
| 218 |
pass
|
| 219 |
+
|
| 220 |
+
# ==============================================================================
|
| 221 |
+
# 🧠 سجلات DeepSteward (Post-Exit Analysis) - [NEW]
|
| 222 |
+
# ==============================================================================
|
| 223 |
+
async def append_deep_steward_audit(self, audit_record: Dict[str, Any]):
|
| 224 |
+
"""
|
| 225 |
+
حفظ تحليل ما بعد الخروج في ملف خاص بالنموذج.
|
| 226 |
+
"""
|
| 227 |
+
try:
|
| 228 |
+
# اسم الملف يعتمد على اسم النموذج لضمان التنظيم
|
| 229 |
+
file_key = "DeepSteward_Audit_Log.json"
|
| 230 |
+
|
| 231 |
+
try:
|
| 232 |
+
response = self.s3_client.get_object(Bucket=self.BUCKET_NAME, Key=file_key)
|
| 233 |
+
history = json.loads(response['Body'].read())
|
| 234 |
+
except ClientError:
|
| 235 |
+
history = []
|
| 236 |
+
|
| 237 |
+
history.append(audit_record)
|
| 238 |
+
# نحتفظ بآخر 1000 سجل فقط لعدم تضخم الملف
|
| 239 |
+
if len(history) > 1000: history = history[-1000:]
|
| 240 |
+
|
| 241 |
+
data_json = json.dumps(history, indent=2, ensure_ascii=False).encode('utf-8')
|
| 242 |
+
self.s3_client.put_object(
|
| 243 |
+
Bucket=self.BUCKET_NAME, Key=file_key, Body=data_json, ContentType="application/json"
|
| 244 |
+
)
|
| 245 |
+
print(f"📝 [R2 Audit] تم تسجيل نتيجة قرار DeepSteward لـ {audit_record.get('symbol')}")
|
| 246 |
+
except Exception as e:
|
| 247 |
+
print(f"❌ [R2 Error] فشل حفظ سجل التدقيق: {e}")
|
| 248 |
|
| 249 |
# ==============================================================================
|
| 250 |
# 🧠 بيانات التعلم العام
|
|
|
|
| 493 |
async def load_whale_learning_config_async(self) -> Dict[str, Any]:
|
| 494 |
return await self._load_json_file_from_r2(WHALE_LEARNING_CONFIG_KEY, default={})
|
| 495 |
|
| 496 |
+
print("✅ Full R2 Service Loaded - Accounting, History, Reset & Audit Ready")
|