Skip to content

Commit

Permalink
Merge branch 'master' into 198-add-nft-support-krakennfttrade-and-kra…
Browse files Browse the repository at this point in the history
…kennftmarket
  • Loading branch information
btschwertfeger committed Mar 10, 2024
2 parents be91d0e + 8387327 commit 11c50d9
Show file tree
Hide file tree
Showing 19 changed files with 739 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pytest.xml
*.pot

# Sphinx documentation
docs/_build/
doc/_build/

# PyBuilder
target/
Expand Down
5 changes: 5 additions & 0 deletions doc/src/spot/rest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ Spot REST
:show-inheritance:
:inherited-members:

.. autoclass:: kraken.spot.Earn
:members:
:show-inheritance:
:inherited-members:

.. autoclass:: kraken.spot.Staking
:members:
:show-inheritance:
Expand Down
1 change: 0 additions & 1 deletion kraken/base_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ def _request( # noqa: PLR0913 # pylint: disable=too-many-arguments
if isinstance(extra_params, str)
else extra_params
)

query_params: str = (
urlencode(params, doseq=True)
if METHOD in {"GET", "DELETE"} and params
Expand Down
48 changes: 48 additions & 0 deletions kraken/exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,46 @@ class KrakenTemporaryLockoutError(Exception):
"""The account was temporary locked out."""


@docstring_message
class KrakenEarnMinimumAllocationError(Exception):
"""(De)allocation operation amount less than minimum"""


@docstring_message
class KrakenEarnAllocationInProgressError(Exception):
"""Another allocation is already in progress"""


@docstring_message
class KrakenEarnTemporaryUnavailableError(Exception):
"""The Earn service is temporary unavailable, try again in a few minutes"""


@docstring_message
class KrakenEarnTierVerificationError(Exception):
"""The user's tier is not high enough"""


@docstring_message
class KrakenEarnStrategyNotFoundError(Exception):
"""Strategy not found"""


@docstring_message
class KrakenEarnInsufficientFundsError(Exception):
"""Insufficient funds to complete the transaction"""


@docstring_message
class KrakenEarnAllocationExceededError(Exception):
"""The allocation exceeds user limit for the strategy"""


@docstring_message
class KrakenEarnDeallocationExceededError(Exception):
"""The deallocation exceeds user limit for the strategy"""


@docstring_message
class KrakenMaxFeeExceededError(Exception):
"""The fee was higher than the defined maximum."""
Expand Down Expand Up @@ -366,6 +406,14 @@ class MaxReconnectError(Exception):
# "WDatabase:No change": ,
# Futures Trading Errors
#
"EEarnings:Below min:(De)allocation operation amount less than minimum": KrakenEarnMinimumAllocationError,
"EEarnings:Busy:Another (de)allocation for the same strategy is in progress": KrakenEarnAllocationInProgressError,
"EEarnings:Busy": KrakenEarnTemporaryUnavailableError,
"EEarnings:Permission denied:The user's tier is not high enough": KrakenEarnTierVerificationError,
"EGeneral:Invalid arguments:Invalid strategy ID": KrakenEarnStrategyNotFoundError,
"EEarnings:Insufficient funds:Insufficient funds to complete the (de)allocation request": KrakenEarnInsufficientFundsError,
"EEarnings:Above max:The allocation exceeds user limit for the strategy": KrakenEarnAllocationExceededError,
"EEarnings:Above max:The allocation exceeds the total strategy limit": KrakenEarnDeallocationExceededError,
"authenticationError": KrakenAuthenticationError,
"insufficientAvailableFunds": KrakenInsufficientAvailableFundsError,
"requiredArgumentMissing": KrakenRequiredArgumentMissingError,
Expand Down
62 changes: 61 additions & 1 deletion kraken/futures/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def get_fills(
def create_batch_order(
self: Trade,
batchorder_list: list[dict],
processBefore: Optional[str] = None,
*,
extra_params: Optional[dict] = None,
) -> dict:
Expand All @@ -133,6 +134,8 @@ def create_batch_order(
:param batchorder_list: List of order instructions (see example below -
or the linked official Kraken documentation)
:type batchorder_list: list[dict]
:param processBefore: Process before timestamp otherwise reject
:type processBefore: str, optional
:return: Information about the submitted request
:rtype: dict
Expand Down Expand Up @@ -223,10 +226,14 @@ def create_batch_order(
}
"""
batchorder: dict = {"batchOrder": batchorder_list}
params = {"json": f"{batchorder}"}
if processBefore:
params["processBefore"] = processBefore

return self._request( # type: ignore[return-value]
method="POST",
uri="/derivatives/api/v3/batchorder",
post_params={"json": f"{batchorder}"},
post_params=params,
auth=True,
extra_params=extra_params,
)
Expand Down Expand Up @@ -335,6 +342,7 @@ def cancel_order(
self: Trade,
order_id: Optional[str] = None,
cliOrdId: Optional[str] = None,
processBefore: Optional[str] = None,
*,
extra_params: Optional[dict] = None,
) -> dict:
Expand All @@ -351,6 +359,8 @@ def cancel_order(
:type order_id: str, optional
:param cliOrdId: The client defined order id
:type cliOrdId: str, optional
:param processBefore: Process before timestamp otherwise reject
:type processBefore: str, optional
:raises ValueError: If both ``order_id`` and ``cliOrdId`` are not set
:return: Success or failure
:rtype: dict
Expand All @@ -377,6 +387,8 @@ def cancel_order(
params["order_id"] = order_id
elif defined(cliOrdId):
params["cliOrdId"] = cliOrdId
elif defined(processBefore):
params["processBefore"] = processBefore
else:
raise ValueError("Either order_id or cliOrdId must be set!")

Expand All @@ -395,6 +407,7 @@ def edit_order(
limitPrice: Optional[str | float] = None,
size: Optional[str | float] = None,
stopPrice: Optional[str | float] = None,
processBefore: Optional[str] = None,
*,
extra_params: Optional[dict] = None,
) -> dict:
Expand All @@ -416,6 +429,8 @@ def edit_order(
:type size: str | float, optional
:param stopPrice: The stop price
:type stopPrice: str | float, optional
:param processBefore: Process before timestamp otherwise reject
:type processBefore: str, optional
:raises ValueError: If both ``orderId`` and ``cliOrdId`` are not set
:return: Success or failure
:rtype: dict
Expand Down Expand Up @@ -453,6 +468,8 @@ def edit_order(
params["size"] = size
if defined(stopPrice):
params["stopPrice"] = stopPrice
if defined(processBefore):
params["processBefore"] = processBefore

return self._request( # type: ignore[return-value]
method="POST",
Expand Down Expand Up @@ -524,6 +541,7 @@ def create_order( # pylint: disable=too-many-arguments # noqa: PLR0913, PLR0917
triggerSignal: Optional[str] = None,
trailingStopDeviationUnit: Optional[str] = None,
trailingStopMaxDeviation: Optional[str] = None,
processBefore: Optional[str] = None,
*,
extra_params: Optional[dict] = None,
) -> dict:
Expand Down Expand Up @@ -561,6 +579,8 @@ def create_order( # pylint: disable=too-many-arguments # noqa: PLR0913, PLR0917
:type trailingStopDeviationUnit: str, optional
:param trailingStopMaxDeviation: See referenced Kraken documentation
:type trailingStopMaxDeviation: str, optional
:param processBefore: Process before timestamp otherwise reject
:type processBefore: str, optional
:return: Success or failure
:rtype: dict
Expand Down Expand Up @@ -721,6 +741,8 @@ def create_order( # pylint: disable=too-many-arguments # noqa: PLR0913, PLR0917
params["trailingStopDeviationUnit"] = trailingStopDeviationUnit
if defined(trailingStopMaxDeviation):
params["trailingStopMaxDeviation"] = trailingStopMaxDeviation
if defined(processBefore):
params["processBefore"] = processBefore

return self._request( # type: ignore[return-value]
method="POST",
Expand All @@ -730,5 +752,43 @@ def create_order( # pylint: disable=too-many-arguments # noqa: PLR0913, PLR0917
extra_params=extra_params,
)

def get_max_order_size(
self: Trade,
orderType: str,
symbol: str,
limitPrice: Optional[float] = None,
*,
extra_params: Optional[dict] = None,
) -> dict:
"""
Retrieve the maximum order price for a specific symbol. Can be adjusted
by ``limitPrice``. This endpoint only supports multi-collateral futures.
Requires at least the ``General API - Read Access`` permission in the
API key settings.
- https://docs.futures.kraken.com/#http-api-trading-v3-api-order-management-get-maximum-order-size
:param orderType: ``lmt`` or ``mkt``
:type orderType: str
:param symbol: The symbol to filter for
:type symbol: str
:param limitPrice: Limit price if ``orderType == lmt`` , defaults to
None
:type limitPrice: Optional[float], optional
"""
params: dict = {"orderType": orderType, "symbol": symbol}

if defined(limitPrice) and orderType == "lmt":
params["limitPrice"] = limitPrice

return self._request( # type: ignore[return-value]
method="GET",
uri="/derivatives/api/v3/initialmargin/maxordersize",
query_params=params,
auth=True,
extra_params=extra_params,
)


__all__ = ["Trade"]
4 changes: 3 additions & 1 deletion kraken/spot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
# GitHub: https://github.com/btschwertfeger
# pylint: disable=unused-import,cyclic-import

"""Module that provides the Spot REST clients and utility functions."""
"""Module that provides the Spot REST clients."""

from kraken.spot.earn import Earn
from kraken.spot.funding import Funding
from kraken.spot.market import Market
from kraken.spot.orderbook_v1 import OrderbookClientV1
Expand All @@ -17,6 +18,7 @@
from kraken.spot.websocket_v2 import KrakenSpotWSClientV2

__all__ = [
"Earn",
"Funding",
"KrakenSpotWSClientV1",
"KrakenSpotWSClientV2",
Expand Down
Loading

0 comments on commit 11c50d9

Please sign in to comment.