def getsymbolpositionsandorders(symbol, side): try: Format the symbol for Phemex…
- User
def get_symbol_positions_and_orders(symbol, side): try: # Format the symbol for Phemex formatted_symbol = format_symbol_for_phemex(symbol)
# Normalize the side parameter normalized_side = side.lower() if normalized_side in ['l', 'long']: position_side = 'long' order_side = 'buy' elif normalized_side in ['s', 'short']: position_side = 'short' order_side = 'sell' else: raise ValueError("Invalid side parameter. Must be 'L', 'Long', 'S', or 'Short' (case-insensitive).") # Fetch positions all_positions = get_open_positions([formatted_symbol]) positions = [pos for pos in all_positions if pos['side'].lower() == position_side] # Fetch open orders open_orders = exchange.fetch_open_orders(formatted_symbol) # Filter orders based on the specified side and exclude stop orders (case-insensitive) filtered_orders = [ order for order in open_orders if order['side'].lower() == order_side and order['type'].lower() not in ['stop', 'stop_limit', 'stop-market', 'stoplimit'] ] # Format the positions formatted_positions = [] for pos in positions: formatted_pos = { 'symbol': pos['symbol'], 'side': pos['side'], 'amount': float(pos['contracts']), 'entryPrice': float(pos['entryPrice']), 'unrealizedPnl': float(pos['unrealizedPnl']) if pos['unrealizedPnl'] is not None else None, 'leverage': float(pos['leverage']), 'markPrice': float(pos['markPrice']), 'liquidationPrice': float(pos['liquidationPrice']), 'margin': float(pos['initialMargin']), 'marginType': pos['marginMode'], 'timestamp': pos['timestamp'], 'id': pos['id'], 'orderId': pos['info'].get('orderId'), 'clientOrderId': pos['info'].get('clientOrderId'), 'positionId': pos['info'].get('positionId'), 'stopLoss': get_position_stop_loss(pos) } formatted_positions.append(formatted_pos) # Format the orders formatted_orders = [] for order in filtered_orders: stop_loss_info = get_stop_loss_info(order['clientOrderId']) formatted_order = { 'id': order['id'], 'clientOrderId': order['clientOrderId'], 'symbol': order['symbol'], 'type': order['type'], 'side': order['side'], 'amount': float(order['amount']) if order['amount'] is not None else None, 'price': float(order['price']) if order['price'] is not None else None, 'status': order['status'], 'timestamp': order['timestamp'], 'storedStopLossPrice': stop_loss_info['stop_loss_price'] if stop_loss_info else None } formatted_orders.append(formatted_order) return { 'status': 'success', 'symbol': symbol, 'side': position_side, 'positions': formatted_positions, 'orders': formatted_orders } except ValueError as e: logging.error(f"Invalid input: {str(e)}") return {'status': 'error', 'message': str(e)} except ccxt.NetworkError as e: logging.error(f"Network error while fetching data for {symbol}: {str(e)}") return {'status': 'error', 'message': f"Network error: {str(e)}"} except ccxt.ExchangeError as e: logging.error(f"Exchange error while fetching data for {symbol}: {str(e)}") return {'status': 'error', 'message': f"Exchange error: {str(e)}"} except Exception as e: logging.error(f"Unexpected error while fetching data for {symbol}: {str(e)}") return {'status': 'error', 'message': f"Unexpected error: {str(e)}"} - Assistant