Spaces:
Running
Running
Update helpers.py
Browse files- helpers.py +52 -3
helpers.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import re
|
| 2 |
from datetime import datetime
|
| 3 |
|
| 4 |
def safe_float_conversion(value, default=0.0):
|
|
@@ -27,9 +27,58 @@ def _apply_patience_logic(decision, hold_minutes, trade_data, processed_data):
|
|
| 27 |
profit_loss_percent = 0
|
| 28 |
|
| 29 |
if profit_loss_percent < 2:
|
| 30 |
-
print(f"
|
| 31 |
decision['action'] = "HOLD"
|
| 32 |
decision['reasoning'] = f"Patience Filter: Blocked premature sell. Held for {hold_minutes:.1f}m. Giving trade more time."
|
| 33 |
return decision
|
| 34 |
|
| 35 |
-
return decision
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, re
|
| 2 |
from datetime import datetime
|
| 3 |
|
| 4 |
def safe_float_conversion(value, default=0.0):
|
|
|
|
| 27 |
profit_loss_percent = 0
|
| 28 |
|
| 29 |
if profit_loss_percent < 2:
|
| 30 |
+
print(f"Blocked premature selling! Only {hold_minutes:.1f} minutes held, PnL: {profit_loss_percent:.2f}%")
|
| 31 |
decision['action'] = "HOLD"
|
| 32 |
decision['reasoning'] = f"Patience Filter: Blocked premature sell. Held for {hold_minutes:.1f}m. Giving trade more time."
|
| 33 |
return decision
|
| 34 |
|
| 35 |
+
return decision
|
| 36 |
+
|
| 37 |
+
def parse_json_from_response(response_text: str):
|
| 38 |
+
"""استخراج JSON من رد النموذج"""
|
| 39 |
+
try:
|
| 40 |
+
json_match = re.search(r'```json\n(.*?)\n```', response_text, re.DOTALL)
|
| 41 |
+
if json_match:
|
| 42 |
+
return json_match.group(1).strip()
|
| 43 |
+
|
| 44 |
+
json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
|
| 45 |
+
if json_match:
|
| 46 |
+
return json_match.group()
|
| 47 |
+
|
| 48 |
+
return None
|
| 49 |
+
except Exception:
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
def validate_required_fields(data_dict: dict, required_fields: list) -> bool:
|
| 53 |
+
"""التحقق من وجود الحقول المطلوبة"""
|
| 54 |
+
return all(field in data_dict for field in required_fields)
|
| 55 |
+
|
| 56 |
+
def format_technical_indicators(advanced_indicators):
|
| 57 |
+
"""تنسيق المؤشرات الفنية"""
|
| 58 |
+
if not advanced_indicators:
|
| 59 |
+
return "No data for advanced indicators."
|
| 60 |
+
|
| 61 |
+
summary = []
|
| 62 |
+
for timeframe, indicators in advanced_indicators.items():
|
| 63 |
+
if indicators:
|
| 64 |
+
parts = []
|
| 65 |
+
if 'rsi' in indicators: parts.append(f"RSI: {indicators['rsi']:.2f}")
|
| 66 |
+
if 'macd_hist' in indicators: parts.append(f"MACD Hist: {indicators['macd_hist']:.4f}")
|
| 67 |
+
if 'volume_ratio' in indicators: parts.append(f"Volume: {indicators['volume_ratio']:.2f}x")
|
| 68 |
+
if parts:
|
| 69 |
+
summary.append(f"{timeframe}: {', '.join(parts)}")
|
| 70 |
+
|
| 71 |
+
return "\n".join(summary) if summary else "Insufficient indicator data."
|
| 72 |
+
|
| 73 |
+
def format_strategy_scores(strategy_scores, recommended_strategy):
|
| 74 |
+
"""تنسيق نتائج الاستراتيجيات"""
|
| 75 |
+
if not strategy_scores:
|
| 76 |
+
return "No strategy data available."
|
| 77 |
+
|
| 78 |
+
summary = [f"Recommended Strategy: {recommended_strategy}"]
|
| 79 |
+
sorted_scores = sorted(strategy_scores.items(), key=lambda item: item[1], reverse=True)
|
| 80 |
+
for strategy, score in sorted_scores:
|
| 81 |
+
score_display = f"{score:.3f}" if isinstance(score, (int, float)) else str(score)
|
| 82 |
+
summary.append(f" • {strategy}: {score_display}")
|
| 83 |
+
|
| 84 |
+
return "\n".join(summary)
|