From 769f304f91349b5e84d262c7d00404596e2c9df0 Mon Sep 17 00:00:00 2001 From: Martin Kersner Date: Mon, 3 Jun 2024 13:20:37 +0900 Subject: [PATCH] Unify candle data (#22) * chore: update cex comments * feat: unified candle data api * fix: pass missing exchange parameter * feat: simplify Datamaxi import * fix: noqa: F401 for auxiliary class import --- datamaxi/__init__.py | 1 + datamaxi/binance/__init__.py | 4 +- datamaxi/bithumb/__init__.py | 4 +- datamaxi/bybit/__init__.py | 4 +- datamaxi/coinone/__init__.py | 4 +- datamaxi/datamaxi/__init__.py | 84 +++++++++++++++++++++++++++++++++++ datamaxi/huobi/__init__.py | 4 +- datamaxi/okx/__init__.py | 4 +- datamaxi/upbit/__init__.py | 4 +- 9 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 datamaxi/datamaxi/__init__.py diff --git a/datamaxi/__init__.py b/datamaxi/__init__.py index e69de29..4548bca 100644 --- a/datamaxi/__init__.py +++ b/datamaxi/__init__.py @@ -0,0 +1 @@ +from datamaxi.datamaxi import Datamaxi # noqa: F401 diff --git a/datamaxi/binance/__init__.py b/datamaxi/binance/__init__.py index 994a2bd..c7bfdee 100644 --- a/datamaxi/binance/__init__.py +++ b/datamaxi/binance/__init__.py @@ -22,7 +22,7 @@ def __init__(self, api_key=None, **kwargs: Any): super().__init__(api_key, **kwargs) def symbols(self) -> List[str]: - """Supported Binance supported symbols + """Binance supported symbols `GET /v1/raw/binance/symbols` @@ -35,7 +35,7 @@ def symbols(self) -> List[str]: return self.query(url_path) def intervals(self) -> List[str]: - """Supported Binance supported intervals + """Binance supported intervals `GET /v1/raw/binance/intervals` diff --git a/datamaxi/bithumb/__init__.py b/datamaxi/bithumb/__init__.py index 15f4150..e9b6839 100644 --- a/datamaxi/bithumb/__init__.py +++ b/datamaxi/bithumb/__init__.py @@ -22,7 +22,7 @@ def __init__(self, api_key=None, **kwargs: Any): super().__init__(api_key, **kwargs) def symbols(self) -> List[str]: - """Supported Bithumb supported symbols + """Bithumb supported symbols `GET /v1/raw/bithumb/symbols` @@ -35,7 +35,7 @@ def symbols(self) -> List[str]: return self.query(url_path) def intervals(self) -> List[str]: - """Supported Bithumb supported intervals + """Bithumb supported intervals `GET /v1/raw/bithumb/intervals` diff --git a/datamaxi/bybit/__init__.py b/datamaxi/bybit/__init__.py index 9b833b8..b5f8a7a 100644 --- a/datamaxi/bybit/__init__.py +++ b/datamaxi/bybit/__init__.py @@ -22,7 +22,7 @@ def __init__(self, api_key=None, **kwargs: Any): super().__init__(api_key, **kwargs) def symbols(self) -> List[str]: - """Supported Bybit supported symbols + """Bybit supported symbols `GET /v1/raw/bybit/symbols` @@ -35,7 +35,7 @@ def symbols(self) -> List[str]: return self.query(url_path) def intervals(self) -> List[str]: - """Supported Bybit supported intervals + """Bybit supported intervals `GET /v1/raw/bybit/intervals` diff --git a/datamaxi/coinone/__init__.py b/datamaxi/coinone/__init__.py index 55f218e..a41326d 100644 --- a/datamaxi/coinone/__init__.py +++ b/datamaxi/coinone/__init__.py @@ -22,7 +22,7 @@ def __init__(self, api_key=None, **kwargs: Any): super().__init__(api_key, **kwargs) def symbols(self) -> List[str]: - """Supported Coinone supported symbols + """Coinone supported symbols `GET /v1/raw/coinone/symbols` @@ -35,7 +35,7 @@ def symbols(self) -> List[str]: return self.query(url_path) def intervals(self) -> List[str]: - """Supported Coinone supported intervals + """Coinone supported intervals `GET /v1/raw/coinone/intervals` diff --git a/datamaxi/datamaxi/__init__.py b/datamaxi/datamaxi/__init__.py new file mode 100644 index 0000000..996aba9 --- /dev/null +++ b/datamaxi/datamaxi/__init__.py @@ -0,0 +1,84 @@ +from typing import Any, List, Union +import pandas as pd +from datamaxi.api import API +from datamaxi.lib.utils import check_required_parameter +from datamaxi.lib.utils import check_required_parameters +from datamaxi.lib.utils import postprocess +from datamaxi.lib.constants import BASE_URL + + +class Datamaxi(API): + """Client to fetch unified data from DataMaxi+ API.""" + + def __init__(self, api_key=None, **kwargs: Any): + """Initialize the object. + + Args: + api_key (str): The DataMaxi+ API key + **kwargs: Keyword arguments used by `datamaxi.api.API`. + """ + if "base_url" not in kwargs: + kwargs["base_url"] = BASE_URL + + super().__init__(api_key, **kwargs) + + def symbols(self, exchange: str) -> List[str]: + """Supported symbols by given exchange + + `GET /v1/symbols` + + + + Args: + exchange (str): Exchange name + + Returns: + List of supported symbols + """ + check_required_parameter(exchange, "exchange") + params = {"exchange": exchange} + url_path = "/v1/symbols" + return self.query(url_path, params) + + def intervals(self, exchange: str) -> List[str]: + """Supported intervals by given exchange + + `GET /v1/intervals` + + + + Args: + exchange (str): Exchange name + + Returns: + List of supported intervals + """ + check_required_parameter(exchange, "exchange") + params = {"exchange": exchange} + url_path = "/v1/intervals" + return self.query(url_path, params) + + @postprocess() + def candle( + self, exchange: str, symbol: str, interval: str = "1d", pandas: bool = True + ) -> Union[List, pd.DataFrame]: + """Get candle data + + `GET /v1/candle` + + + + Args: + exchange (str): Exchange name + symbol (str): Symbol name + interval (str): Candle interval + pandas (bool): Return data as pandas DataFrame + + Returns: + Candle data for a given symbol and interval in pandas DataFrame + """ + check_required_parameters( + [[exchange, "exchange"], [symbol, "symbol"], [interval, "interval"]] + ) + params = {"exchange": exchange, "symbol": symbol, "interval": interval} + return self.query("/v1/candle", params) diff --git a/datamaxi/huobi/__init__.py b/datamaxi/huobi/__init__.py index c56ea0f..7f0ea72 100644 --- a/datamaxi/huobi/__init__.py +++ b/datamaxi/huobi/__init__.py @@ -22,7 +22,7 @@ def __init__(self, api_key=None, **kwargs: Any): super().__init__(api_key, **kwargs) def symbols(self) -> List[str]: - """Supported Huobi supported symbols + """Huobi supported symbols `GET /v1/raw/huobi/symbols` @@ -35,7 +35,7 @@ def symbols(self) -> List[str]: return self.query(url_path) def intervals(self) -> List[str]: - """Supported Huobi supported intervals + """Huobi supported intervals `GET /v1/raw/huobi/intervals` diff --git a/datamaxi/okx/__init__.py b/datamaxi/okx/__init__.py index 66eec24..c155adb 100644 --- a/datamaxi/okx/__init__.py +++ b/datamaxi/okx/__init__.py @@ -22,7 +22,7 @@ def __init__(self, api_key=None, **kwargs: Any): super().__init__(api_key, **kwargs) def symbols(self) -> List[str]: - """Supported Okx supported symbols + """Okx supported symbols `GET /v1/raw/okx/symbols` @@ -35,7 +35,7 @@ def symbols(self) -> List[str]: return self.query(url_path) def intervals(self) -> List[str]: - """Supported Okx supported intervals + """Okx supported intervals `GET /v1/raw/okx/intervals` diff --git a/datamaxi/upbit/__init__.py b/datamaxi/upbit/__init__.py index 2605234..6329f6b 100644 --- a/datamaxi/upbit/__init__.py +++ b/datamaxi/upbit/__init__.py @@ -22,7 +22,7 @@ def __init__(self, api_key=None, **kwargs: Any): super().__init__(api_key, **kwargs) def symbols(self) -> List[str]: - """Supported Upbit supported symbols + """Upbit supported symbols `GET /v1/raw/upbit/symbols` @@ -35,7 +35,7 @@ def symbols(self) -> List[str]: return self.query(url_path) def intervals(self) -> List[str]: - """Supported Upbit supported intervals + """Upbit supported intervals `GET /v1/raw/upbit/intervals`