diff --git a/.gitignore b/.gitignore index 547d303..3906afd 100644 --- a/.gitignore +++ b/.gitignore @@ -40,5 +40,8 @@ nosetests.xml .pydevproject # Other +.venv* .virtualenv* *.key +*.secret +*.pickle diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3f4660a..0f0e2ba 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,34 @@ to `semantic versioning`_. .. _Keep a Changelog: http://keepachangelog.com/ .. _semantic versioning: http://semver.org/ +[vX.Y.Z] - Unreleased +--------------------- + +* Nothing (yet). + +[v2.1.0] - 2018-04-20 (Fryday) +------------------------------ + +Added +^^^^^ +* A ``timeout`` argument for ``API.query_public()`` or + ``API.query_private()``, to set a time-out period for + that particular query. (`#67`_) +* ``krakenex.API.json_options()`` to set options to the JSON + de-serialiser, to be used on query responses. (`#87`_) +* An ``examples/json-options.py`` example to demonstrate + the above. (`#88`_) + +.. _#67: https://github.com/veox/python3-krakenex/pull/67 +.. _#87: https://github.com/veox/python3-krakenex/pull/87 +.. _#88: https://github.com/veox/python3-krakenex/pull/88 + +Changed +^^^^^^^ +* Updated examples using ``pandas`` to post-v0.17.0 sorting. (`#29`_) + +.. _#29: https://github.com/veox/python3-krakenex/issues/29 + [v2.0.0] - 2017-11-14 (Tuesday) ------------------------------- diff --git a/examples/generate-form8949-csv.py b/examples/generate-form8949-csv.py index 5bccaae..e9555e7 100755 --- a/examples/generate-form8949-csv.py +++ b/examples/generate-form8949-csv.py @@ -57,7 +57,7 @@ def data(start, end, ofs): # trades = pd.concat(data, axis = 0) trades = trades[~trades.index.duplicated()] -trades = trades.sort(columns='time', ascending=True) +trades = trades.sort_values(columns='time', ascending=True) trades.to_csv('data.csv') #format pair description for IRS diff --git a/examples/generate-trades-history-csv.py b/examples/generate-trades-history-csv.py index 6f254f7..2ba15fb 100755 --- a/examples/generate-trades-history-csv.py +++ b/examples/generate-trades-history-csv.py @@ -52,5 +52,5 @@ def data(start, end, ofs): trades = pd.DataFrame trades = pd.concat(data, axis = 0) -trades = trades.sort(columns='time', ascending=True) +trades = trades.sort_values(columns='time', ascending=True) trades.to_csv('data.csv') diff --git a/examples/json-options.py b/examples/json-options.py new file mode 100755 index 0000000..7e488a6 --- /dev/null +++ b/examples/json-options.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +# This file is part of krakenex. +# Licensed under the Simplified BSD license. See `examples/LICENSE.txt`. + +# Demonstrate use of json_options(). + +from types import SimpleNamespace + +import krakenex + +kraken = krakenex.API().json_options(object_hook=lambda kv: SimpleNamespace(**kv)) +response = kraken.query_public('Time') + +if response.error: + print('error:', response.error) +else: + result = response.result + print('unixtime:', result.unixtime) + print('rfc1123:', result.rfc1123) + diff --git a/examples/print-open-positions.py b/examples/print-open-positions.py index b6bb37c..23e769a 100755 --- a/examples/print-open-positions.py +++ b/examples/print-open-positions.py @@ -16,26 +16,32 @@ # prepare request req_data = {'docalcs': 'true'} -# querry servers +# query servers start = k.query_public('Time') open_positions = k.query_private('OpenPositions', req_data) end = k.query_public('Time') -latency = end['result']['unixtime']-start['result']['unixtime'] +latency = end['result']['unixtime'] - start['result']['unixtime'] # parse result -dict(open_positions) +b, c = 0, 0 -b = 0 -c = 0 -for i in open_positions['result']: - order = open_positions['result'][i] - if(order['pair']=='XETHZUSD'): +for order in open_positions['result']: + coin = order["pair"] + if coin == 'XETHZUSD': b += (float(order['vol'])) - if (order['pair'] == 'XXBTZUSD'): + elif coin == 'XXBTZUSD': c += (float(order['vol'])) -print('error count: ' + str(len(open_positions['error']))) -print('latency: ' + str(latency)) -print('total open eth: ' + str(b)) -print('total open btc: ' + str(c)) -print('total open positions: ' + str(len(open_positions['result']))) +n_errors = len(open_positions['error']) +total = len(open_positions['result']) + +msg = """ +error counts: {n_errors} +latency: {latency} + +open orders + eth: {b} + btc: {c} + total: {total} +""" +print(msg.format(n_errors=n_errors, total=total, b=b, c=c, latency=latency)) diff --git a/krakenex/api.py b/krakenex/api.py index db4ef28..de361f7 100644 --- a/krakenex/api.py +++ b/krakenex/api.py @@ -66,8 +66,19 @@ def __init__(self, key='', secret=''): 'User-Agent': 'krakenex/' + version.__version__ + ' (+' + version.__url__ + ')' }) self.response = None + self._json_options = {} return + def json_options(self, **kwargs): + """ Set keyword arguments to be passed to JSON deserialization. + + :param kwargs: passed to :py:meth:`requests.Response.json` + :returns: this instance for chaining + + """ + self._json_options = kwargs + return self + def close(self): """ Close this session. @@ -120,13 +131,13 @@ def _query(self, urlpath, data, headers=None, timeout=None): url = self.uri + urlpath - self.response = self.session.post(url, data = data, headers = headers, + self.response = self.session.post(url, data = data, headers = headers, timeout = timeout) if self.response.status_code not in (200, 201, 202): self.response.raise_for_status() - return self.response.json() + return self.response.json(**self._json_options) def query_public(self, method, data=None, timeout=None): diff --git a/krakenex/version.py b/krakenex/version.py index cb12f89..c03c2d7 100644 --- a/krakenex/version.py +++ b/krakenex/version.py @@ -22,5 +22,5 @@ """ -__version__ = '2.0.0' +__version__ = '2.1.0' __url__ = 'https://github.com/veox/python3-krakenex'