diff --git a/src/BrighterTrades.py b/src/BrighterTrades.py index 7c23be0..ffdafe0 100644 --- a/src/BrighterTrades.py +++ b/src/BrighterTrades.py @@ -1690,12 +1690,14 @@ class BrighterTrades: if trade.status in ['pending', 'open', 'unfilled']: # Cancel the unfilled order result = self.trades.cancel_order(str(trade_id)) - reply_type = "order_cancelled" if result.get('success') else "trade_error" else: # Close the position for this trade's symbol broker_key = 'paper' if trade.broker_kind == 'paper' else f"{trade.broker_exchange}_{trade.broker_mode}" result = self.trades.close_position(trade.creator, trade.symbol, broker_key) - reply_type = "position_closed" if result.get('success') else "trade_error" + + # Always include trade_id and use consistent event name for frontend + result['trade_id'] = str(trade_id) + reply_type = "trade_closed" if result.get('success') else "trade_error" return standard_reply(reply_type, result) diff --git a/src/Exchange.py b/src/Exchange.py index 7063fbc..a43c9db 100644 --- a/src/Exchange.py +++ b/src/Exchange.py @@ -85,7 +85,13 @@ class Exchange: client.set_sandbox_mode(True) logger.info(f"Sandbox mode enabled for {self.exchange_id}") except Exception as e: - logger.warning(f"Could not enable sandbox mode for {self.exchange_id}: {e}") + # CRITICAL: Do NOT continue with production if testnet was requested + # This prevents users from accidentally trading real money + logger.error(f"TESTNET UNAVAILABLE: {self.exchange_id} does not support sandbox mode: {e}") + raise ValueError( + f"Testnet/sandbox mode is not available for {self.exchange_id}. " + f"Please use paper trading mode instead, or trade on production with caution." + ) return client diff --git a/src/ExchangeInterface.py b/src/ExchangeInterface.py index 7961b3c..8281987 100644 --- a/src/ExchangeInterface.py +++ b/src/ExchangeInterface.py @@ -126,6 +126,9 @@ class ExchangeInterface: pass # No existing entry, that's fine # Create new exchange with explicit testnet setting + if not exchange_name: + logger.error("Cannot create exchange: exchange_name is required") + return False logger.info(f"Creating {exchange_name} for {user_name} with testnet={testnet}") exchange = Exchange(name=exchange_name, api_keys=api_keys, exchange_id=exchange_name.lower(), testnet=testnet) diff --git a/src/app.py b/src/app.py index e58ce69..28d38a1 100644 --- a/src/app.py +++ b/src/app.py @@ -186,30 +186,35 @@ def strategy_execution_loop(): # This is the only place where brokers are polled for order fills if brighter_trades.manual_broker_manager: try: - # Collect prices for broker updates - # Paper trades use symbol-only keys (single synthetic market) - # Live trades use exchange:symbol keys + # Collect prices for broker updates from positions/orders (not Trade.exchange) broker_price_updates = {} - for trade in brighter_trades.trades.active_trades.values(): - if trade.broker_order_id: # Only broker-managed trades - try: - is_paper = trade.broker_kind == 'paper' - exchange = getattr(trade, 'exchange', None) or trade.target - if is_paper: - # Paper trades: single synthetic market, use first available exchange - price = brighter_trades.exchanges.get_price(trade.symbol) - if price: - # Paper uses symbol-only key - broker_price_updates[trade.symbol] = price - else: - # Live trades: use specific exchange + # Get required price feeds from paper broker positions/orders + paper_user_ids = brighter_trades.manual_broker_manager.get_active_paper_user_ids() + for user_id in paper_user_ids: + feeds = brighter_trades.manual_broker_manager.get_required_price_feeds(user_id) + for exchange, symbol in feeds: + if exchange: + # Exchange-qualified: fetch from specific exchange + price = brighter_trades.exchanges.get_price(symbol, exchange) + if price and price > 0: + broker_price_updates[f"{exchange.lower()}:{symbol}"] = price + else: + # No exchange: use default price source + price = brighter_trades.exchanges.get_price(symbol) + if price and price > 0: + broker_price_updates[symbol] = price + + # Also collect for live trades (unchanged logic) + for trade in brighter_trades.trades.active_trades.values(): + if trade.broker_order_id and trade.broker_kind == 'live': + try: + exchange = getattr(trade, 'exchange', None) or trade.target + if exchange: price = brighter_trades.exchanges.get_price(trade.symbol, exchange) - if price: - # Live uses exchange:symbol key + if price and price > 0: price_key = f"{exchange.lower()}:{trade.symbol}" broker_price_updates[price_key] = price - # Also add symbol-only as fallback broker_price_updates[trade.symbol] = price except Exception: pass @@ -221,34 +226,26 @@ def strategy_execution_loop(): event_type = event.get('type', 'fill') if event_type == 'sltp_triggered': - # SL/TP triggered - find and settle related trades + # SL/TP triggered - reconcile matching paper trades only. symbol = event.get('symbol') trigger_price = event.get('trigger_price', 0) user_id = event.get('user_id') - - # Find ALL matching paper trades for this symbol and settle them - trades_to_settle = [] - for trade in list(brighter_trades.trades.active_trades.values()): - if trade.symbol == symbol and (trade.is_paper or trade.broker_kind == 'paper'): - trades_to_settle.append(trade) - user_id = user_id or trade.creator - - # Settle each matching trade - for trade in trades_to_settle: - # Settle the trade at the trigger price - trade.settle(qty=trade.stats.get('qty_filled', trade.base_order_qty), price=trigger_price) - # Move from active to settled - if trade.unique_id in brighter_trades.trades.active_trades: - del brighter_trades.trades.active_trades[trade.unique_id] - brighter_trades.trades.settled_trades[trade.unique_id] = trade - brighter_trades.trades._save_trade(trade) - _loop_debug.debug(f"Settled trade {trade.unique_id} via SL/TP at {trigger_price}") + trade_ids = [] + if user_id and trigger_price: + trade_ids = brighter_trades.trades.settle_broker_closed_position( + user_id=user_id, + symbol=symbol, + broker_key='paper', + close_price=trigger_price + ) + _loop_debug.debug( + f"Reconciled SL/TP close for user={user_id} symbol={symbol}: {trade_ids}" + ) # Notify user if user_id: user_name = brighter_trades.users.get_username(user_id=user_id) if user_name: - trade_ids = [t.unique_id for t in trades_to_settle] socketio.emit('message', { 'reply': 'sltp_triggered', 'data': sanitize_for_json({ @@ -295,6 +292,52 @@ def strategy_execution_loop(): }) }, room=user_name) _loop_debug.debug(f"Emitted order_filled to room={user_name}") + else: + user_id = event.get('user_id') + broker_key = event.get('broker_key') + symbol = event.get('symbol') + side = str(event.get('side') or '').lower() + filled_price = event.get('filled_price', event.get('price', 0)) + + if user_id: + user_name = brighter_trades.users.get_username(user_id=user_id) + + # Always emit order_filled so broker-backed panels refresh even when + # the fill belongs to a close order that has no local opening trade ID. + if user_name: + socketio.emit('message', { + 'reply': 'order_filled', + 'data': sanitize_for_json({ + 'order_id': event.get('order_id'), + 'trade_id': None, + 'symbol': symbol, + 'side': event.get('side'), + 'filled_qty': event.get('filled_qty', event.get('size', 0)), + 'filled_price': filled_price, + 'status': 'filled', + 'broker_kind': event.get('broker_kind'), + 'broker_key': broker_key + }) + }, room=user_name) + + # A live sell fill without a matching opening trade is typically a + # broker-initiated close order from the position-close flow. + if side == 'sell' and broker_key and symbol and filled_price: + settled_ids = brighter_trades.trades.settle_broker_closed_position( + user_id=user_id, + symbol=symbol, + broker_key=broker_key, + close_price=filled_price + ) + if settled_ids and user_name: + socketio.emit('message', { + 'reply': 'position_closed', + 'data': sanitize_for_json({ + 'symbol': symbol, + 'broker_key': broker_key, + 'closed_trades': settled_ids + }) + }, room=user_name) except Exception as e: _loop_debug.debug(f"Exception in broker update: {e}") @@ -317,6 +360,9 @@ def strategy_execution_loop(): exchange_symbols.add((None, trade.symbol)) _loop_debug.debug(f"Exchange+symbols to fetch: {exchange_symbols}") + # Log at INFO level for live trades debugging + if any(ex and ex.lower() not in ['paper', 'test_exchange'] for ex, _ in exchange_symbols): + logger.info(f"[PRICE FETCH] exchange_symbols to fetch: {exchange_symbols}") price_updates = {} for exchange, symbol in exchange_symbols: try: @@ -1251,6 +1297,20 @@ def close_manual_position(symbol): try: result = brighter_trades.trades.close_position(user_id, symbol, broker_key) + + # Emit position_closed event to refresh UI + if result.get('success'): + user_name = brighter_trades.users.get_username(user_id=user_id) + if user_name: + socketio.emit('message', { + 'reply': 'position_closed', + 'data': { + 'symbol': symbol, + 'broker_key': broker_key, + 'closed_trades': result.get('closed_trades', []) + } + }, room=user_name) + return jsonify(result) except Exception as e: logger.error(f"Error closing position {symbol}: {e}", exc_info=True) @@ -1265,15 +1325,44 @@ def get_manual_balance(): return jsonify({'success': False, 'message': 'Not authenticated'}), 401 broker_key = request.args.get('broker_key', 'paper') + chart_exchange = (request.args.get('exchange') or '').strip().lower() or None try: total = brighter_trades.manual_broker_manager.get_broker_balance(user_id, broker_key) available = brighter_trades.manual_broker_manager.get_available_balance(user_id, broker_key) + + # Fallback for live mode: if manual broker balance is unavailable/stale, use the + # cached direct exchange balances for the exchange currently shown in the chart. + fallback_source = None + if broker_key != 'paper' and chart_exchange and total == 0.0 and available == 0.0: + user_name = brighter_trades.users.get_username(user_id=user_id) + exchange_balances = brighter_trades.exchanges.get_exchange_balances(user_name, chart_exchange) + + quote_balance = 0.0 + if exchange_balances is not None: + for asset in ('USDT', 'USD', 'BUSD', 'USDC'): + match = next( + ( + bal for bal in exchange_balances + if str(bal.get('asset', '')).upper() == asset + ), + None + ) + if match: + quote_balance = float(match.get('balance', 0.0) or 0.0) + break + + if quote_balance > 0: + total = quote_balance + available = quote_balance + fallback_source = 'exchange' + return jsonify({ 'success': True, 'total': total, 'available': available, - 'broker_key': broker_key + 'broker_key': broker_key, + 'source': fallback_source or 'broker' }) except Exception as e: logger.error(f"Error getting balance: {e}", exc_info=True) @@ -1315,6 +1404,21 @@ def get_trade_history(): return jsonify({'success': False, 'message': str(e)}), 500 +@app.route('/api/manual/paper/reset', methods=['POST']) +def reset_paper_balance(): + """Reset the paper trading broker to initial state.""" + user_id = _get_current_user_id() + if not user_id: + return jsonify({'success': False, 'message': 'Not authenticated'}), 401 + + try: + result = brighter_trades.manual_broker_manager.reset_paper_broker(user_id) + return jsonify(result) + except Exception as e: + logger.error(f"Error resetting paper broker: {e}", exc_info=True) + return jsonify({'success': False, 'message': str(e)}), 500 + + # ============================================================================= # External Sources API Routes # ============================================================================= diff --git a/src/brokers/base_broker.py b/src/brokers/base_broker.py index 74c6a1c..93bdc8f 100644 --- a/src/brokers/base_broker.py +++ b/src/brokers/base_broker.py @@ -62,6 +62,8 @@ class Position: current_price: float unrealized_pnl: float realized_pnl: float = 0.0 + entry_commission: float = 0.0 # Fee paid on entry, included in P&L + price_source: Optional[str] = None # Exchange to use for price lookups (e.g., 'kucoin') def to_dict(self) -> Dict[str, Any]: """Convert position to dictionary for persistence.""" @@ -72,6 +74,8 @@ class Position: 'current_price': self.current_price, 'unrealized_pnl': self.unrealized_pnl, 'realized_pnl': self.realized_pnl, + 'entry_commission': self.entry_commission, + 'price_source': self.price_source, } @classmethod @@ -84,6 +88,8 @@ class Position: current_price=data['current_price'], unrealized_pnl=data['unrealized_pnl'], realized_pnl=data.get('realized_pnl', 0.0), + entry_commission=data.get('entry_commission', 0.0), + price_source=data.get('price_source'), ) diff --git a/src/brokers/live_broker.py b/src/brokers/live_broker.py index e29c3cd..7f05816 100644 --- a/src/brokers/live_broker.py +++ b/src/brokers/live_broker.py @@ -425,8 +425,8 @@ class LiveBroker(BaseBroker): # Create local order tracking symbol = ex_order['symbol'] - side = OrderSide.BUY if ex_order['side'].lower() == 'buy' else OrderSide.SELL - order_type = OrderType.LIMIT if ex_order.get('type', 'limit').lower() == 'limit' else OrderType.MARKET + side = OrderSide.BUY if (ex_order.get('side') or 'buy').lower() == 'buy' else OrderSide.SELL + order_type = OrderType.LIMIT if (ex_order.get('type') or 'limit').lower() == 'limit' else OrderType.MARKET size = float(ex_order['quantity']) price = float(ex_order.get('price', 0) or 0) @@ -640,13 +640,20 @@ class LiveBroker(BaseBroker): ) # Parse order status from exchange response - ex_status = exchange_order.get('status', 'open').lower() + # Use 'or' to handle case where status key exists but value is None + ex_status = (exchange_order.get('status') or 'open').lower() if ex_status == 'closed' or ex_status == 'filled': order.status = OrderStatus.FILLED order.filled_qty = float(exchange_order.get('filled', size)) - order.filled_price = float(exchange_order.get('average', price or 0)) + raw_avg = exchange_order.get('average') + order.filled_price = float(raw_avg if raw_avg is not None else (price or 0)) order.filled_at = datetime.now(timezone.utc) + # DEBUG: Log exchange response for price validation + logger.info(f"[FILL DEBUG] Exchange response: average={raw_avg}, price={price}, " + f"filled={order.filled_qty}, cost={exchange_order.get('cost')}, " + f"final filled_price={order.filled_price}") + # Calculate commission fee = exchange_order.get('fee', {}) if fee: @@ -752,7 +759,8 @@ class LiveBroker(BaseBroker): def _update_order_from_exchange(self, order: LiveOrder, ex_order: Dict[str, Any]): """Update local order with exchange data.""" - ex_status = ex_order.get('status', 'open').lower() + # Use 'or' to handle case where status key exists but value is None + ex_status = (ex_order.get('status') or 'open').lower() if ex_status == 'closed' or ex_status == 'filled': order.status = OrderStatus.FILLED diff --git a/src/brokers/paper_broker.py b/src/brokers/paper_broker.py index 62a0de8..ec1036b 100644 --- a/src/brokers/paper_broker.py +++ b/src/brokers/paper_broker.py @@ -32,7 +32,8 @@ class PaperOrder: price: Optional[float] = None, stop_loss: Optional[float] = None, take_profit: Optional[float] = None, - time_in_force: str = 'GTC' + time_in_force: str = 'GTC', + exchange: Optional[str] = None ): self.order_id = order_id self.symbol = symbol @@ -43,6 +44,7 @@ class PaperOrder: self.stop_loss = stop_loss self.take_profit = take_profit self.time_in_force = time_in_force + self.exchange = exchange # Exchange for price feed (e.g., 'kucoin') self.status = OrderStatus.PENDING self.filled_qty = 0.0 self.filled_price = 0.0 @@ -68,7 +70,8 @@ class PaperOrder: 'commission': self.commission, 'locked_funds': self.locked_funds, 'created_at': self.created_at.isoformat(), - 'filled_at': self.filled_at.isoformat() if self.filled_at else None + 'filled_at': self.filled_at.isoformat() if self.filled_at else None, + 'exchange': self.exchange, } @@ -123,10 +126,21 @@ class PaperBroker(BaseBroker): """Set the price provider callable.""" self._price_provider = provider - def update_price(self, symbol: str, price: float): - """Update the current price for a symbol.""" + def update_price(self, symbol: str, price: float, exchange: Optional[str] = None): + """Update price, optionally qualified by exchange.""" + if exchange: + # Store exchange-qualified price + self._current_prices[f"{exchange}:{symbol}"] = price + # Always store symbol-only for backward compat self._current_prices[symbol] = price + @staticmethod + def _should_fill_limit_order(side: OrderSide, current_price: float, limit_price: float) -> bool: + """Return True when the current price crosses a limit order's fill threshold.""" + if side == OrderSide.BUY: + return current_price <= limit_price + return current_price >= limit_price + def place_order( self, symbol: str, @@ -136,13 +150,14 @@ class PaperBroker(BaseBroker): price: Optional[float] = None, stop_loss: Optional[float] = None, take_profit: Optional[float] = None, - time_in_force: str = 'GTC' + time_in_force: str = 'GTC', + exchange: Optional[str] = None ) -> OrderResult: """Place a paper trading order.""" order_id = str(uuid.uuid4())[:8] - # Validate order - current_price = self.get_current_price(symbol) + # Validate order - use exchange-aware price lookup + current_price = self.get_current_price(symbol, exchange) if current_price <= 0: return OrderResult( success=False, @@ -189,7 +204,8 @@ class PaperBroker(BaseBroker): price=price, stop_loss=stop_loss, take_profit=take_profit, - time_in_force=time_in_force + time_in_force=time_in_force, + exchange=exchange ) # For market orders, fill immediately @@ -198,6 +214,39 @@ class PaperBroker(BaseBroker): self._fill_order(order, fill_price) logger.info(f"PaperBroker: Market order filled: {side.value} {size} {symbol} @ {fill_price:.4f}") else: + is_marketable = self._should_fill_limit_order(side, current_price, execution_price) + + # IOC/FOK must fill immediately or fail immediately. + if time_in_force in ['IOC', 'FOK']: + if is_marketable: + self._fill_order(order, execution_price) + logger.info( + f"PaperBroker: {time_in_force} limit order filled immediately: " + f"{side.value} {size} {symbol} @ {execution_price}" + ) + else: + order.status = OrderStatus.CANCELLED if time_in_force == 'IOC' else OrderStatus.EXPIRED + self._orders[order_id] = order + logger.info( + f"PaperBroker: {time_in_force} limit order not fillable immediately: " + f"{side.value} {size} {symbol} @ {price}" + ) + return OrderResult( + success=False, + order_id=order_id, + status=order.status, + message=f"{time_in_force} limit order could not be filled immediately" + ) + return OrderResult( + success=True, + order_id=order_id, + status=order.status, + filled_qty=order.filled_qty, + filled_price=order.filled_price, + commission=order.commission, + message=f"Order {order_id} filled" + ) + # Store pending order order.status = OrderStatus.OPEN self._orders[order_id] = order @@ -253,13 +302,19 @@ class PaperBroker(BaseBroker): new_entry = (existing.entry_price * existing.size + fill_price * order.size) / new_size existing.size = new_size existing.entry_price = new_entry + existing.entry_commission += order.commission # Accumulate entry fees + # Most recent fill wins: update price source + if order.exchange: + existing.price_source = order.exchange else: self._positions[order.symbol] = Position( symbol=order.symbol, size=order.size, entry_price=fill_price, current_price=fill_price, - unrealized_pnl=0.0 + unrealized_pnl=-order.commission, # Start with entry fee as loss + entry_commission=order.commission, + price_source=order.exchange ) # Record SL/TP for this position (if set on order) @@ -280,10 +335,22 @@ class PaperBroker(BaseBroker): if order.symbol in self._positions: position = self._positions[order.symbol] order.entry_price = position.entry_price # Store for fee calculation - realized_pnl = (fill_price - position.entry_price) * order.size - order.commission + + # Calculate proportional entry commission for partial closes + if position.size > 0: + proportion = order.size / position.size + proportional_entry_commission = position.entry_commission * proportion + else: + proportional_entry_commission = position.entry_commission + + # Realized P&L includes both entry and exit fees + realized_pnl = (fill_price - position.entry_price) * order.size - proportional_entry_commission - order.commission position.realized_pnl += realized_pnl position.size -= order.size + # Reduce remaining entry commission proportionally + position.entry_commission -= proportional_entry_commission + # Track profitability for fee calculation order.realized_pnl = realized_pnl order.is_profitable = realized_pnl > 0 @@ -363,15 +430,30 @@ class PaperBroker(BaseBroker): """Get all open positions.""" return list(self._positions.values()) - def get_current_price(self, symbol: str) -> float: - """Get current price for a symbol.""" - # First check cache + def get_current_price(self, symbol: str, exchange: Optional[str] = None) -> float: + """Get price, preferring exchange-qualified if available.""" + # First try exchange-qualified lookup + if exchange: + key = f"{exchange}:{symbol}" + if key in self._current_prices: + return self._current_prices[key] + + # Fall back to symbol-only lookup if symbol in self._current_prices: return self._current_prices[symbol] # Then try price provider if self._price_provider: try: + # Try exchange-qualified first, then symbol-only + if exchange: + try: + price = self._price_provider(f"{exchange}:{symbol}") + if price > 0: + self._current_prices[f"{exchange}:{symbol}"] = price + return price + except Exception: + pass price = self._price_provider(symbol) self._current_prices[symbol] = price return price @@ -388,12 +470,14 @@ class PaperBroker(BaseBroker): """ events = [] - # Update position P&L + # Update position P&L (includes entry commission for accurate fee reflection) for symbol, position in self._positions.items(): - current_price = self.get_current_price(symbol) + # Use position's price_source for exchange-aware price lookup + current_price = self.get_current_price(symbol, position.price_source) if current_price > 0: position.current_price = current_price - position.unrealized_pnl = (current_price - position.entry_price) * position.size + # P&L = price movement - entry fees already paid + position.unrealized_pnl = (current_price - position.entry_price) * position.size - position.entry_commission # Evaluate SL/TP for all tracked positions for symbol, sltp in list(self._position_sltp.items()): @@ -402,7 +486,8 @@ class PaperBroker(BaseBroker): continue position = self._positions[symbol] - current_price = self.get_current_price(symbol) + # Use position's price_source for exchange-aware price lookup + current_price = self.get_current_price(symbol, position.price_source) if position.size <= 0 or current_price <= 0: del self._position_sltp[symbol] @@ -444,17 +529,15 @@ class PaperBroker(BaseBroker): if order.status != OrderStatus.OPEN: continue - current_price = self.get_current_price(order.symbol) + # Use order's exchange for price lookup + current_price = self.get_current_price(order.symbol, order.exchange) if current_price <= 0: continue - should_fill = False - if order.order_type == OrderType.LIMIT: - if order.side == OrderSide.BUY and current_price <= order.price: - should_fill = True - elif order.side == OrderSide.SELL and current_price >= order.price: - should_fill = True + should_fill = self._should_fill_limit_order(order.side, current_price, order.price) + else: + should_fill = False if should_fill: # Release locked funds first (for buy orders) @@ -497,6 +580,7 @@ class PaperBroker(BaseBroker): self._positions.clear() self._trade_history.clear() self._current_prices.clear() + self._position_sltp.clear() # Clear SL/TP state to prevent stale triggers logger.info(f"PaperBroker: Reset with balance {self.initial_balance}") # ==================== State Persistence Methods ==================== @@ -609,6 +693,8 @@ class PaperBroker(BaseBroker): price=order_dict.get('price'), stop_loss=order_dict.get('stop_loss'), take_profit=order_dict.get('take_profit'), + time_in_force=order_dict.get('time_in_force', 'GTC'), + exchange=order_dict.get('exchange'), ) order.status = OrderStatus(order_dict['status']) order.filled_qty = order_dict.get('filled_qty', 0.0) diff --git a/src/candles.py b/src/candles.py index 9f00ae0..5ec4498 100644 --- a/src/candles.py +++ b/src/candles.py @@ -1,12 +1,11 @@ import datetime as dt -import logging as log +import logging import pytz from shared_utilities import timeframe_to_minutes, ts_of_n_minutes_ago - -# log.basicConfig(level=log.ERROR) +logger = logging.getLogger(__name__) class Candles: def __init__(self, exchanges, users, datacache, config, edm_client=None): @@ -24,6 +23,8 @@ class Candles: # Cache the last received candle to detect duplicates self.cached_last_candle = None + # Avoid repeating the same expected EDM cap warning on every refresh. + self._edm_cap_warned_scopes: set[tuple[str, str, str]] = set() # size_limit is the max number of lists of candle(ohlc) data allowed. self.data.create_cache(name='candles', cache_type='row', default_expiration=dt.timedelta(days=5), @@ -57,10 +58,21 @@ class Candles: # EDM API has a maximum limit of 1000 candles EDM_MAX_CANDLES = 1000 if num_candles > EDM_MAX_CANDLES: - log.warning(f'Requested {num_candles} candles, capping to EDM limit of {EDM_MAX_CANDLES}') + warning_scope = (asset, exchange, timeframe) + if warning_scope not in self._edm_cap_warned_scopes: + logger.warning( + "Requested %s candles for %s/%s/%s, capping to EDM limit of %s", + num_candles, asset, timeframe, exchange, EDM_MAX_CANDLES + ) + self._edm_cap_warned_scopes.add(warning_scope) + else: + logger.debug( + "Requested %s candles for %s/%s/%s, capped to %s", + num_candles, asset, timeframe, exchange, EDM_MAX_CANDLES + ) num_candles = EDM_MAX_CANDLES - log.info(f'[GET CANDLES] {asset} {exchange} {timeframe} limit={num_candles}') + logger.debug("Fetching candles from EDM: %s %s %s limit=%s", asset, exchange, timeframe, num_candles) if self.edm is None: raise RuntimeError("EDM client not initialized. Cannot fetch candle data.") @@ -76,10 +88,10 @@ class Candles: ) if candles.empty: - log.warning(f"No candles returned from EDM for {asset}/{timeframe}/{exchange}") + logger.warning("No candles returned from EDM for %s/%s/%s", asset, timeframe, exchange) return self.convert_candles(candles) - log.info(f"Fetched {len(candles)} candles from EDM for {asset}/{timeframe}/{exchange}") + logger.debug("Fetched %s candles from EDM for %s/%s/%s", len(candles), asset, timeframe, exchange) return self.convert_candles(candles[-num_candles:]) def set_new_candle(self, cdata: dict) -> bool: @@ -93,7 +105,7 @@ class Candles: """ # Update the cached last candle self.cached_last_candle = cdata - log.debug(f"Candle updated: {cdata.get('symbol', 'unknown')} @ {cdata.get('close', 0)}") + logger.debug("Candle updated: %s @ %s", cdata.get('symbol', 'unknown'), cdata.get('close', 0)) return True def set_cache(self, symbol=None, interval=None, exchange_name=None, user_name=None): """ @@ -110,24 +122,24 @@ class Candles: if not symbol: assert user_name is not None symbol = self.users.get_chart_view(user_name=user_name, prop='market') - log.info(f'set_candle_history(): No symbol provided. Using{symbol}') + logger.info('set_candle_history(): No symbol provided. Using %s', symbol) if not interval: assert user_name is not None interval = self.users.get_chart_view(user_name=user_name, prop='timeframe') - log.info(f'set_candle_history(): No timeframe provided. Using{interval}') + logger.info('set_candle_history(): No timeframe provided. Using %s', interval) if not exchange_name: assert user_name is not None exchange_name = self.users.get_chart_view(user_name=user_name, prop='exchange_name') # Log the completion to the console. - log.info('set_candle_history(): Loading candle data...') + logger.info('set_candle_history(): Loading candle data...') # Load candles from database _cdata = self.get_last_n_candles(num_candles=self.max_records, asset=symbol, timeframe=interval, exchange=exchange_name, user_name=user_name) # Log the completion to the console. - log.info('set_candle_history(): Candle data Loaded.') + logger.info('set_candle_history(): Candle data Loaded.') return @staticmethod @@ -213,17 +225,17 @@ class Candles: if not symbol: assert user_name is not None symbol = self.users.get_chart_view(user_name=user_name, prop='market') - log.info(f'get_candle_history(): No symbol provided. Using {symbol}') + logger.info('get_candle_history(): No symbol provided. Using %s', symbol) if not interval: assert user_name is not None interval = self.users.get_chart_view(user_name=user_name, prop='timeframe') - log.info(f'get_candle_history(): No timeframe provided. Using {interval}') + logger.info('get_candle_history(): No timeframe provided. Using %s', interval) if not exchange_name: assert user_name is not None exchange_name = self.users.get_chart_view(user_name=user_name, prop='exchange_name') - log.info(f'get_candle_history(): No exchange name provided. Using {exchange_name}') + logger.info('get_candle_history(): No exchange name provided. Using %s', exchange_name) candlesticks = self.get_last_n_candles(num_candles=num_records, asset=symbol, timeframe=interval, exchange=exchange_name, user_name=user_name) diff --git a/src/manual_trading_broker.py b/src/manual_trading_broker.py index 7aa58a1..6373473 100644 --- a/src/manual_trading_broker.py +++ b/src/manual_trading_broker.py @@ -101,6 +101,10 @@ class ManualTradingBrokerManager: :param user_name: Username for exchange lookup. :return: LiveBroker instance or None if not configured or mode conflict. """ + if not exchange_name: + logger.error("Cannot create live broker: exchange_name is required") + return None + # Use 'testnet'/'production' to match what's stored in trade.broker_mode requested_mode = 'testnet' if testnet else 'production' broker_key = f"{exchange_name}_{requested_mode}" @@ -213,16 +217,17 @@ class ManualTradingBrokerManager: """ Update current prices for all paper brokers. - :param price_updates: Dict mapping symbol to price. + :param price_updates: Dict mapping symbol or exchange:symbol to price. """ for broker in self._paper_brokers.values(): - for symbol, price in price_updates.items(): - # Handle exchange:symbol format - if ':' in symbol: - _, sym = symbol.split(':', 1) + for key, price in price_updates.items(): + if ':' in key: + # Exchange-qualified key (e.g., 'kucoin:BTC/USDT') + exchange, symbol = key.split(':', 1) + broker.update_price(symbol, price, exchange) else: - sym = symbol - broker.update_price(sym, price) + # Symbol-only key + broker.update_price(key, price) def update_all_brokers(self, price_updates: Dict[str, float]) -> List[Dict]: """ @@ -394,6 +399,7 @@ class ManualTradingBrokerManager: "success": result.success, "message": result.message, "order_id": result.order_id, + "status": getattr(result.status, 'value', result.status), "filled_qty": result.filled_qty, "filled_price": result.filled_price } @@ -466,16 +472,22 @@ class ManualTradingBrokerManager: logger.error(f"Error placing order: {e}") return OrderResult(success=False, message=str(e)) - def _get_broker(self, user_id: int, broker_key: str): + def _get_broker(self, user_id: int, broker_key: str, create_paper: bool = True): """ Get a broker by user_id and broker_key. :param user_id: The user ID. :param broker_key: 'paper' or 'exchange_mode' format. + :param create_paper: If True, create paper broker on-demand (loads saved state). :return: Broker instance or None. """ if broker_key == 'paper': - return self._paper_brokers.get(user_id) + if user_id in self._paper_brokers: + return self._paper_brokers[user_id] + # Create paper broker on-demand (this loads saved state) + if create_paper: + return self.get_paper_broker(user_id) + return None if user_id in self._live_brokers: return self._live_brokers[user_id].get(broker_key) @@ -495,6 +507,37 @@ class ManualTradingBrokerManager: return 0.0 return broker.get_balance() + def reset_paper_broker(self, user_id: int) -> Dict: + """ + Reset the paper broker for a user to initial state. + + Clears all positions, orders, and restores the initial balance. + + :param user_id: The user ID. + :return: Dict with success status and new balance. + """ + broker = self._paper_brokers.get(user_id) + if not broker: + # Create a fresh broker if one doesn't exist + broker = self.get_paper_broker(user_id) + + try: + broker.reset() + + # Save the reset state + state_id = f"manual_paper_{user_id}" + broker.save_state(state_id) + + logger.info(f"Reset paper broker for user {user_id}, balance: {broker.initial_balance}") + return { + "success": True, + "message": "Paper trading balance reset successfully", + "balance": broker.initial_balance + } + except Exception as e: + logger.error(f"Error resetting paper broker for user {user_id}: {e}") + return {"success": False, "message": str(e)} + def get_available_balance(self, user_id: int, broker_key: str) -> float: """ Get the available balance (not locked in orders). @@ -574,3 +617,36 @@ class ManualTradingBrokerManager: logger.error(f"Error recovering broker {broker_key} for user {user_id}: {e}") return recovered + + def get_active_paper_user_ids(self) -> List[int]: + """Return user IDs with active paper brokers.""" + return list(self._paper_brokers.keys()) + + def get_required_price_feeds(self, user_id: int) -> List[tuple]: + """ + Get (exchange, symbol) pairs needed for P&L updates. + + Derived from positions and open orders, NOT from Trade.exchange. + Returns list of (exchange, symbol) tuples where exchange may be None + for positions/orders without a specified price source. + + :param user_id: The user ID. + :return: List of (exchange, symbol) tuples. + """ + feeds = set() + broker = self._paper_brokers.get(user_id) + if broker: + # From positions + for pos in broker.get_all_positions(): + if pos.price_source: + feeds.add((pos.price_source, pos.symbol)) + else: + # Fallback: no exchange specified, use symbol only + feeds.add((None, pos.symbol)) + # From open orders + for order in broker.get_open_orders(): + exchange = order.get('exchange') + symbol = order.get('symbol') + if symbol: + feeds.add((exchange, symbol)) + return list(feeds) diff --git a/src/static/charts.js b/src/static/charts.js index 3443fc6..485f026 100644 --- a/src/static/charts.js +++ b/src/static/charts.js @@ -36,6 +36,7 @@ class Charts { this.candleSeries = this.chart_1.addSeries(LightweightCharts.CandlestickSeries); // Initialize the candlestick series if price_history is available + this.price_history = this._normalizeCandles(this.price_history); if (this.price_history && this.price_history.length > 0) { this.candleSeries.setData(this.price_history); console.log(`Candle series initialized with ${this.price_history.length} candles`); @@ -48,8 +49,110 @@ class Charts { } update_main_chart(new_candle){ + const normalizedCandle = this._normalizeCandle(new_candle); + if (!normalizedCandle) { + console.warn('Skipping invalid candle update:', new_candle); + return; + } + + const lastCandle = Array.isArray(this.price_history) && this.price_history.length > 0 + ? this.price_history[this.price_history.length - 1] + : null; + if (lastCandle && normalizedCandle.time < lastCandle.time) { + console.warn('Skipping stale candle update:', normalizedCandle, 'last:', lastCandle); + return; + } + // Update candlestick series - this.candleSeries.update(new_candle); + this.candleSeries.update(normalizedCandle); + + // Keep local price history aligned with the live chart series. + if (!Array.isArray(this.price_history)) { + this.price_history = []; + } + + const lastIndex = this.price_history.length - 1; + if (lastIndex >= 0 && this.price_history[lastIndex].time === normalizedCandle.time) { + this.price_history[lastIndex] = normalizedCandle; + } else if (lastIndex < 0 || this.price_history[lastIndex].time < normalizedCandle.time) { + this.price_history.push(normalizedCandle); + } + } + + _normalizeCandleTime(rawTime) { + if (rawTime === null || rawTime === undefined) { + return null; + } + + if (typeof rawTime === 'number' && Number.isFinite(rawTime)) { + return rawTime > 1e12 ? Math.floor(rawTime / 1000) : Math.floor(rawTime); + } + + if (typeof rawTime === 'string') { + const numericValue = Number(rawTime); + if (Number.isFinite(numericValue)) { + return this._normalizeCandleTime(numericValue); + } + + const parsedTime = Date.parse(rawTime); + if (!Number.isNaN(parsedTime)) { + return Math.floor(parsedTime / 1000); + } + return null; + } + + if (rawTime instanceof Date) { + return Math.floor(rawTime.getTime() / 1000); + } + + if (typeof rawTime === 'object') { + if ( + Object.prototype.hasOwnProperty.call(rawTime, 'year') && + Object.prototype.hasOwnProperty.call(rawTime, 'month') && + Object.prototype.hasOwnProperty.call(rawTime, 'day') + ) { + return Math.floor(Date.UTC(rawTime.year, rawTime.month - 1, rawTime.day) / 1000); + } + + for (const key of ['timestamp', 'time', 'value', '$date']) { + if (Object.prototype.hasOwnProperty.call(rawTime, key)) { + return this._normalizeCandleTime(rawTime[key]); + } + } + } + + return null; + } + + _normalizeCandle(candle) { + if (!candle) { + return null; + } + + const time = this._normalizeCandleTime(candle.time); + if (time === null) { + return null; + } + + return { + ...candle, + time, + open: parseFloat(candle.open), + high: parseFloat(candle.high), + low: parseFloat(candle.low), + close: parseFloat(candle.close) + }; + } + + _normalizeCandles(candles) { + if (!Array.isArray(candles)) { + return []; + } + + return candles + .map(candle => this._normalizeCandle(candle)) + .filter(candle => candle !== null) + .sort((a, b) => a.time - b.time); } create_RSI_chart(){ diff --git a/src/static/communication.js b/src/static/communication.js index 0a4dd6a..953b716 100644 --- a/src/static/communication.js +++ b/src/static/communication.js @@ -216,6 +216,57 @@ class Comms { } } + /** + * Normalize incoming candle times to UTC seconds for lightweight-charts. + * EDM data occasionally arrives nested or as ISO strings. + * @param {*} rawTime + * @returns {number|null} + */ + _normalizeCandleTime(rawTime) { + if (rawTime === null || rawTime === undefined) { + return null; + } + + if (typeof rawTime === 'number' && Number.isFinite(rawTime)) { + return rawTime > 1e12 ? Math.floor(rawTime / 1000) : Math.floor(rawTime); + } + + if (typeof rawTime === 'string') { + const numericValue = Number(rawTime); + if (Number.isFinite(numericValue)) { + return this._normalizeCandleTime(numericValue); + } + + const parsedTime = Date.parse(rawTime); + if (!Number.isNaN(parsedTime)) { + return Math.floor(parsedTime / 1000); + } + return null; + } + + if (rawTime instanceof Date) { + return Math.floor(rawTime.getTime() / 1000); + } + + if (typeof rawTime === 'object') { + if ( + Object.prototype.hasOwnProperty.call(rawTime, 'year') && + Object.prototype.hasOwnProperty.call(rawTime, 'month') && + Object.prototype.hasOwnProperty.call(rawTime, 'day') + ) { + return Math.floor(Date.UTC(rawTime.year, rawTime.month - 1, rawTime.day) / 1000); + } + + for (const key of ['timestamp', 'time', 'value', '$date']) { + if (Object.prototype.hasOwnProperty.call(rawTime, key)) { + return this._normalizeCandleTime(rawTime[key]); + } + } + } + + return null; + } + /* Callback declarations */ candleUpdate(newCandle) { @@ -345,14 +396,17 @@ class Comms { const candles = data.candles || []; // EDM already sends time in seconds, no conversion needed - return candles.map(c => ({ - time: c.time, - open: c.open, - high: c.high, - low: c.low, - close: c.close, - volume: c.volume - })); + return candles + .map(c => ({ + time: this._normalizeCandleTime(c.time), + open: parseFloat(c.open), + high: parseFloat(c.high), + low: parseFloat(c.low), + close: parseFloat(c.close), + volume: parseFloat(c.volume) + })) + .filter(c => c.time !== null) + .sort((a, b) => a.time - b.time); } /** @@ -571,8 +625,13 @@ class Comms { if (messageType === 'candle') { const candle = message.data; + const candleTime = this._normalizeCandleTime(candle.time); + if (candleTime === null) { + console.warn('Skipping candle with invalid time payload:', candle.time); + return; + } const newCandle = { - time: candle.time, // EDM sends time in seconds + time: candleTime, open: parseFloat(candle.open), high: parseFloat(candle.high), low: parseFloat(candle.low), diff --git a/src/static/trade.js b/src/static/trade.js index 8638b5f..cb5be5c 100644 --- a/src/static/trade.js +++ b/src/static/trade.js @@ -23,17 +23,18 @@ class TradeUIManager { this.sltpRow = null; this.onCloseTrade = null; - // Exchanges known to support testnet/sandbox mode + // Exchanges known to support testnet/sandbox mode in ccxt + // IMPORTANT: Only include exchanges with verified working sandbox URLs + // KuCoin does NOT have sandbox support - removed to prevent real trades! this.testnetSupportedExchanges = [ 'binance', 'binanceus', 'binanceusdm', 'binancecoinm', - 'kucoin', 'kucoinfutures', 'bybit', 'okx', 'okex', 'bitget', 'bitmex', 'deribit', - 'phemex', - 'mexc' + 'phemex' + // Removed: 'kucoin', 'kucoinfutures', 'mexc' - no sandbox support ]; } @@ -141,6 +142,7 @@ class TradeUIManager { this._updateTestnetVisibility(); this._updateExchangeRowVisibility(); this._updateSellAvailability(); + this._updateSltpVisibility(); }); } @@ -332,23 +334,23 @@ class TradeUIManager { } /** - * Updates SL/TP row visibility based on order side. - * SL/TP only applies to BUY orders (opening positions). - * SELL orders close existing positions, so SL/TP is not applicable. + * Updates SL/TP row visibility based on supported mode and side. + * Manual SL/TP is currently supported for paper BUY orders only. */ _updateSltpVisibility() { - if (!this.sltpRow || !this.sideSelect) return; + if (!this.sltpRow || !this.sideSelect || !this.targetSelect) return; const side = this.sideSelect.value.toLowerCase(); + const isPaperTrade = this.targetSelect.value === 'test_exchange'; - if (side === 'sell') { - // Hide SL/TP for SELL (closing positions) + if (!isPaperTrade || side === 'sell') { + // Hide SL/TP when unsupported or not applicable. this.sltpRow.style.display = 'none'; - // Clear any values + // Clear values to avoid submitting stale unsupported inputs. if (this.stopLossInput) this.stopLossInput.value = ''; if (this.takeProfitInput) this.takeProfitInput.value = ''; } else { - // Show SL/TP for BUY (opening positions) + // Show SL/TP for paper BUY orders. this.sltpRow.style.display = 'contents'; } } @@ -473,18 +475,16 @@ class TradeUIManager { if (this.testnetCheckbox) { this.testnetCheckbox.checked = true; } - // Reset side to BUY and show SL/TP row + // Reset side to BUY if (this.sideSelect) { this.sideSelect.value = 'buy'; } - if (this.sltpRow) { - this.sltpRow.style.display = 'contents'; - } this.formElement.style.display = 'grid'; // Update SELL availability based on current broker/symbol await this._updateSellAvailability(); + this._updateSltpVisibility(); } /** @@ -737,6 +737,22 @@ class TradeUIManager { this.onCloseTrade = callback; } + /** + * Sets the callback function for full refresh (trades + statistics). + * @param {Function} callback - The callback function. + */ + registerRefreshCallback(callback) { + this.onRefresh = callback; + } + + /** + * Sets the callback function for position-close updates. + * @param {Function} callback - The callback function. + */ + registerPositionClosedCallback(callback) { + this.onPositionClosed = callback; + } + // ============ Broker Event Listeners ============ /** @@ -759,7 +775,11 @@ class TradeUIManager { comms.on('position_closed', (data) => { console.log('Position closed:', data); - this.refreshAll(); + if (this.onPositionClosed) { + this.onPositionClosed(data); + } else { + this.refreshAll(); + } }); comms.on('sltp_triggered', (data) => { @@ -834,12 +854,15 @@ class TradeUIManager { container.innerHTML = ''; - if (!positions || positions.length === 0) { + // Filter out closed positions (size <= 0) + const openPositions = (positions || []).filter(pos => pos.size && Math.abs(pos.size) > 0); + + if (openPositions.length === 0) { container.innerHTML = '
No open positions
'; return; } - for (const pos of positions) { + for (const pos of openPositions) { const card = this._createPositionCard(pos); container.appendChild(card); } @@ -853,6 +876,10 @@ class TradeUIManager { const plClass = pl >= 0 ? 'positive' : 'negative'; const plSign = pl >= 0 ? '+' : ''; + // Price source for tooltip (shows which exchange's prices are used for P&L) + const priceSource = position.price_source || 'default'; + card.title = `P&L uses ${priceSource} prices`; + card.innerHTML = `