-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_test.py
More file actions
68 lines (55 loc) · 2.59 KB
/
simple_test.py
File metadata and controls
68 lines (55 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
from binance.client import Client
from binance.exceptions import BinanceAPIException
from dotenv import load_dotenv
def main():
print("🔍 Testing Binance Testnet API connection...")
# Load environment variables
load_dotenv()
# Get API keys from environment variables
api_key = os.getenv("BINANCE_TESTNET_API_KEY") or os.getenv("BINANCE_API_KEY")
api_secret = os.getenv("BINANCE_TESTNET_API_SECRET") or os.getenv("BINANCE_API_SECRET")
if not api_key or not api_secret:
print("❌ Error: API keys not found in environment variables")
print("Please create a .env file with your Binance Testnet API keys")
print("See .env.example for reference")
return
print(f"🔑 Using API Key: {api_key[:8]}...{api_key[-4:]}")
try:
# Initialize client
print("🚀 Initializing client...")
client = Client(api_key, api_secret, testnet=True)
# Test server time
print("🕒 Getting server time...")
time = client.get_server_time()
print(f"✅ Server time: {time['serverTime']}")
# Test futures exchange info
print("📊 Getting exchange info...")
info = client.futures_exchange_info()
print(f"✅ Exchange info received. Rate limits:")
for limit in info['rateLimits'][:2]:
print(f" - {limit['rateLimitType']}: {limit['limit']} requests per {limit['intervalNum']} {limit['interval']}")
# Test account balance
print("💰 Getting account balance...")
balance = client.futures_account_balance()
print("✅ Account balance received")
for asset in balance:
if float(asset['balance']) > 0:
print(f" - {asset['asset']}: {asset['balance']}")
# Test order book
print("📈 Getting order book...")
orderbook = client.futures_order_book(symbol='BTCUSDT', limit=5)
print(f"✅ Order book received. Best bid: {orderbook['bids'][0][0]}, Best ask: {orderbook['asks'][0][0]}")
print("\n🎉 All tests passed successfully!")
except BinanceAPIException as e:
print(f"❌ Binance API Error ({e.status_code}): {e.message}")
if e.status_code == 401:
print(" - Invalid API key/secret or permissions")
elif e.status_code == 403:
print(" - IP address not whitelisted")
elif e.status_code == 429:
print(" - Rate limit exceeded")
except Exception as e:
print(f"❌ Unexpected error: {str(e)}")
if __name__ == "__main__":
main()