Created
December 26, 2017 20:34
-
-
Save brandomr/ea87b4ee11a9921e19c415fd19fa733f to your computer and use it in GitHub Desktop.
Class for interacting with Gemini API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class gemini(object): | |
""" | |
An object for interacting with the Gemini API. Full Gemini API documentation is | |
available at https://docs.gemini.com | |
""" | |
def __init__(self, gemini_api_key, gemini_api_secret, base_url): | |
""" | |
Initializes gemini object. | |
Args: | |
gemini_api_key: your API key for the exchange | |
gemini_api_secret: secret associated with your API key | |
base_url: https://api.sandbox.gemini.com (live) OR https://api.sandbox.gemini.com (sandbox) | |
""" | |
self.gemini_api_key = gemini_api_key | |
self.gemini_api_secret = gemini_api_secret | |
self.base_url = base_url | |
def json_to_b64(self, payload): | |
""" | |
Takes JSON payload and base64 encodes it | |
""" | |
b64 = base64.b64encode(str.encode((json.dumps(payload)))) | |
return b64 | |
def b64_to_json(self, b64): | |
""" | |
Takes base64 encoded JSON object and returns JSON | |
""" | |
byte_str = base64.b64decode(b64) | |
return json.loads(str(byte_str, 'utf-8')) | |
def gen_nonce(self): | |
""" | |
Generates epoch millis nonce | |
""" | |
return int(time.time()*1000) | |
def submit_order(self, side, amount, price, symbol): | |
""" | |
Submits either a buy or sell order to the exchange | |
Args: | |
side: either "buy" or "sell" | |
amount: the volume to trade | |
price: the price being bidded/asked | |
symbol: the ticker to trade (ex: btcusd) | |
Returns: | |
JSON response from exchange | |
""" | |
order_new_endpoint = "/v1/order/new" | |
url = self.base_url + order_new_endpoint | |
# generate order id | |
date = datetime.datetime.strftime(datetime.today(), '%Y%m%d') | |
order_id_hash = hashlib.md5(str.encode((str(random.random())))).hexdigest() | |
client_order_id = "{0}-{1}".format(date, order_id_hash) | |
# build payload and b64 encode it | |
payload = { | |
"request": order_new_endpoint, | |
"nonce": self.gen_nonce(), | |
"client_order_id": client_order_id, | |
"symbol": symbol, | |
"amount": amount, | |
"price": price, | |
"side": side, | |
"type": "exchange limit", | |
"options": ["maker-or-cancel"] | |
} | |
b64 = self.json_to_b64(payload) | |
# generate signature and header | |
signature = hmac.new(str.encode(self.gemini_api_secret), b64, sha384).hexdigest() | |
headers = { | |
'Content-Type': "text/plain", | |
'Content-Length': "0", | |
'X-GEMINI-APIKEY': self.gemini_api_key, | |
'X-GEMINI-PAYLOAD': b64, | |
'X-GEMINI-SIGNATURE': signature, | |
'Cache-Control': "no-cache" | |
} | |
# submit request | |
response = requests.request("POST", url, headers=headers) | |
return json.loads(response.text) | |
def order_status(self): | |
""" | |
Obtains order status | |
Returns: | |
JSON array of orders submitted to exchange | |
""" | |
order_status_endpoint = "/v1/orders" | |
url = self.base_url + order_status_endpoint | |
# build payload and b64 encode it | |
payload = { | |
"request": "/v1/orders", | |
"nonce": self.gen_nonce(), | |
} | |
b64 = json_to_b64(payload) | |
# generate signature and headers | |
signature = hmac.new(str.encode(gemini_api_secret), b64, sha384).hexdigest() | |
headers = { | |
'Content-Type': "text/plain", | |
'Content-Length': "0", | |
'X-GEMINI-APIKEY': gemini_api_key, | |
'X-GEMINI-PAYLOAD': b64, | |
'X-GEMINI-SIGNATURE': signature, | |
'Cache-Control': "no-cache" | |
} | |
response = requests.request("POST", base_url+order_status, headers=headers) | |
return json.loads(response.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment