Update helpers.py
Browse files- helpers.py +65 -2
helpers.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
-
import os, re, json
|
| 2 |
from datetime import datetime
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def safe_float_conversion(value, default=0.0):
|
| 5 |
try:
|
|
@@ -127,4 +129,65 @@ def local_re_analyze_trade(trade_data, processed_data):
|
|
| 127 |
return {
|
| 128 |
"action": action, "reasoning": reasoning, "new_stop_loss": None, "new_take_profit": None,
|
| 129 |
"new_expected_minutes": None, "model_source": "local", "strategy": strategy
|
| 130 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, re, json, hashlib
|
| 2 |
from datetime import datetime
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
def safe_float_conversion(value, default=0.0):
|
| 7 |
try:
|
|
|
|
| 129 |
return {
|
| 130 |
"action": action, "reasoning": reasoning, "new_stop_loss": None, "new_take_profit": None,
|
| 131 |
"new_expected_minutes": None, "model_source": "local", "strategy": strategy
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
def validate_candidate_data_enhanced(candidate):
|
| 135 |
+
try:
|
| 136 |
+
required_fields = ['symbol', 'current_price', 'final_score', 'enhanced_final_score']
|
| 137 |
+
for field in required_fields:
|
| 138 |
+
if field not in candidate: candidate[field] = 0.0 if field.endswith('_score') or field == 'current_price' else 'UNKNOWN'
|
| 139 |
+
candidate['current_price'] = safe_float_conversion(candidate.get('current_price'), 0.0)
|
| 140 |
+
candidate['final_score'] = safe_float_conversion(candidate.get('final_score'), 0.5)
|
| 141 |
+
candidate['enhanced_final_score'] = safe_float_conversion(candidate.get('enhanced_final_score'), candidate['final_score'])
|
| 142 |
+
if 'reasons_for_candidacy' not in candidate: candidate['reasons_for_candidacy'] = ['unknown_reason']
|
| 143 |
+
if 'sentiment_data' not in candidate: candidate['sentiment_data'] = {'btc_sentiment': 'NEUTRAL','fear_and_greed_index': 50,'general_whale_activity': {'sentiment': 'NEUTRAL', 'critical_alert': False}}
|
| 144 |
+
if 'advanced_indicators' not in candidate: candidate['advanced_indicators'] = {}
|
| 145 |
+
if 'strategy_scores' not in candidate: candidate['strategy_scores'] = {}
|
| 146 |
+
if 'target_strategy' not in candidate: candidate['target_strategy'] = 'GENERIC'
|
| 147 |
+
return True
|
| 148 |
+
except Exception as error:
|
| 149 |
+
print(f"Failed to validate candidate data for {candidate.get('symbol')}: {error}")
|
| 150 |
+
return False
|
| 151 |
+
|
| 152 |
+
def normalize_weights(weights_dict):
|
| 153 |
+
total = sum(weights_dict.values())
|
| 154 |
+
if total > 0:
|
| 155 |
+
for strategy in weights_dict:
|
| 156 |
+
weights_dict[strategy] /= total
|
| 157 |
+
return weights_dict
|
| 158 |
+
|
| 159 |
+
def calculate_market_volatility(market_context):
|
| 160 |
+
try:
|
| 161 |
+
btc_price = market_context.get('bitcoin_price_usd', 0)
|
| 162 |
+
fear_greed = market_context.get('fear_and_greed_index', 50)
|
| 163 |
+
whale_sentiment = market_context.get('general_whale_activity', {}).get('sentiment', 'NEUTRAL')
|
| 164 |
+
|
| 165 |
+
volatility_score = 0
|
| 166 |
+
|
| 167 |
+
if btc_price > 0:
|
| 168 |
+
if abs(fear_greed - 50) > 20:
|
| 169 |
+
volatility_score += 1
|
| 170 |
+
|
| 171 |
+
if whale_sentiment in ['BULLISH', 'BEARISH']:
|
| 172 |
+
volatility_score += 1
|
| 173 |
+
elif whale_sentiment == 'SLIGHTLY_BULLISH':
|
| 174 |
+
volatility_score += 0.5
|
| 175 |
+
|
| 176 |
+
if volatility_score >= 1.5:
|
| 177 |
+
return "high"
|
| 178 |
+
elif volatility_score >= 0.5:
|
| 179 |
+
return "medium"
|
| 180 |
+
else:
|
| 181 |
+
return "low"
|
| 182 |
+
|
| 183 |
+
except Exception as e:
|
| 184 |
+
print(f"Volatility calculation error: {e}")
|
| 185 |
+
return "medium"
|
| 186 |
+
|
| 187 |
+
def generate_trade_id():
|
| 188 |
+
return str(int(time.time()))
|
| 189 |
+
|
| 190 |
+
def should_update_weights(performance_history_count):
|
| 191 |
+
if performance_history_count <= 10:
|
| 192 |
+
return True
|
| 193 |
+
return performance_history_count % 3 == 0
|