From 3565811ec92b60ffdc32c09c9427e38bc885ecab Mon Sep 17 00:00:00 2001 From: itsdeka Date: Wed, 25 Jan 2023 20:46:55 +0100 Subject: [PATCH] position history --- bfxapi/rest/BfxRestInterface.py | 15 ++++++++------- bfxapi/rest/serializers.py | 17 +++++++++++++++++ bfxapi/rest/types.py | 12 ++++++++++++ examples/rest/get_positions_history.py | 17 +++++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 examples/rest/get_positions_history.py diff --git a/bfxapi/rest/BfxRestInterface.py b/bfxapi/rest/BfxRestInterface.py index 697784c..84ca7f2 100644 --- a/bfxapi/rest/BfxRestInterface.py +++ b/bfxapi/rest/BfxRestInterface.py @@ -45,8 +45,6 @@ class _Requests(object): def _GET(self, endpoint, params = None): response = requests.get(f"{self.host}/{endpoint}", params=params) - - print(f"{self.host}/{endpoint}") if response.status_code == HTTPStatus.NOT_FOUND: raise ResourceNotFound(f"No resources found at endpoint <{endpoint}>.") @@ -487,9 +485,9 @@ class _RestAuthenticatedEndpoints(_Requests): return [ serializers.Movement.parse(*sub_data) for sub_data in self._POST(endpoint, data=data) ] def get_symbol_margin_info(self, symbol: str) -> SymbolMarginInfo: - data = self._POST(f"auth/r/info/margin/{symbol}") + response = self._POST(f"auth/r/info/margin/{symbol}") - return serializers.SymbolMarginInfo.parse(*([data[1]] + data[2])) + return serializers.SymbolMarginInfo.parse(*([response[1]] + response[2])) def get_all_symbols_margin_info(self) -> List[SymbolMarginInfo]: return [ serializers.SymbolMarginInfo.parse(*([sub_data[1]] + sub_data[2])) for sub_data in self._POST(f"auth/r/info/margin/sym_all") ] @@ -501,11 +499,14 @@ class _RestAuthenticatedEndpoints(_Requests): return serializers._Notification[Claim](serializer=serializers.Claim).parse(*self._POST("auth/w/position/claim", data={ "id": id, "amount": amount })) def get_increase_position_info(self, symbol: str, amount: Union[Decimal, float, str]) -> IncreaseInfo: - data = self._POST(f"auth/r/position/increase/info", data={ "symbol": symbol, "amount": amount }) + response = self._POST(f"auth/r/position/increase/info", data={ "symbol": symbol, "amount": amount }) return serializers.IncreaseInfo.parse(*( - data[0] + [data[1][0]] + data[1][1] + [data[1][2]] + data[4] + data[5] + response[0] + [response[1][0]] + response[1][1] + [response[1][2]] + response[4] + response[5] )) def increase_position(self, symbol: str, amount: Union[Decimal, float, str]) -> Notification[Increase]: - return serializers._Notification[Increase](serializer=serializers.Increase).parse(*self._POST("auth/w/position/increase", data={ "symbol": symbol, "amount": amount })) \ No newline at end of file + return serializers._Notification[Increase](serializer=serializers.Increase).parse(*self._POST("auth/w/position/increase", data={ "symbol": symbol, "amount": amount })) + + def get_position_history(self, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[PositionHistory]: + return [ serializers.PositionHistory.parse(*sub_data) for sub_data in self._POST("auth/r/positions/hist", data={ "start": start, "end": end, "limit": limit }) ] diff --git a/bfxapi/rest/serializers.py b/bfxapi/rest/serializers.py index cb4bb39..57ab4eb 100644 --- a/bfxapi/rest/serializers.py +++ b/bfxapi/rest/serializers.py @@ -537,4 +537,21 @@ Increase = generate_labeler_serializer("Increase", klass=types.Increase, labels= "BASE_PRICE" ]) +PositionHistory = generate_labeler_serializer("PositionHistory", klass=types.PositionHistory, labels=[ + "SYMBOL", + "STATUS", + "AMOUNT", + "BASE_PRICE", + "FUNDING", + "FUNDING_TYPE", + "_PLACEHOLDER", + "_PLACEHOLDER", + "_PLACEHOLDER", + "_PLACEHOLDER", + "_PLACEHOLDER", + "POSITION_ID", + "MTS_CREATE", + "MTS_UPDATE" +]) + #endregion \ No newline at end of file diff --git a/bfxapi/rest/types.py b/bfxapi/rest/types.py index b2c6d79..6f27c0c 100644 --- a/bfxapi/rest/types.py +++ b/bfxapi/rest/types.py @@ -425,4 +425,16 @@ class Increase(_Type): AMOUNT: float BASE_PRICE: float +@dataclass +class PositionHistory(_Type): + SYMBOL: str + STATUS: str + AMOUNT: float + BASE_PRICE: float + FUNDING: float + FUNDING_TYPE: int + POSITION_ID: int + MTS_CREATE: int + MTS_UPDATE: int + #endregion \ No newline at end of file diff --git a/examples/rest/get_positions_history.py b/examples/rest/get_positions_history.py new file mode 100644 index 0000000..87a4b24 --- /dev/null +++ b/examples/rest/get_positions_history.py @@ -0,0 +1,17 @@ +# python -c "import examples.rest.get_positions_history" + +import os +import time + +from bfxapi.client import Client, Constants + +bfx = Client( + REST_HOST=Constants.REST_HOST, + API_KEY=os.getenv("BFX_API_KEY"), + API_SECRET=os.getenv("BFX_API_SECRET") +) + +now = int(round(time.time() * 1000)) + +positions_history = bfx.rest.auth.get_position_history(end=now, limit=50) +print(positions_history)