fixed trading and leverage
This commit is contained in:
@ -824,37 +824,14 @@ class CleanTradingDashboard:
|
||||
else: # SHORT or SELL
|
||||
raw_pnl_per_unit = entry_price - current_price
|
||||
|
||||
# Apply current leverage to unrealized P&L
|
||||
leveraged_unrealized_pnl = raw_pnl_per_unit * size * self.current_leverage
|
||||
total_session_pnl += leveraged_unrealized_pnl
|
||||
|
||||
side = self.current_position.get('side', 'UNKNOWN')
|
||||
size = self.current_position.get('size', 0)
|
||||
entry_price = self.current_position.get('price', 0)
|
||||
|
||||
if entry_price and size > 0:
|
||||
# Calculate unrealized P&L with current leverage
|
||||
if side.upper() == 'LONG' or side.upper() == 'BUY':
|
||||
raw_pnl_per_unit = current_price - entry_price
|
||||
else: # SHORT or SELL
|
||||
raw_pnl_per_unit = entry_price - current_price
|
||||
|
||||
# Apply current leverage to unrealized P&L
|
||||
leveraged_unrealized_pnl = raw_pnl_per_unit * size * self.current_leverage
|
||||
total_session_pnl += leveraged_unrealized_pnl
|
||||
side = self.current_position.get('side', 'UNKNOWN')
|
||||
size = self.current_position.get('size', 0)
|
||||
entry_price = self.current_position.get('price', 0)
|
||||
|
||||
if entry_price and size > 0:
|
||||
# Calculate unrealized P&L with current leverage
|
||||
if side.upper() == 'LONG' or side.upper() == 'BUY':
|
||||
raw_pnl_per_unit = current_price - entry_price
|
||||
else: # SHORT or SELL
|
||||
raw_pnl_per_unit = entry_price - current_price
|
||||
|
||||
# Apply current leverage to unrealized P&L
|
||||
leveraged_unrealized_pnl = raw_pnl_per_unit * size * self.current_leverage
|
||||
# Apply leverage only if not already applied by exchange
|
||||
leverage_applied_by_exchange = self._get_leverage_applied_by_exchange()
|
||||
if leverage_applied_by_exchange:
|
||||
# Broker already applies leverage, so use base P&L
|
||||
leveraged_unrealized_pnl = raw_pnl_per_unit * size
|
||||
else:
|
||||
# Apply leverage locally
|
||||
leveraged_unrealized_pnl = raw_pnl_per_unit * size * self.current_leverage
|
||||
total_session_pnl += leveraged_unrealized_pnl
|
||||
|
||||
session_pnl_str = f"${total_session_pnl:.2f}"
|
||||
@ -879,10 +856,15 @@ class CleanTradingDashboard:
|
||||
else: # SHORT or SELL
|
||||
raw_pnl_per_unit = entry_price - current_price
|
||||
|
||||
# Apply current leverage to P&L calculation
|
||||
# With leverage, P&L is amplified by the leverage factor
|
||||
leveraged_pnl_per_unit = raw_pnl_per_unit * self.current_leverage
|
||||
unrealized_pnl = leveraged_pnl_per_unit * size
|
||||
# Apply leverage only if not already applied by exchange
|
||||
leverage_applied_by_exchange = self._get_leverage_applied_by_exchange()
|
||||
if leverage_applied_by_exchange:
|
||||
# Broker already applies leverage, so use base P&L
|
||||
unrealized_pnl = raw_pnl_per_unit * size
|
||||
else:
|
||||
# Apply leverage locally
|
||||
leveraged_pnl_per_unit = raw_pnl_per_unit * self.current_leverage
|
||||
unrealized_pnl = leveraged_pnl_per_unit * size
|
||||
|
||||
# Format P&L string with color
|
||||
if unrealized_pnl >= 0:
|
||||
@ -1334,6 +1316,16 @@ class CleanTradingDashboard:
|
||||
# logger.error(f"Error updating cold start mode: {e}")
|
||||
# return "ERROR", "fw-bold text-danger"
|
||||
|
||||
def _get_leverage_applied_by_exchange(self) -> bool:
|
||||
"""Check if leverage is already applied by the exchange"""
|
||||
try:
|
||||
if self.trading_executor and hasattr(self.trading_executor, 'primary_config'):
|
||||
return self.trading_executor.primary_config.get('leverage_applied_by_exchange', False)
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.debug(f"Error checking leverage_applied_by_exchange: {e}")
|
||||
return False
|
||||
|
||||
def _get_current_price(self, symbol: str) -> Optional[float]:
|
||||
"""Get current price for symbol - ONLY using our data providers"""
|
||||
try:
|
||||
@ -4101,8 +4093,14 @@ class CleanTradingDashboard:
|
||||
else: # SHORT
|
||||
raw_pnl_per_unit = entry_price - current_price
|
||||
|
||||
# Apply current leverage to P&L calculation
|
||||
leveraged_unrealized_pnl = raw_pnl_per_unit * size * self.current_leverage
|
||||
# Apply leverage only if not already applied by exchange
|
||||
leverage_applied_by_exchange = self._get_leverage_applied_by_exchange()
|
||||
if leverage_applied_by_exchange:
|
||||
# Broker already applies leverage, so use base P&L
|
||||
leveraged_unrealized_pnl = raw_pnl_per_unit * size
|
||||
else:
|
||||
# Apply leverage locally
|
||||
leveraged_unrealized_pnl = raw_pnl_per_unit * size * self.current_leverage
|
||||
|
||||
# Calculate profit incentive - bigger profits create stronger incentive to close
|
||||
if leveraged_unrealized_pnl > 0:
|
||||
|
Reference in New Issue
Block a user