-
Notifications
You must be signed in to change notification settings - Fork 2
/
gemini_api.py
103 lines (93 loc) · 3.62 KB
/
gemini_api.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import requests
import hmac
import hashlib
import time
import json
class GeminiAPI:
"""
This class contains the functions for interacting with the Gemini cryptocurrency exchange API. It handles the API requests
and responses and provides a unified interface for the application to communicate with Gemini.
"""
BASE_URL = "https://api.gemini.com"
def __init__(self, api_key, api_secret):
self.api_key = api_key
self.api_secret = api_secret
def get_signature(self, request_body, nonce):
"""
This function generates the signature for the API request.
"""
signature_payload = f"{nonce}{json.dumps(request_body)}"
signature = hmac.new(self.api_secret.encode(), signature_payload.encode(), hashlib.sha384).hexdigest()
return signature
def get_headers(self, request_body):
"""
This function generates the headers for the API request.
"""
nonce = int(time.time() * 1000)
signature = self.get_signature(request_body, nonce)
headers = {
"Content-Type": "text/plain",
"Content-Length": "0",
"X-GEMINI-APIKEY": self.api_key,
"X-GEMINI-PAYLOAD": json.dumps(request_body),
"X-GEMINI-SIGNATURE": signature,
"Cache-Control": "no-cache"
}
return headers
def get_ticker(self, symbol):
"""
This function returns the ticker information for the specified symbol.
"""
endpoint = f"{self.BASE_URL}/v1/pubticker/{symbol}"
response = requests.get(endpoint)
if response.status_code == 200:
return response.json()
else:
raise ValueError("Error getting ticker information from Gemini.")
def get_order_book(self, symbol):
"""
This function returns the order book for the specified symbol.
"""
endpoint = f"{self.BASE_URL}/v1/book/{symbol}"
response = requests.get(endpoint)
if response.status_code == 200:
return response.json()
else:
raise ValueError("Error getting order book from Gemini.")
def get_balance(self):
"""
This function returns the balance for the account associated with the API key.
"""
endpoint = f"{self.BASE_URL}/v1/balances"
request_body = {"request": "/v1/balances", "nonce": int(time.time() * 1000)}
headers = self.get_headers(request_body)
response = requests.post(endpoint, headers=headers)
if response.status_code == 200:
return response.json()
else:
raise ValueError("Error getting balance information from Gemini.")
def new_order(self, symbol, amount, price, side, order_type):
"""
This function places a new order with the specified parameters.
"""
endpoint = f"{self.BASE_URL}/v1/order/new"
request_body = {
"request": "/v1/order/new",
"nonce": int(time.time() * 1000),
"symbol": symbol,
"amount": str(amount),
"price": str(price),
"side": side,
"type": order_type
}
headers = self.get_headers(request_body)
response = requests.post(endpoint, headers=headers)
if response.status_code == 200:
return response.json()
else:
raise ValueError("Error placing order with Gemini.")
def cancel_order(self, order_id):
"""
This function cancels the order with the specified order ID.
"""
endpoint = f"{self.BASE_URL}/v1/order