Test Binance Futures REST API client.
()
| 219 | |
| 220 | |
| 221 | def test_futures_client(): |
| 222 | """Test Binance Futures REST API client.""" |
| 223 | print("=" * 60) |
| 224 | print("Testing Binance Futures REST API Client") |
| 225 | print("=" * 60) |
| 226 | |
| 227 | # Get API credentials from environment |
| 228 | api_key = os.getenv("BINANCE_TESTNET_TRADING_API_KEY", "") |
| 229 | api_secret = os.getenv("BINANCE_TESTNET_TRADING_SECRET_KEY", "") |
| 230 | testnet = True |
| 231 | |
| 232 | if not api_key or not api_secret: |
| 233 | print("⚠️ BINANCE_TESTNET_TRADING_API_KEY and BINANCE_TESTNET_TRADING_SECRET_KEY not found in environment variables") |
| 234 | print(" Skipping Futures Client tests...\n") |
| 235 | return |
| 236 | |
| 237 | try: |
| 238 | # Initialize Futures client |
| 239 | futures_client = BinanceFuturesClient( |
| 240 | api_key=api_key, |
| 241 | api_secret=api_secret, |
| 242 | testnet=testnet |
| 243 | ) |
| 244 | |
| 245 | print(f"✅ Futures Client initialized (testnet={testnet})") |
| 246 | print(f" Base URL: {futures_client.base_url}\n") |
| 247 | |
| 248 | # Test 1: Get account info (authentication required) |
| 249 | print("Test 1: Getting futures account info (authenticated endpoint)...") |
| 250 | try: |
| 251 | account_info = futures_client.get_account() |
| 252 | print(f"✅ Futures account info retrieved successfully") |
| 253 | print(f" Total wallet balance: {account_info.get('totalWalletBalance')}") |
| 254 | print(f" Available balance: {account_info.get('availableBalance')}") |
| 255 | print(f" Total unrealized profit: {account_info.get('totalUnrealizedProfit')}") |
| 256 | assets = account_info.get('assets', []) |
| 257 | non_zero_assets = [a for a in assets if float(a.get('walletBalance', 0)) > 0] |
| 258 | print(f" Non-zero assets: {len(non_zero_assets)}") |
| 259 | if non_zero_assets: |
| 260 | for asset in non_zero_assets[:3]: # Show first 3 |
| 261 | asset_name = asset.get('asset') |
| 262 | wallet_balance = asset.get('walletBalance') |
| 263 | print(f" {asset_name}: walletBalance={wallet_balance}") |
| 264 | except Exception as e: |
| 265 | print(f"❌ Failed to get futures account info: {e}") |
| 266 | if "401" in str(e) or "Invalid" in str(e): |
| 267 | print(" This might be due to:") |
| 268 | print(" - Invalid API key or secret key") |
| 269 | print(" - API key not enabled for Futures trading") |
| 270 | print(" - IP whitelist restrictions") |
| 271 | |
| 272 | print() |
| 273 | |
| 274 | # Test 2: Get position risk |
| 275 | print("Test 2: Getting position risk (authenticated endpoint)...") |
| 276 | try: |
| 277 | positions = futures_client.get_position_risk() |
| 278 | print(f"✅ Position risk retrieved successfully") |
no test coverage detected