diff --git a/libcryptomarket/core/candle/__init__.py b/libcryptomarket/core/candle/__init__.py index ef8bb08..dc59462 100644 --- a/libcryptomarket/core/candle/__init__.py +++ b/libcryptomarket/core/candle/__init__.py @@ -24,14 +24,21 @@ FREQUENCY_TO_SEC_DICT.update(dict( from .exchanges import * # noqa -def candles(source, symbol, start_time, end_time, frequency): - """Return candles of a given period and frequency. + +def candles(source, symbol, start_time, end_time, frequency, **kwargs): + r"""Return candles of a given period and frequency. :param source: `str` exchange name. :param symbol: `str` symbol. :param start_time: `datetime` start time. :param end_time: `datetime` end time. :param frequency: `str` frequency. + :param \**kwargs: + See below + + :Keyword Arguments: + * *quote_currency* (``str``) -- + Quote currency symbol, e.g. BTC. """ source = source.lower() @@ -57,7 +64,7 @@ def candles(source, symbol, start_time, end_time, frequency): end_time - pd.DateOffset(seconds=FREQUENCY_TO_SEC_DICT[frequency])): sleep(describe['rateLimit'] / 1000) data = func(source=exchange, symbol=symbol, start_time=start_time, - end_time=end_time, frequency=frequency) + end_time=end_time, frequency=frequency, **kwargs) if len(data) == 0: break @@ -78,7 +85,8 @@ def candles(source, symbol, start_time, end_time, frequency): return pd.concat(all_data) -def latest_candles(source, symbols, frequency, frequency_count, end_time=None): +def latest_candles(source, symbols, frequency, frequency_count, end_time=None, + **kwargs): """Return the latest candles based on the frequency and its count. :param source: `str` exchange name. @@ -87,6 +95,12 @@ def latest_candles(source, symbols, frequency, frequency_count, end_time=None): :param frequency: `str` frequency. :param end_time: `datetime` end time. Default is None which will use current time. + :param \**kwargs: + See below + + :Keyword Arguments: + * *quote_currency* (``str``) -- + Quote currency symbol, e.g. BTC. """ if isinstance(symbols, str): symbols = [symbols] @@ -105,7 +119,8 @@ def latest_candles(source, symbols, frequency, frequency_count, end_time=None): symbol=symbol, start_time=start_time, end_time=closest_end_time, - frequency=frequency) + frequency=frequency, + **kwargs) data = data[data['end_time'] <= closest_end_time] all_data.append(data.set_index(['start_time', 'end_time'])) diff --git a/libcryptomarket/core/candle/exchanges.py b/libcryptomarket/core/candle/exchanges.py index 8fca57f..296a42e 100644 --- a/libcryptomarket/core/candle/exchanges.py +++ b/libcryptomarket/core/candle/exchanges.py @@ -3,7 +3,8 @@ import pandas as pd from libcryptomarket.core.candle import FREQUENCY_TO_SEC_DICT -def poloniex_candles(source, symbol, start_time, end_time, frequency): +def poloniex_candles(source, symbol, start_time, end_time, frequency, + **kwargs): """Poloniex candles. """ data = source.public_get_returnchartdata(params={ @@ -24,10 +25,29 @@ def poloniex_candles(source, symbol, start_time, end_time, frequency): data['end_time'] = data['start_time'] + pd.DateOffset( seconds=FREQUENCY_TO_SEC_DICT[frequency]) + if 'quote_currency' in kwargs.keys(): + base_currency = symbol.split('_')[1] + + if kwargs['quote_currency'] == base_currency: + data.loc[:, "open"] = (1 / data.loc[:, "open"]).apply( + lambda x: round(x, 8)) + data.loc[:, "close"] = (1 / data.loc[:, "close"]).apply( + lambda x: round(x, 8)) + data.loc[:, "weighted_average"] = ( + (1 / data.loc[:, "weighted_average"]).apply( + lambda x: round(x, 8))) + high_prices = (1 / data.loc[:, "low"]).apply( + lambda x: round(x, 8)) + low_prices = (1 / data.loc[:, "high"]).apply( + lambda x: round(x, 8)) + data.loc[:, "high"] = high_prices + data.loc[:, "low"] = low_prices + return data -def bitfinex_candles(source, symbol, start_time, end_time, frequency): +def bitfinex_candles(source, symbol, start_time, end_time, frequency, + **kwargs): """Bitfinex candles. """ data = source.request( @@ -51,7 +71,8 @@ def bitfinex_candles(source, symbol, start_time, end_time, frequency): return data -def gdax_candles(source, symbol, start_time, end_time, frequency): +def gdax_candles(source, symbol, start_time, end_time, frequency, + **kwargs): """GDAX candles. """ data = source.request( diff --git a/notebooks/Candles.ipynb b/notebooks/Candles.ipynb index af4b7e4..e7f19ef 100644 --- a/notebooks/Candles.ipynb +++ b/notebooks/Candles.ipynb @@ -106,6 +106,40 @@ " assert data.shape[0] == 1" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Latest candles with quote_currency" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "ExecuteTime": { + "end_time": "2018-02-05T14:32:57.924368Z", + "start_time": "2018-02-05T14:32:55.837626Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Running exchange poloniex for instrument ['USDT_BTC']\n" + ] + } + ], + "source": [ + "for source, symbols in [\n", + " (\"poloniex\", [\"USDT_BTC\", ]), \n", + " ]:\n", + " print(\"Running exchange {} for instrument {}\".format(source, symbols))\n", + " data = latest_candles(source=source, symbols=symbols, frequency=\"5m\", frequency_count=1, quote_currency=\"BTC\")\n", + " assert data.shape[0] == 1" + ] + }, { "cell_type": "code", "execution_count": null,