8000 docstrings and documentation by marzig76 · Pull Request #5 · geminipy/geminipy · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
# geminipy
# Geminipy
A library for the Gemini bitcoin exchange API

Easily communicate with the Gemini Bitcoin exchange API without
having to deal with HTTP requests.


Installation
============

Just download geminipy.py

Example
=======

from geminipy import Geminipy

con = Geminipy(api_key='your API key', secret_key='your secret key')

# public request
symbols = con.symbols()

print symbols.content

# authenticated request
order = con.new_order(amount='1', price='200',side='buy')

print order.content

#send a heartbeat
con.heartbeat()

The required nonce is the current millisecond timestamp.

Dependencies
============

requests >=2.9.1

sudo pip install requests --upgrade

83 changes: 83 additions & 0 deletions geminipy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
This module contains a class to make requests to the Gemini API.

Author: Mike Marzigliano
"""
import time
import json
import hmac
Expand All @@ -7,6 +12,12 @@


class Geminipy(object):
"""
A class to make requests to the Gemini API.

Make public or authenticated requests according to the API documentation:
https://docs.gemini.com/
"""

live_url = 'https://api.gemini.com'
sandbox_url = 'https://api.sandbox.gemini.com'
Expand All @@ -15,6 +26,14 @@ class Geminipy(object):
secret_key = ''

def __init__(self, api_key='', secret_key='', live=False):
"""
Initialize the class.

Arguments:
api_key -- your Gemini API key
secret_key -- your Gemini API secret key for signatures
live -- use the live API or the Sandbox API (default False)
"""
self.api_key = api_key
self.secret_key = secret_key

Expand All @@ -23,11 +42,20 @@ def __init__(self, api_key='', secret_key='', live=False):

# public requests
def symbols(self):
"""Send a request for all trading symbols, return the response."""
url = self.base_url + '/v1/symbols'
response = requests.get(url)
return response.content

def book(self, symbol='btcusd', limit_bids=0, limit_asks=0):
"""
Send a request to get the public order book, return the response.

Arguments:
symbol -- currency symbol (default 'btcusd')
limit_bids -- limit the number of bids returned (default 0)
limit_asks -- limit the number of asks returned (default 0)
"""
url = self.base_url + '/v1/book/' + symbol
params = {
'limit_bids': limit_bids,
Expand All @@ -38,6 +66,15 @@ def book(self, symbol='btcusd', limit_bids=0, limit_asks=0):

def trades(self, symbol='btcusd', since=0, limit_trades=50,
include_breaks=0):
"""
Send a request to get all public trades, return the response.

Arguments:
symbol -- currency symbol (default 'btcusd')
since -- only return trades after this unix timestamp (default 0)
limit_trades -- maximum number of trades to return (default 50).
include_breaks -- whether to display broken trades (default False)
"""
url = self.base_url + '/v1/trades/' + symbol
params = {
'since': since,
Expand All @@ -50,6 +87,17 @@ def trades(self, symbol='btcusd', since=0, limit_trades=50,
# authenticated requests
def new_order(self, amount, price, side, client_order_id=None,
symbol='btcusd', type='exchange limit'):
"""
Send a request to place an order, return the response.

Arguments:
amount -- quoted decimal amount of BTC to purchase
price -- quoted decimal amount of USD to spend per BTC
side -- 'buy' or 'sell'
client_order_id -- an optional client-specified order id (default None)
symbol -- currency symbol (default 'btcusd')
type -- the order type (default 'exchange limit')
"""
request = '/v1/order/new'
url = self.base_url + request
params = {
Expand All @@ -68,6 +116,12 @@ def new_order(self, amount, price, side, client_order_id=None,
return requests.post(url, headers=self.prepare(params))

def cancel_order(self, order_id):
"""
Send a request to cancel an order, return the response.

Arguments:
order_id - the order id to cancel
"""
request = '/v1/order/cancel'
url = self.base_url + request
params = {
Expand All @@ -79,6 +133,7 @@ def cancel_order(self, order_id):
return requests.post(url, headers=self.prepare(params))

def cancel_session(self):
"""Send a request to cancel all session orders, return the response."""
request = '/v1/order/cancel/session'
url = self.base_url + request
params = {
Expand All @@ -89,6 +144,7 @@ def cancel_session(self):
return requests.post(url, headers=self.prepare(params))

def cancel_all(self):
"""Send a request to cancel all orders, return the response."""
request = '/v1/order/cancel/all'
url = self.base_url + request
params = {
Expand All @@ -99,6 +155,12 @@ def cancel_all(self):
return requests.post(url, headers=self.prepare(params))

def order_status(self, order_id):
"""
Send a request to get an order status, return the response.

Arguments:
order_id -- the order id to get information on
"""
request = '/v1/order/status'
url = self.base_url + request
params = {
Expand All @@ -110,6 +172,7 @@ def order_status(self, order_id):
return requests.post(url, headers=self.prepare(params))

def active_orders(self):
"""Send a request to get active orders, return the response."""
request = '/v1/orders'
url = self.base_url + request
params = {
Expand All @@ -120,6 +183,14 @@ def active_orders(self):
return requests.post(url, headers=self.prepare(params))

def past_trades(self, symbol='btcusd', limit_trades=50, timestamp=0):
"""
Send a trade history request, return the response.

Arguements:
symbol -- currency symbol (default 'btcusd')
limit_trades -- maximum number of trades to return (default 50)
timestamp -- only return trades after this unix timestamp (default 0)
"""
request = '/v1/mytrades'
url = self.base_url + request
params = {
Expand All @@ -133,6 +204,7 @@ def past_trades(self, symbol='btcusd', limit_trades=50, timestamp=0):
return requests.post(url, headers=self.prepare(params))

def balances(self):
"""Send an account balance request, return the response."""
request = '/v1/balances'
url = self.base_url + request
params = {
Expand All @@ -143,6 +215,7 @@ def balances(self):
return requests.post(url, headers=self.prepare(params))

def heartbeat(self):
"""Send a heartbeat message, return the response."""
request = '/v1/heartbeat'
url = self.base_url + request
params = {
Expand All @@ -153,9 +226,19 @@ def heartbeat(self):
return requests.post(url, headers=self.prepare(params))

def get_nonce(self):
"""Return the current millisecond timestamp as the nonce."""
return int(round(time.time() * 1000))

def prepare(self, params):
"""
Prepare, return the required HTTP headers.

Base 64 encode the parameters, sign it with the secret key,
create the HTTP headres,, return the whole payload.

Arguments:
params -- a dictionary of parameters
"""
jsonparams = json.dumps(params)
payload = base64.b64encode(jsonparams)
signature = hmac.new(self.secret_key, payload,
Expand Down
0