execute_hedge_arbitrage_strategy
Execute hedge arbitrage on Binance by leveraging funding rate differences for specified trading pairs and quantities, returning detailed arbitrage results.
Instructions
Execute hedge arbitrage based on funding rate.
Args: symbol: The trading pair. quantity: Amount to trade.
Returns: Summary of the arbitrage result.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| quantity | Yes | ||
| symbol | Yes |
Implementation Reference
- binance.py:225-279 (handler)The core handler function for the 'execute_hedge_arbitrage_strategy' tool, registered via @mcp.tool() decorator. It performs hedge arbitrage by opening positions on spot and futures markets based on funding rate direction, holding briefly, then closing, and computes estimated net profit after fees.@mcp.tool() def execute_hedge_arbitrage_strategy(symbol: str, quantity: str) -> Any: """ Execute hedge arbitrage based on funding rate. Args: symbol: The trading pair. quantity: Amount to trade. Returns: Summary of the arbitrage result. """ asset = symbol.replace("USDT", "") balance_result = mcp.use_tool("get_account_balance", asset) try: available_balance = float(balance_result.get("balance", 0)) except: available_balance = 0 actual_quantity = min(float(quantity), available_balance) if available_balance > 0 else 0 if actual_quantity <= 0: return {"error": f"Insufficient {asset} balance."} funding_rate_data = mcp.use_tool("get_funding_rate_history", symbol) funding_rate = float(funding_rate_data[0]['fundingRate']) spot_price_data = mcp.use_tool("get_symbol_price", symbol) spot_price = float(spot_price_data["price"]) if funding_rate > 0: mcp.use_tool("place_market_order", symbol, "BUY", actual_quantity) place_futures_order(symbol, "SELL", actual_quantity) else: mcp.use_tool("place_market_order", symbol, "SELL", actual_quantity) place_futures_order(symbol, "BUY", actual_quantity) time.sleep(10) if funding_rate > 0: place_futures_order(symbol, "BUY", actual_quantity) mcp.use_tool("place_market_order", symbol, "SELL", actual_quantity) else: place_futures_order(symbol, "SELL", actual_quantity) mcp.use_tool("place_market_order", symbol, "BUY", actual_quantity) SPOT_FEE = 0.001 FUTURES_FEE = 0.0002 fee = (spot_price * actual_quantity * SPOT_FEE * 2) + (spot_price * actual_quantity * FUTURES_FEE * 2) profit = abs(funding_rate) * spot_price * actual_quantity net_profit = profit - fee return { "net_profit": round(net_profit, 4), "fees": round(fee, 4), "message": f"Arbitrage executed. Estimated net profit: {net_profit:.4f} USDT" }
- binance.py:282-308 (helper)Helper function used by the arbitrage strategy to place orders on Binance futures/perpetual contracts.def place_futures_order(symbol: str, side: str, quantity: str) -> Any: """ Place a perpetual futures market order. Args: symbol: Futures pair. side: BUY or SELL. quantity: Amount. Returns: Order placement result. """ url = "https://fapi.binance.com/fapi/v1/order" timestamp = int(time.time() * 1000) params = { "symbol": symbol, "side": side, "type": "MARKET", "quantity": quantity, "timestamp": timestamp } query_string = "&".join([f"{k}={v}" for k, v in params.items()]) signature = hmac.new(BINANCE_SECRET_KEY.encode(), query_string.encode(), hashlib.sha256).hexdigest() params["signature"] = signature headers = {"X-MBX-APIKEY": BINANCE_API_KEY} response = requests.post(url, headers=headers, params=params) return response.json() if response.status_code == 200 else {"error": response.text}