Skip to content

Instantly share code, notes, and snippets.

@Jwan622
Last active May 29, 2020 15:22
Show Gist options
  • Save Jwan622/70f38299c3b6e5c91dbca0927b811836 to your computer and use it in GitHub Desktop.
Save Jwan622/70f38299c3b6e5c91dbca0927b811836 to your computer and use it in GitHub Desktop.
gemini api calls
import requests
import json
import base64
import hmac
import hashlib
import datetime, time
base_url = "https://api.sandbox.gemini.com"
gemini_api_key = "mykey"
gemini_api_secret = "1234abcd".encode()
# this gets the symbol list
print("======================SYMBOL LIST=====================")
response = requests.get(base_url + "/v1/symbols")
symbols = response.json()
print(symbols)
# this gets the price feed
print("==============PRICE FEED=====================")
price_feed_response = requests.get(base_url + "/v1/pricefeed")
prices = price_feed_response.json()
print(prices)
# this is the order book
print("==============ORDER BOOK FOR BTCUSD=====================")
response = requests.get(base_url + "/v1/book/btcusd")
btc_book = response.json()
print(btc_book)
# this is the trade history
print("==============TRADE HISTORY FOR BTCUSD=====================")
response = requests.get(base_url + "/v1/trades/btcusd")
btcusd_trades = response.json()
print(btcusd_trades)
# this is the auction history
print("==============AUCTION HISTORY FOR BTCUSD=====================")
response = requests.get(base_url + "/v1/auction/btcusd/history")
btc_auction_history = response.json()
print(btc_auction_history)
# this cancels all orders
print("=======================THIS CANCELS ALL ORDERS==================")
endpoint_for_cancel_all = "/v1/order/cancel/all"
url = base_url + endpoint_for_cancel_all
t = datetime.datetime.now()
nonce_for_cancel_all = str(int(time.mktime(t.timetuple())*1000))
payload = {
"request": "/v1/order/cancel/session",
"nonce": nonce_for_cancel_all
}
encoded_payload = json.dumps(payload).encode()
b64 = base64.b64encode(encoded_payload)
signature = hmac.new(gemini_api_secret, b64, hashlib.sha384).hexdigest()
request_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.post(url,
data=None,
headers=request_headers)
cancel_all_response = response.json()
print(cancel_all_response)
# this is order placement
print("==============ORDER PLACEMENT FOR NEW ORDERS=====================")
endpoint = "/v1/order/new"
url = base_url + endpoint
t = datetime.datetime.now()
payload_nonce = str(int(time.mktime(t.timetuple())*1000))
payload = {
"request": "/v1/order/new",
"nonce": payload_nonce,
"symbol": "btcusd",
"amount": "5",
"price": "3633.00",
"side": "buy",
"type": "exchange limit",
"options": ["maker-or-cancel"]
}
encoded_payload = json.dumps(payload).encode()
b64 = base64.b64encode(encoded_payload)
signature = hmac.new(gemini_api_secret, b64, hashlib.sha384).hexdigest()
request_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" }
print("before the request")
response = requests.post(url,
data=None,
headers=request_headers)
new_order = response.json()
print(new_order)
# might want to look at this:
# https://info.folioinstitutional.com/api-service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment