From 4e1a89a3c67eb3db19e4f93583208c574fa23ab2 Mon Sep 17 00:00:00 2001 From: "Gavin.Chan" Date: Thu, 4 Jan 2018 15:21:45 +0000 Subject: [PATCH] [#15] Added optional variables on historical_tickers --- libcryptomarket/api/bitfinex_api.py | 5 ++++ libcryptomarket/api/exchange_api.py | 36 ++++++++++++++++++----------- libcryptomarket/core/historical.py | 29 +++++++++++++++-------- 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/libcryptomarket/api/bitfinex_api.py b/libcryptomarket/api/bitfinex_api.py index 723429d..42b4e3c 100644 --- a/libcryptomarket/api/bitfinex_api.py +++ b/libcryptomarket/api/bitfinex_api.py @@ -101,6 +101,11 @@ class BitfinexApi(ExchangeApi): "trade:{}:{}".format(timeframe, symbol), section]) + self.log_info("Public request:\n" + + "name: {}\n".format(name) + + "http_method: {}\n".format(http_method) + + "kwargs: {}".format(kwargs)) + return self._send_request( command=name, http_method=http_method, public_method=True, params=kwargs) diff --git a/libcryptomarket/api/exchange_api.py b/libcryptomarket/api/exchange_api.py index 840fc81..274c042 100644 --- a/libcryptomarket/api/exchange_api.py +++ b/libcryptomarket/api/exchange_api.py @@ -137,6 +137,18 @@ class ExchangeApi: """ raise NotImplementedError("Not yet implemented.") + def log_info(self, msg, *args): + """Log in INFO. + """ + if self._logger is not None: + self._logger.info(msg, *args) + + def log_debug(self, msg, *args): + """Log in DEBUG. + """ + if self._logger is not None: + self._logger.debug(msg, *args) + def _send_request(self, command, http_method, params=None, data=None, public_method=False): """Send request. @@ -193,14 +205,13 @@ class ExchangeApi: headers = None auth = None - if self._logger is not None: - self._logger.info(">>> OUT:\n%s" % json.dumps({ - "Method": http_method, - "Url": url, - "Params": params, - "Data": data, - "Headers": headers - })) + self.log_debug(">>> OUT:\n%s" % json.dumps({ + "Method": http_method, + "Url": url, + "Params": params, + "Data": data, + "Headers": headers + })) if auth is None: response = R(url, params=params, data=data, headers=headers) @@ -208,10 +219,9 @@ class ExchangeApi: response = R(url, params=params, data=data, headers=headers, auth=auth) - if self._logger is not None: - self._logger.info("<<< IN:\n%s" % json.dumps({ - "Status code": response.status_code, - "Text": response.text - })) + self.log_debug("<<< IN:\n%s" % json.dumps({ + "Status code": response.status_code, + "Text": response.text + })) return response diff --git a/libcryptomarket/core/historical.py b/libcryptomarket/core/historical.py index f4f54df..cf5a232 100644 --- a/libcryptomarket/core/historical.py +++ b/libcryptomarket/core/historical.py @@ -5,7 +5,8 @@ from time import sleep import pandas as pd -def historical_ticker(source, symbol, period, start_time=None, end_time=None): +def historical_ticker(source, symbol, period, start_time=None, end_time=None, + **kwargs): """Return historical ticker. :param source: Source, an Exchange API object. @@ -14,6 +15,7 @@ def historical_ticker(source, symbol, period, start_time=None, end_time=None): object. :param start_time: Start time, datetime object. :param end_time: Start time, datetime object. + :param wait_sec: Seconds to wait between queries, int. Optional. """ # Validation if start_time is not None and not isinstance(start_time, datetime): @@ -28,22 +30,25 @@ def historical_ticker(source, symbol, period, start_time=None, end_time=None): if source_name == "poloniex": return _historical_ticker_poloniex( source=source, symbol=symbol, period=period, - start_time=start_time, end_time=end_time) - + start_time=start_time, end_time=end_time, + **kwargs) elif source_name == "gdax": return _historical_ticker_gdax( source=source, symbol=symbol, period=period, - start_time=start_time, end_time=end_time) + start_time=start_time, end_time=end_time, + **kwargs) elif source_name == "bitfinex": return _historical_ticker_bitfinex( source=source, symbol=symbol, period=period, - start_time=start_time, end_time=end_time) + start_time=start_time, end_time=end_time, + **kwargs) else: raise ValueError("Source (%s [%s]) does not support historical ticker" % (source, source_name)) -def _historical_ticker_poloniex(source, symbol, period, start_time, end_time): +def _historical_ticker_poloniex(source, symbol, period, start_time, end_time, + **kwargs): """Return historical ticker in Poloniex. :param source: Source, an Exchange API object. @@ -78,7 +83,8 @@ def _historical_ticker_poloniex(source, symbol, period, start_time, end_time): return data -def _historical_ticker_gdax(source, symbol, period, start_time, end_time): +def _historical_ticker_gdax(source, symbol, period, start_time, end_time, + **kwargs): """Return historical ticker in GDAX. :param source: Source, an Exchange API object. @@ -87,6 +93,7 @@ def _historical_ticker_gdax(source, symbol, period, start_time, end_time): object. :param start_time: Start time, datetime object. :param end_time: Start time, datetime object. + :param wait_sec: Seconds to wait between queries, int. Optional. """ # Exchange validation if (start_time is None) + (end_time is None) not in [0, 2]: @@ -127,7 +134,7 @@ def _historical_ticker_gdax(source, symbol, period, start_time, end_time): last_datetime = tmp_data.iloc[0, 0] start_time = datetime.fromtimestamp(last_datetime) - sleep(0.333) + sleep(kwargs.get("wait_sec", 0.33)) if len(data) > 1: data = pd.concat(data, axis=0) @@ -142,7 +149,8 @@ def _historical_ticker_gdax(source, symbol, period, start_time, end_time): return data -def _historical_ticker_bitfinex(source, symbol, period, start_time, end_time): +def _historical_ticker_bitfinex(source, symbol, period, start_time, end_time, + **kwargs): """Return historical ticker in Bitfinex. :param source: Source, an Exchange API object. @@ -151,6 +159,7 @@ def _historical_ticker_bitfinex(source, symbol, period, start_time, end_time): object. :param start_time: Start time, datetime object. :param end_time: Start time, datetime object. + :param wait_sec: Seconds to wait between queries, int. Optional. """ request_func = partial(source.candles, symbol=symbol, timeframe=period, section="hist", sort=1, limit=1000) @@ -191,7 +200,7 @@ def _historical_ticker_bitfinex(source, symbol, period, start_time, end_time): round(last_datetime / 1000 + 1)) data.append(pd.DataFrame(tmp_data)) - sleep(1) + sleep(kwargs.get("wait_sec", 1)) data = pd.concat(data, axis=0)