Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DexTrade client (v0.13.0) #46

Merged
merged 6 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datamaxi/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.12.0"
__version__ = "0.13.0"
2 changes: 2 additions & 0 deletions datamaxi/datamaxi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datamaxi.lib.constants import BASE_URL
from datamaxi.datamaxi.candle import Candle
from datamaxi.datamaxi.funding_rate import FundingRate
from datamaxi.datamaxi.dex_trade import DexTrade


class Datamaxi:
Expand All @@ -19,3 +20,4 @@ def __init__(self, api_key=None, **kwargs: Any):

self.candle = Candle(api_key, **kwargs)
self.funding_rate = FundingRate(api_key, **kwargs)
self.dex_trade = DexTrade(api_key, **kwargs)
2 changes: 1 addition & 1 deletion datamaxi/datamaxi/candle.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def get(
raise ValueError("no data found")

def next_request():
return self.candle(
return self.get(
exchange,
symbol,
interval,
Expand Down
115 changes: 115 additions & 0 deletions datamaxi/datamaxi/dex_trade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from typing import Any, Callable, Tuple, List, Dict, Union
import pandas as pd
from datamaxi.api import API
from datamaxi.lib.utils import check_required_parameters
from datamaxi.datamaxi.utils import convert_data_to_data_frame


class DexTrade(API):
"""Client to fetch DEX trade data from DataMaxi+ API."""

def __init__(self, api_key=None, **kwargs: Any):
"""Initialize DEX trade client.

Args:
api_key (str): The DataMaxi+ API key
**kwargs: Keyword arguments used by `datamaxi.api.API`.
"""
super().__init__(api_key, **kwargs)

def get(
self,
exchange: str,
symbol: str,
page: int = 1,
limit: int = 1000,
fromDateTime: str = None,
toDateTime: str = None,
sort: str = "desc",
pandas: bool = True,
) -> Union[Tuple[Dict, Callable], Tuple[pd.DataFrame, Callable]]:
"""Fetch DEX trade data

`GET /v1/dex/trade`

<https://docs.datamaxiplus.com/api/datasets/dex-trade/trade>

Args:
exchange (str): Exchange name
symbol (str): Symbol name
page (int): Page number
limit (int): Limit of data
fromDateTime (str): Start date and time (accepts format "2006-01-02 15:04:05" or "2006-01-02")
toDateTime (str): End date and time (accepts format "2006-01-02 15:04:05" or "2006-01-02")
sort (str): Sort order
pandas (bool): Return data as pandas DataFrame

Returns:
DEX trade data in pandas DataFrame and next request function
"""
check_required_parameters(
[
[exchange, "exchange"],
[symbol, "symbol"],
]
)
if page < 1:
raise ValueError("page must be greater than 0")

if limit < 1:
raise ValueError("limit must be greater than 0")

if fromDateTime is not None and toDateTime is not None:
raise ValueError(
"fromDateTime and toDateTime cannot be set at the same time"
)

if sort not in ["asc", "desc"]:
raise ValueError("sort must be either asc or desc")

params = {
"exchange": exchange,
"symbol": symbol,
"page": page,
"limit": limit,
"fromDateTime": fromDateTime,
"toDateTime": toDateTime,
"sort": sort,
}

res = self.query("/v1/dex/trade", params)
if res["data"] is None:
raise ValueError("no data found")

def next_request():
return self.get(
exchange,
symbol,
page + 1,
limit,
fromDateTime,
toDateTime,
sort,
pandas,
)

if pandas:
df = convert_data_to_data_frame(res["data"], ["b", "bq", "qq", "p", "usd"])
return df, next_request
else:
return res, next_request

def exchanges(self) -> List[str]:
"""Fetch supported exchanges accepted by
[datamaxi.DexTrade.get](./#datamaxi.datamaxi.DexTrade.get)
API.

`GET /v1/dex/trade/exchanges`

<https://docs.datamaxiplus.com/api/datasets/dex-trade/exchanges>

Returns:
List of supported exchanges
"""
url_path = "/v1/dex/trade/exchanges"
return self.query(url_path)
2 changes: 1 addition & 1 deletion datamaxi/datamaxi/funding_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get(
raise ValueError("no data found")

def next_request():
return self.funding_rate(
return self.get(
exchange,
symbol,
page + 1,
Expand Down
18 changes: 15 additions & 3 deletions datamaxi/datamaxi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@
import pandas as pd


def convert_data_to_data_frame(data: List) -> pd.DataFrame:
def convert_data_to_data_frame(
data: List,
columns_to_replace: List[str] = [],
) -> pd.DataFrame:
df = pd.DataFrame(data)
df = df.set_index("d")
df.replace("NaN", pd.NA, inplace=True)
df = df.apply(pd.to_numeric, errors="coerce")

if len(columns_to_replace) == 0:
df.replace("NaN", pd.NA, inplace=True)
df = df.apply(pd.to_numeric, errors="coerce")
return df

df[columns_to_replace] = df[columns_to_replace].replace("NaN", pd.NA)
df[columns_to_replace] = df[columns_to_replace].apply(
pd.to_numeric, errors="coerce"
)

return df
6 changes: 6 additions & 0 deletions docs/dex-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# DEX Trade

::: datamaxi.datamaxi.DexTrade
options:
show_submodules: true
show_source: false
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ nav:
- Datamaxi:
- Candle: candle.md
- Funding Rate: funding-rate.md
- DEX Trade: dex-trade.md
- Defillama: defillama.md
- Trend:
- Naver Trend: naver-trend.md
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "datamaxi"
version = "0.12.0"
version = "0.13.0"
authors = [
{ name="Bisonai", email="[email protected]" },
]
Expand Down
Loading