-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinance_api.py
More file actions
85 lines (67 loc) · 2.76 KB
/
Copy pathbinance_api.py
File metadata and controls
85 lines (67 loc) · 2.76 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import time
import hmac
import hashlib
import requests
BASE_URL = "https://api.binance.com"
class BinanceAPI:
def __init__(self, api_key, api_secret):
self.api_key = api_key
self.api_secret = api_secret
self.transactions = [] # Store the transactions here
def _get_signature(self, data):
return hmac.new(bytes(self.api_secret, 'latin-1'), msg=bytes(data, 'latin-1'),
digestmod=hashlib.sha256).hexdigest()
def get_symbol_info(self, symbol):
headers = {
"X-MBX-APIKEY": self.api_key
}
response = requests.get(f"{BASE_URL}/api/v3/exchangeInfo", headers=headers, params={"symbol": symbol})
return response.json()
def get_balance(self):
timestamp = int(time.time() * 1000)
data = f"timestamp={timestamp}"
headers = {
"X-MBX-APIKEY": self.api_key
}
signature = self._get_signature(data)
response = requests.get(f"{BASE_URL}/api/v3/account", headers=headers, params=data + f"&signature={signature}")
balances = {}
for asset in response.json().get("balances", []):
if float(asset["free"]) > 0 or float(asset["locked"]) > 0:
balances[asset["asset"]] = {
"free": asset["free"],
"locked": asset["locked"]
}
return balances
def _send_order(self, symbol, side, quantity, price, order_type="LIMIT"):
timestamp = int(time.time() * 1000) # Binance uses milliseconds timestamp
data = f"symbol={symbol}&side={side}&type={order_type}&timeInForce=GTC&quantity={quantity}&price={price}×tamp={timestamp}"
headers = {
"X-MBX-APIKEY": self.api_key
}
signature = self._get_signature(data)
response = requests.post(f"{BASE_URL}/api/v3/order", headers=headers, data=data + f"&signature={signature}")
print(side)
print(response.content)
# Log the transaction after sending an order
self.transactions.append({
"symbol": symbol,
"side": side,
"quantity": quantity,
"price": price,
"timestamp": timestamp,
"response": response.json()
})
return response.json()
def buy(self, symbol, quantity, price):
return self._send_order(symbol, "BUY", quantity, price)
def sell(self, symbol, quantity, price):
return self._send_order(symbol, "SELL", quantity, price)
def view_transactions(self):
return self.transactions
# Usage:
# api = BinanceAPI(YOUR_API_KEY, YOUR_API_SECRET)
# buy_response = api.buy("BTCUSDT", 1, 40000)
# sell_response = api.sell("BTCUSDT", 1, 45000)
# transactions = api.view_transactions()
# print(transactions)