Add support to new calculation endpoints.

This commit is contained in:
itsdeka
2023-01-24 12:35:12 +01:00
committed by Davide Casale
parent ae42fb7d93
commit 02a2e962d3
4 changed files with 62 additions and 2 deletions

View File

@@ -62,13 +62,13 @@ class _Requests(object):
return data
def _POST(self, endpoint, params = None, data = None, _append_authentication_headers = True):
def _POST(self, endpoint, params = None, data = None):
headers = { "Content-Type": "application/json" }
if isinstance(data, dict):
data = json.dumps({ key: value for key, value in data.items() if value != None}, cls=JSONEncoder)
if _append_authentication_headers:
if self.API_KEY and self.API_SECRET:
headers = { **headers, **self.__build_authentication_headers(endpoint, data) }
response = requests.post(f"{self.host}/{endpoint}", params=params, data=data, headers=headers)
@@ -268,6 +268,21 @@ class _RestPublicEndpoints(_Requests):
return messages
def get_trading_market_average_price(self, symbol: str, amount: Union[Decimal, str], price_limit: Optional[Union[Decimal, str]] = None) -> TradingMarketAveragePrice:
data = {
"symbol": symbol, "amount": amount, "price_limit": price_limit
}
return serializers.TradingMarketAveragePrice.parse(*self._POST("calc/trade/avg", data=data))
def get_funding_market_average_price(self, symbol: str, amount: Union[Decimal, str], period: int, rate_limit: Optional[Union[Decimal, str]] = None) -> FundingMarketAveragePrice:
data = {
"symbol": symbol, "amount": amount, "period": period,
"rate_limit": rate_limit
}
return serializers.FundingMarketAveragePrice.parse(*self._POST("calc/trade/avg", data=data))
class _RestAuthenticatedEndpoints(_Requests):
def get_wallets(self) -> List[Wallet]:
return [ serializers.Wallet.parse(*subdata) for subdata in self._POST("auth/r/wallets") ]

View File

@@ -230,6 +230,17 @@ PulseMessage = generate_recursive_serializer("PulseMessage", klass=types.PulseMe
"_PLACEHOLDER"
])
TradingMarketAveragePrice = generate_labeler_serializer("TradingMarketAveragePrice", klass=types.TradingMarketAveragePrice, labels=[
"PRICE_AVG",
"AMOUNT"
])
FundingMarketAveragePrice = generate_labeler_serializer("FundingMarketAveragePrice", klass=types.FundingMarketAveragePrice, labels=[
"RATE_AVG",
"AMOUNT"
])
#endregion
#region Serializers definition for Rest Authenticated Endpoints

View File

@@ -181,6 +181,16 @@ class PulseMessage(_Type):
PROFILE: PulseProfile
COMMENTS: int
@dataclass
class TradingMarketAveragePrice(_Type):
PRICE_AVG: float
AMOUNT: float
@dataclass
class FundingMarketAveragePrice(_Type):
RATE_AVG: float
AMOUNT: float
#endregion
#region Type hinting for Rest Authenticated Endpoints

View File

@@ -0,0 +1,24 @@
# python -c "from examples.rest.extra_calcs import *"
from bfxapi.client import Client, Constants
bfx = Client(
REST_HOST=Constants.REST_HOST
)
t_symbol_response = bfx.rest.public.get_trading_market_average_price(
symbol="tBTCUSD",
amount=-100,
price_limit="20000.5"
)
print(t_symbol_response)
f_symbol_response = bfx.rest.public.get_funding_market_average_price(
symbol="fUSD",
amount=100,
period=2,
rate_limit="0.00015"
)
print(f_symbol_response)