From 7c16b34a8d723dab3048bfa79c38c4b6d32262d9 Mon Sep 17 00:00:00 2001 From: "Gavin.Chan" Date: Fri, 19 Jan 2018 10:50:09 +0000 Subject: [PATCH] [#19] Build candle api --- libcryptomarket/core/__init__.py | 1 + libcryptomarket/core/candle.py | 78 ++++++++++++++++++++++++++++++++ setup.py | 3 +- 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 libcryptomarket/core/candle.py diff --git a/libcryptomarket/core/__init__.py b/libcryptomarket/core/__init__.py index 8f94529..9ef2f9a 100644 --- a/libcryptomarket/core/__init__.py +++ b/libcryptomarket/core/__init__.py @@ -3,3 +3,4 @@ from libcryptomarket.core.historical import historical_ticker from libcryptomarket.core.instrument import instruments from libcryptomarket.core.order_book import order_book +from libcryptomarket.core.candle import candles, latest_candles diff --git a/libcryptomarket/core/candle.py b/libcryptomarket/core/candle.py new file mode 100644 index 0000000..1915508 --- /dev/null +++ b/libcryptomarket/core/candle.py @@ -0,0 +1,78 @@ +from datetime import datetime, timedelta + +import pandas as pd +import ccxt + + +def candles(source, symbol, start_time, end_time, frequency): + """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: `int` frequency in seconds. + """ + + if source.lower() == 'poloniex': + source = getattr(ccxt, source.lower())() + + data = source.public_get_returnchartdata(params={ + "currencyPair": symbol, + "start": start_time.timestamp(), + "end": end_time.timestamp(), + "period": frequency + }) + + data = pd.DataFrame(data).rename(columns={ + 'date': 'start_time', + 'quoteVolume': 'quote_volume', + 'weightedAverage': 'weighted_average' + }) + + data.loc[:, 'start_time'] = data['start_time'].apply( + pd.Timestamp.fromtimestamp) + data['end_time'] = data['start_time'] + pd.DateOffset( + seconds=frequency) + + return data + else: + raise ValueError("Source {} is not implemented".format( + source.__class__.__name__)) + + +def latest_candles(source, symbols, frequency, frequency_count, end_time=None): + """Return the latest candles based on the frequency and its count. + + :param source: `str` exchange name. + :param symbols: `list` list of symbols, or `str` symbol name. + :param frequency: `int` frequency in seconds. + :param frequency: `int` frequency count. + :param end_time: `datetime` end time. Default is None which will use + current time. + """ + if isinstance(symbols, str): + symbols = [symbols] + + if end_time is None: + end_time = datetime.utcnow() + + closest_end_time = pd.Timestamp(end_time).floor( + timedelta(seconds=frequency)) + start_time = closest_end_time - timedelta( + seconds=frequency * frequency_count) + + all_data = [] + for symbol in symbols: + data = candles(source=source, + symbol=symbol, + start_time=start_time, + end_time=closest_end_time, + frequency=frequency) + data = data[data['end_time'] <= closest_end_time] + all_data.append(data.set_index(['start_time', 'end_time'])) + + if len(all_data) == 1: + return all_data[0] + else: + return pd.concat(all_data, axis=1, keys=symbols) diff --git a/setup.py b/setup.py index 9f6dace..5f928af 100644 --- a/setup.py +++ b/setup.py @@ -12,9 +12,10 @@ with open('HISTORY.rst') as history_file: history = history_file.read() requirements = [ + 'ccxt>=1.10.0', 'pandas>=0.20.0', 'requests', - 'setuptools_scm' + 'setuptools_scm', ] setup_requirements = [