Spaces:
Paused
Paused
| #!/usr/bin/env python3 | |
| import os | |
| from dotenv import load_dotenv | |
| from pybit.unified_trading import HTTP | |
| load_dotenv() | |
| def test_public_endpoints(): | |
| print("Testing public API endpoints...") | |
| try: | |
| session = HTTP(testnet=True) | |
| server_time = session.get_server_time() | |
| print(f"β Server time: {server_time['result']['timeSecond']}") | |
| ticker = session.get_tickers(category="linear", symbol="BTCUSDT") | |
| if ticker['result']['list']: | |
| price = ticker['result']['list'][0]['lastPrice'] | |
| print(f"β BTCUSDT price: ${price}") | |
| kline = session.get_kline( | |
| category="linear", | |
| symbol="BTCUSDT", | |
| interval="1", | |
| limit=1 | |
| ) | |
| if kline['result']['list']: | |
| print("β Kline data retrieved successfully") | |
| instruments = session.get_instruments_info(category="linear", symbol="BTCUSDT") | |
| if instruments['result']['list']: | |
| print("β Instrument info retrieved successfully") | |
| return True | |
| except Exception as e: | |
| print(f"β Public API test failed: {e}") | |
| return False | |
| def test_private_endpoints(): | |
| print("\nTesting private API endpoints...") | |
| API_KEY = os.getenv("BYBIT_API_KEY") | |
| API_SECRET = os.getenv("BYBIT_API_SECRET") | |
| TESTNET = os.getenv("BYBIT_TESTNET", "true").lower() == "true" | |
| print(f"API Key configured: {'Yes' if API_KEY else 'No'}") | |
| print(f"API Secret configured: {'Yes' if API_SECRET else 'No'}") | |
| print(f"Using {'TESTNET' if TESTNET else 'MAINNET'}") | |
| if not API_KEY or not API_SECRET: | |
| print("β ERROR: API credentials not found in .env file") | |
| print("Please set BYBIT_API_KEY and BYBIT_API_SECRET in your .env file") | |
| return False | |
| if API_KEY == "your_api_key_here" or API_SECRET == "your_api_secret_here": | |
| print("β ERROR: API credentials are still set to default placeholder values") | |
| print("Please update .env with your actual Bybit API credentials") | |
| return False | |
| try: | |
| session = HTTP( | |
| api_key=API_KEY, | |
| api_secret=API_SECRET, | |
| testnet=TESTNET | |
| ) | |
| print("β HTTP client initialized with API credentials") | |
| balance_success = False | |
| positions_success = False | |
| orders_success = False | |
| try: | |
| balance = session.get_wallet_balance(accountType="UNIFIED") | |
| print(f"β Account balance retrieved: {len(balance['result']['list'])} coins") | |
| for coin in balance['result']['list']: | |
| if coin['coin'] == 'USDT': | |
| print(f" USDT Balance: {coin.get('walletBalance', '0')}") | |
| balance_success = True | |
| except Exception as e: | |
| print(f"β Balance check failed: {e}") | |
| print(" Trying CONTRACT account type...") | |
| try: | |
| balance = session.get_wallet_balance(accountType="CONTRACT") | |
| print(f"β Contract account balance retrieved: {len(balance['result']['list'])} coins") | |
| balance_success = True | |
| except Exception as e2: | |
| print(f"β Contract balance also failed: {e2}") | |
| print(" Common causes:") | |
| print(" - API key lacks balance viewing permissions") | |
| print(" - Invalid API credentials") | |
| print(" - Account not funded on testnet") | |
| print(" - Check API key permissions in Bybit dashboard") | |
| try: | |
| positions = session.get_positions(category="linear") | |
| print(f"β Positions retrieved: {len(positions['result']['list'])} positions") | |
| positions_success = True | |
| except Exception as e: | |
| print(f"β Positions check failed: {e}") | |
| print(" This requires 'View Positions' permission") | |
| try: | |
| orders = session.get_open_orders(category="linear") | |
| print(f"β Open orders retrieved: {len(orders['result']['list'])} orders") | |
| orders_success = True | |
| except Exception as e: | |
| print(f"β Orders check failed: {e}") | |
| print(" This requires 'View Orders' permission") | |
| if balance_success and positions_success and orders_success: | |
| print("π All private API endpoints working!") | |
| return True | |
| else: | |
| print("β οΈ Some private API endpoints failed") | |
| print(" For trading bot to work, you need:") | |
| print(" - Valid API credentials") | |
| print(" - API key with permissions: View Account Balance, View Positions, View Orders") | |
| print(" - API key with permissions: Trade (for placing orders)") | |
| print(" - Funded testnet account (if using testnet)") | |
| return False | |
| except Exception as e: | |
| print(f"β Private API connection failed: {e}") | |
| return False | |
| def main(): | |
| print("π Bybit API Connection Test") | |
| print("=" * 40) | |
| public_ok = test_public_endpoints() | |
| private_ok = test_private_endpoints() | |
| print("\n" + "=" * 40) | |
| print("π Test Results:") | |
| if public_ok: | |
| print("β Public API: Working") | |
| else: | |
| print("β Public API: Failed") | |
| if private_ok: | |
| print("β Private API: Working") | |
| print("π All API tests passed! Ready for trading.") | |
| return True | |
| else: | |
| print("β Private API: Failed") | |
| print("\nπ§ To fix private API issues:") | |
| print("1. Go to Bybit account > API Management") | |
| print("2. Ensure your API key has these permissions:") | |
| print(" - 'Unified Trading Account' - Read & Trade") | |
| print(" - 'USDC Derivatives' - Read & Trade (if using USDC)") | |
| print(" - 'Spot' - Read & Trade (if using spot)") | |
| print("3. Make sure your account is upgraded to Unified Trading Account") | |
| print("4. Fund your testnet account with USDT:") | |
| print(" - Go to Bybit testnet faucet or transfer from mainnet") | |
| print(" - Minimum 10 USDT recommended for testing") | |
| print("5. Verify API credentials in .env file are correct") | |
| return False | |
| if __name__ == "__main__": | |
| success = main() | |
| exit(0 if success else 1) | |