diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..04f196a --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include README.md +include LICENSE diff --git a/README.md b/README.md index d85ffe9..833ab0b 100644 --- a/README.md +++ b/README.md @@ -5,37 +5,45 @@ Easily communicate with the Gemini Bitcoin exchange API without having to deal with HTTP requests. +Requirements +============ +Please get an API key from [https://exchange.gemini.com/settings/api](https://exchange.gemini.com/settings/api) + + Installation ============ -Just download geminipy.py +```shell +pip install geminipy +``` Example ======= - from geminipy import Geminipy - - con = Geminipy(api_key='your API key', secret_key='your secret key') +```python +from geminipy import Geminipy + +# The connection defaults to the Gemini sandbox. +# Add 'live=True' to use the live exchange +con = Geminipy(api_key='your API key', secret_key='your secret key', live=True) - # public request - symbols = con.symbols() +# public request +symbols = con.symbols() - print symbols.content +# a Requests response is returned. +# So we can access the HTTP reponse code, +# the raw response content, or a json object +print symbols.status_code +print symbols.content +print symbols.json() - # authenticated request - order = con.new_order(amount='1', price='200',side='buy') +# authenticated request +order = con.new_order(amount='1', price='200',side='buy') - print order.content +print order.json() - #send a heartbeat - con.heartbeat() +#send a heartbeat +con.heartbeat() +``` The required nonce is the current millisecond timestamp. - -Dependencies -============ - -requests >=2.9.1 - - sudo pip install requests --upgrade - diff --git a/changelog b/changelog new file mode 100644 index 0000000..274a0ad --- /dev/null +++ b/changelog @@ -0,0 +1,14 @@ +# Geminipy Changelog + +# v0.0.4 +Added "fees" function for the fee and notional volume API endpoint + +# v0.0.3 +Added "pubticker" function for the public ticker API endpoint +Added "auction" and "auction_history" functions for auction data +Added "tradevolume" function to get user trade volume +Added "newAddress" function to get a new cryptocurrency deposit address + +# v0.0.2 +Changed project layout to make it compatible with PyPi (pip) +Added an "options" argument to the "new_order" function diff --git a/geminipy.py b/geminipy/__init__.py similarity index 69% rename from geminipy.py rename to geminipy/__init__.py index e1a68c9..20c0a3c 100644 --- a/geminipy.py +++ b/geminipy/__init__.py @@ -32,7 +32,7 @@ def __init__(self, api_key='', secret_key='', live=False): 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) + live -- use the live API? otherwise, use the sandbox (default False) """ self.api_key = api_key self.secret_key = secret_key @@ -44,8 +44,14 @@ def __init__(self, api_key='', secret_key='', live=False): 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 + + return requests.get(url) + + def pubticker(self, symbol='btcusd'): + """Send a request for latest ticker info, return the response.""" + url = self.base_url + '/v1/pubticker/' + symbol + + return requests.get(url) def book(self, symbol='btcusd', limit_bids=0, limit_asks=0): """ @@ -61,8 +67,8 @@ def book(self, symbol='btcusd', limit_bids=0, limit_asks=0): 'limit_bids': limit_bids, 'limit_asks': limit_asks } - response = requests.get(url, params) - return response.content + + return requests.get(url, params) def trades(self, symbol='btcusd', since=0, limit_trades=50, include_breaks=0): @@ -81,12 +87,40 @@ def trades(self, symbol='btcusd', since=0, limit_trades=50, 'limit_trades': limit_trades, 'include_breaks': include_breaks } - response = requests.get(url, params) - return response.content + + return requests.get(url, params) + + def auction(self, symbol='btcusd'): + """Send a request for latest auction info, return the response.""" + url = self.base_url + '/v1/auction/' + symbol + + return requests.get(url) + + def auction_history(self, symbol='btcusd', since=0, + limit_auction_results=50, include_indicative=1): + """ + Send a request for auction history info, return the response. + + Arguments: + symbol -- currency symbol (default 'btcusd') + since -- only return auction events after this timestamp (default 0) + limit_auction_results -- maximum number of auction events to return + (default 50). + include_indicative -- whether to include publication of indicative + prices and quantities. (default True) + """ + url = self.base_url + '/v1/auction/' + symbol + '/history' + params = { + 'since': since, + 'limit_auction_results': limit_auction_results, + 'include_indicative': include_indicative + } + + return requests.get(url, params) # authenticated requests def new_order(self, amount, price, side, client_order_id=None, - symbol='btcusd', type='exchange limit'): + symbol='btcusd', type='exchange limit', options=None): """ Send a request to place an order, return the response. @@ -113,6 +147,9 @@ def new_order(self, amount, price, side, client_order_id=None, if client_order_id is not None: params['client_order_id'] = client_order_id + if options is not None: + params['options'] = options + return requests.post(url, headers=self.prepare(params)) def cancel_order(self, order_id): @@ -203,6 +240,17 @@ def past_trades(self, symbol='btcusd', limit_trades=50, timestamp=0): return requests.post(url, headers=self.prepare(params)) + def tradevolume(self): + """Send a request to get your trade volume, return the response.""" + request = '/v1/tradevolume' + url = self.base_url + request + params = { + 'request': request, + 'nonce': self.get_nonce() + } + + return requests.post(url, headers=self.prepare(params)) + def balances(self): """Send an account balance request, return the response.""" request = '/v1/balances' @@ -214,6 +262,38 @@ def balances(self): return requests.post(url, headers=self.prepare(params)) + def newAddress(self, currency='btc', label=''): + """ + Send a request for a new cryptocurrency deposit address + with an optional label. Return the response. + + Arguements: + currency -- a Gemini supported cryptocurrency (btc, eth) + label -- optional label for the deposit address + """ + request = '/v1/deposit/' + currency + '/newAddress' + url = self.base_url + request + params = { + 'request': request, + 'nonce': self.get_nonce() + } + + if label != '': + params['label'] = label + + return requests.post(url, headers=self.prepare(params)) + + def fees(self): + """Send a request to get fee and notional volume, return the response.""" + request = '/v1/notionalvolume' + url = self.base_url + request + params = { + 'request': request, + 'nonce': self.get_nonce() + } + + return requests.post(url, headers=self.prepare(params)) + def heartbeat(self): """Send a heartbeat message, return the response.""" request = '/v1/heartbeat' @@ -234,14 +314,14 @@ 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. + create the HTTP headers, 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, + payload = base64.b64encode(jsonparams.encode()) + signature = hmac.new(self.secret_key.encode(), payload, hashlib.sha384).hexdigest() return {'X-GEMINI-APIKEY': self.api_key, diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..28fbc91 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests>=2.9.1 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..c9d0387 --- /dev/null +++ b/setup.py @@ -0,0 +1,17 @@ +from setuptools import setup + +with open('README.md') as f: + readme = f.read().replace('```', '') + +setup( + name='geminipy', + version='0.0.4', + packages=['geminipy'], + url='https://github.com/geminipy/geminipy', + license='GNU GPL', + author='Mike Marzigliano', + author_email='marzig76@gmail.com', + zip_safe=False, + long_description=readme, + description='API client for Gemini', +)