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

Pagination #41

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
52 changes: 45 additions & 7 deletions datamaxi/binance/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import Any, List, Union
from typing import Any, Callable, Dict, Tuple, Union
import pandas as pd
from datamaxi.api import API
from datamaxi.lib.utils import check_required_parameters
from datamaxi.lib.utils import postprocess
from datamaxi.lib.constants import BASE_URL


Expand All @@ -21,12 +20,16 @@ def __init__(self, api_key=None, **kwargs: Any):

super().__init__(api_key, **kwargs)

@postprocess()
def funding_rate(
self,
symbol: str,
page: int = 1,
limit: int = 1000,
fromDateTime: str = None,
toDateTime: str = None,
sort: str = "desc",
pandas: bool = True,
) -> Union[List, pd.DataFrame]:
) -> Union[Tuple[Dict, Callable], Tuple[pd.DataFrame, Callable]]:
"""Get Binance funding rate data

`GET /v1/raw/binance/funding-rate`
Expand All @@ -35,12 +38,47 @@ def funding_rate(

Args:
symbol (str): Binance symbol
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:
Binance funding rate data for a given symbol in pandas DataFrame
Binance funding rate data for a given symbol in pandas DataFrame and next request function
"""
check_required_parameters([[symbol, "symbol"]])

params = {"symbol": symbol}
return self.query("/v1/raw/binance/funding-rate", params)
params = {
"symbol": symbol,
"page": page,
"limit": limit,
"fromDateTime": fromDateTime,
"toDateTime": toDateTime,
"sort": sort,
}

res = self.query("/v1/raw/binance/funding-rate", params)
if res["data"] is None:
raise ValueError("no data found")

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

if pandas:
df = pd.DataFrame(res["data"])
df = df.set_index("d")
df.replace("NaN", pd.NA, inplace=True)
df = df.apply(pd.to_numeric, errors="coerce")
return df, next_request
else:
return res, next_request
65 changes: 59 additions & 6 deletions datamaxi/datamaxi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Any, List, Union
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_parameter
from datamaxi.lib.utils import check_required_parameters
from datamaxi.lib.utils import postprocess
from datamaxi.lib.constants import BASE_URL


Expand Down Expand Up @@ -58,15 +57,19 @@ def intervals(self, exchange: str) -> List[str]:
url_path = "/v1/intervals"
return self.query(url_path, params)

@postprocess()
def candle(
self,
exchange: str,
symbol: str,
interval: str = "1d",
market: str = "spot",
page: int = 1,
limit: int = 1000,
fromDateTime: str = None,
toDateTime: str = None,
sort: str = "desc",
pandas: bool = True,
) -> Union[List, pd.DataFrame]:
) -> Union[Tuple[Dict, Callable], Tuple[pd.DataFrame, Callable]]:
"""Get candle data

`GET /v1/candle`
Expand All @@ -78,10 +81,15 @@ def candle(
symbol (str): Symbol name
interval (str): Candle interval
market (str): Market type (spot/futures)
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:
Candle data for a given symbol, interval and market in pandas DataFrame
Candle data for a given symbol, interval and market in pandas DataFrame and next request function
"""
check_required_parameters(
[
Expand All @@ -95,10 +103,55 @@ def candle(
if market not in ["spot", "futures"]:
raise ValueError("market must be either spot or futures")

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,
"interval": interval,
"market": market,
"page": page,
"limit": limit,
"fromDateTime": fromDateTime,
"toDateTime": toDateTime,
"sort": sort,
}
return self.query("/v1/candle", params)

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

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

if pandas:
df = pd.DataFrame(res["data"])
df = df.set_index("d")
df.replace("NaN", pd.NA, inplace=True)
df = df.apply(pd.to_numeric, errors="coerce")
return df, next_request
else:
return res, next_request
35 changes: 0 additions & 35 deletions tests/binance/test_binance_funding_rate.py

This file was deleted.

Loading