williams data structure in data provider

This commit is contained in:
Dobromir Popov
2025-05-31 00:26:05 +03:00
parent 0331bbfa7c
commit 7a0e468c3e
4 changed files with 960 additions and 76 deletions

View File

@ -2378,14 +2378,14 @@ class TradingDashboard:
net_pnl = leveraged_pnl - leveraged_fee - self.current_position['fees']
self.total_realized_pnl += net_pnl
self.total_fees += fee
self.total_fees += leveraged_fee
# Record the close trade
close_record = decision.copy()
close_record['position_action'] = 'CLOSE_SHORT'
close_record['entry_price'] = entry_price
close_record['pnl'] = net_pnl
close_record['fees'] = fee
close_record['fees'] = leveraged_fee
close_record['fee_type'] = fee_type
close_record['fee_rate'] = fee_rate
close_record['size'] = size # Use original position size for close
@ -2434,7 +2434,7 @@ class TradingDashboard:
# Now open long position (regardless of previous position)
if self.current_position is None:
# Open long position with confidence-based size
fee = decision['price'] * decision['size'] * fee_rate * self.leverage_multiplier # Leverage affects fees
fee = decision['price'] * decision['size'] * fee_rate # ✅ FIXED: No leverage on fees
self.current_position = {
'side': 'LONG',
'price': decision['price'],
@ -2471,14 +2471,14 @@ class TradingDashboard:
net_pnl = leveraged_pnl - leveraged_fee - self.current_position['fees']
self.total_realized_pnl += net_pnl
self.total_fees += fee
self.total_fees += leveraged_fee
# Record the close trade
close_record = decision.copy()
close_record['position_action'] = 'CLOSE_SHORT'
close_record['entry_price'] = entry_price
close_record['pnl'] = net_pnl
close_record['fees'] = fee
close_record['fees'] = leveraged_fee
close_record['fee_type'] = fee_type
close_record['fee_rate'] = fee_rate
self.session_trades.append(close_record)
@ -2539,14 +2539,14 @@ class TradingDashboard:
net_pnl = leveraged_pnl - leveraged_fee - self.current_position['fees']
self.total_realized_pnl += net_pnl
self.total_fees += fee
self.total_fees += leveraged_fee
# Record the close trade
close_record = decision.copy()
close_record['position_action'] = 'CLOSE_LONG'
close_record['entry_price'] = entry_price
close_record['pnl'] = net_pnl
close_record['fees'] = fee
close_record['fees'] = leveraged_fee
close_record['fee_type'] = fee_type
close_record['fee_rate'] = fee_rate
close_record['size'] = size # Use original position size for close
@ -2583,7 +2583,7 @@ class TradingDashboard:
# Now open short position (regardless of previous position)
if self.current_position is None:
# Open short position with confidence-based size
fee = decision['price'] * decision['size'] * fee_rate * self.leverage_multiplier # Leverage affects fees
fee = decision['price'] * decision['size'] * fee_rate # ✅ FIXED: No leverage on fees
self.current_position = {
'side': 'SHORT',
'price': decision['price'],
@ -2625,16 +2625,16 @@ class TradingDashboard:
else:
return 0.0, 0.0
# Apply leverage amplification
# Apply leverage amplification ONLY to P&L
leveraged_pnl = base_pnl * self.leverage_multiplier
# Calculate fees with leverage (higher position value = higher fees)
position_value = exit_price * size * self.leverage_multiplier
leveraged_fee = position_value * fee_rate
# Calculate fees WITHOUT leverage (normal position value)
position_value = exit_price * size # ✅ FIXED: No leverage multiplier
normal_fee = position_value * fee_rate # ✅ FIXED: Normal fees
logger.info(f"[LEVERAGE] {side} PnL: Base=${base_pnl:.2f} x {self.leverage_multiplier}x = ${leveraged_pnl:.2f}, Fee=${leveraged_fee:.4f}")
logger.info(f"[LEVERAGE] {side} PnL: Base=${base_pnl:.2f} x {self.leverage_multiplier}x = ${leveraged_pnl:.2f}, Fee=${normal_fee:.4f}")
return leveraged_pnl, leveraged_fee
return leveraged_pnl, normal_fee # ✅ FIXED: Return normal fee
except Exception as e:
logger.warning(f"Error calculating leveraged PnL and fees: {e}")
@ -3013,7 +3013,7 @@ class TradingDashboard:
logger.info(f"Using best {model_type} model: {model_info.model_name} "
f"(Score: {model_info.metrics.get_composite_score():.3f})")
else:
else:
logger.info("No managed models available, falling back to legacy loading")
# Fallback to original model loading logic
self._load_legacy_models()
@ -3021,7 +3021,7 @@ class TradingDashboard:
except ImportError:
logger.warning("ModelManager not available, using legacy model loading")
self._load_legacy_models()
except Exception as e:
except Exception as e:
logger.error(f"Error loading models via ModelManager: {e}")
self._load_legacy_models()
@ -3048,12 +3048,12 @@ class TradingDashboard:
def __init__(self, model):
self.model = model
self.model.eval()
def predict(self, feature_matrix):
with torch.no_grad():
if hasattr(feature_matrix, 'shape') and len(feature_matrix.shape) == 2:
feature_tensor = torch.FloatTensor(feature_matrix).unsqueeze(0)
else:
else:
feature_tensor = torch.FloatTensor(feature_matrix)
prediction = self.model(feature_tensor)
@ -3073,10 +3073,10 @@ class TradingDashboard:
prediction = exp_pred / np.sum(exp_pred)
return prediction
def get_memory_usage(self):
return 50 # MB estimate
def to_device(self, device):
self.model = self.model.to(device)
return self
@ -3090,7 +3090,7 @@ class TradingDashboard:
})
logger.info(f"Loaded CNN model: {model_file}")
except Exception as e:
except Exception as e:
logger.warning(f"Failed to load CNN model {model_file}: {e}")
# Check for RL models
@ -3101,12 +3101,12 @@ class TradingDashboard:
try:
checkpoint_path = os.path.join(rl_models_dir, model_file)
class RLWrapper:
def __init__(self, checkpoint_path):
class RLWrapper:
def __init__(self, checkpoint_path):
self.checkpoint_path = checkpoint_path
self.checkpoint = torch.load(checkpoint_path, map_location='cpu')
def predict(self, feature_matrix):
def predict(self, feature_matrix):
# Mock RL prediction
if hasattr(feature_matrix, 'shape'):
state_sum = np.sum(feature_matrix) % 100
@ -3117,17 +3117,17 @@ class TradingDashboard:
action_probs = [0.1, 0.1, 0.8] # BUY
elif state_sum < 30:
action_probs = [0.8, 0.1, 0.1] # SELL
else:
else:
action_probs = [0.2, 0.6, 0.2] # HOLD
return np.array(action_probs)
def get_memory_usage(self):
return 75 # MB estimate
def to_device(self, device):
return self
wrapper = RLWrapper(checkpoint_path)
self.available_models['rl'].append({
'name': model_file,
@ -3137,12 +3137,12 @@ class TradingDashboard:
})
logger.info(f"Loaded RL model: {model_file}")
except Exception as e:
except Exception as e:
logger.warning(f"Failed to load RL model {model_file}: {e}")
total_models = sum(len(models) for models in self.available_models.values())
logger.info(f"Legacy model loading complete. Total models: {total_models}")
except Exception as e:
logger.error(f"Error in legacy model loading: {e}")
# Initialize empty model structure