From 48433fbb01085b772f696c15cfa75f62801bcfed Mon Sep 17 00:00:00 2001 From: itsdeka Date: Thu, 26 Jan 2023 16:44:32 +0100 Subject: [PATCH] derivatives --- bfxapi/rest/BfxRestInterface.py | 6 ++++++ bfxapi/rest/serializers.py | 9 +++++++++ bfxapi/rest/types.py | 9 +++++++++ examples/rest/derivatives.py | 31 +++++++++++++++++++++++++++++++ examples/rest/get_positions.py | 2 +- 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 examples/rest/derivatives.py diff --git a/bfxapi/rest/BfxRestInterface.py b/bfxapi/rest/BfxRestInterface.py index 1853388..bf4e1cf 100644 --- a/bfxapi/rest/BfxRestInterface.py +++ b/bfxapi/rest/BfxRestInterface.py @@ -519,3 +519,9 @@ class _RestAuthenticatedEndpoints(_Requests): def get_positions_audit(self, ids: Optional[List[int]] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[PositionAudit]: return [ serializers.PositionAudit.parse(*sub_data) for sub_data in self._POST("auth/r/positions/audit", data={ "ids": ids, "start": start, "end": end, "limit": limit }) ] + + def set_derivative_position_collateral(self, symbol: str, collateral: Union[Decimal, float, str]) -> DerivativePositionCollateral: + return serializers.DerivativePositionCollateral.parse(*(self._POST("auth/w/deriv/collateral/set", data={ "symbol": symbol, "collateral": collateral })[0])) + + def get_derivative_position_collateral_limits(self, symbol: str) -> DerivativePositionCollateralLimits: + return serializers.DerivativePositionCollateralLimits.parse(*self._POST("auth/calc/deriv/collateral/limits", data={ "symbol": symbol })) diff --git a/bfxapi/rest/serializers.py b/bfxapi/rest/serializers.py index 95c7f63..32d2d44 100644 --- a/bfxapi/rest/serializers.py +++ b/bfxapi/rest/serializers.py @@ -594,4 +594,13 @@ PositionAudit = generate_labeler_serializer("PositionAudit", klass=types.Positio "META" ]) +DerivativePositionCollateral = generate_labeler_serializer("DerivativePositionCollateral", klass=types.DerivativePositionCollateral, labels=[ + "STATUS" +]) + +DerivativePositionCollateralLimits = generate_labeler_serializer("DerivativePositionCollateralLimits", klass=types.DerivativePositionCollateralLimits, labels=[ + "MIN_COLLATERAL", + "MAX_COLLATERAL" +]) + #endregion \ No newline at end of file diff --git a/bfxapi/rest/types.py b/bfxapi/rest/types.py index 0c47c53..4f2647f 100644 --- a/bfxapi/rest/types.py +++ b/bfxapi/rest/types.py @@ -465,4 +465,13 @@ class PositionAudit(_Type): COLLATERAL_MIN: float META: JSON +@dataclass +class DerivativePositionCollateral(_Type): + STATUS: int + +@dataclass +class DerivativePositionCollateralLimits(_Type): + MIN_COLLATERAL: float + MAX_COLLATERAL: float + #endregion \ No newline at end of file diff --git a/examples/rest/derivatives.py b/examples/rest/derivatives.py new file mode 100644 index 0000000..89865a5 --- /dev/null +++ b/examples/rest/derivatives.py @@ -0,0 +1,31 @@ +# python -c "import examples.rest.derivatives" + +import os + +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") +) + +# Create a new order +submitted_order = bfx.rest.auth.submit_order( + symbol="tBTCF0:USTF0", + amount="0.015", + price="16700", + lev=10, + type="LIMIT" +) + +print("Submit Order Notification:", submitted_order) + +# Get position collateral limits +limits = bfx.rest.auth.get_derivative_position_collateral_limits(symbol="tBTCF0:USTF0") +print(f"Limits {limits}") + +# Update position collateral +response = bfx.rest.auth.set_derivative_position_collateral(symbol="tBTCF0:USTF0", collateral=50) +print(response.STATUS) + diff --git a/examples/rest/get_positions.py b/examples/rest/get_positions.py index 5b6cfca..7e71824 100644 --- a/examples/rest/get_positions.py +++ b/examples/rest/get_positions.py @@ -1,4 +1,4 @@ -# python -c "import examples.rest.get_positions_snapshot" +# python -c "import examples.rest.get_positions" import os import time