Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/kraken/spot/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def get_asset_pairs(
self: Market,
pair: str | list[str] | None = None,
info: str | None = None,
aclass_base: str | None = None,
*,
extra_params: dict | None = None,
) -> dict:
Expand Down Expand Up @@ -221,6 +222,8 @@ def get_asset_pairs(
params["pair"] = pair
if defined(info):
params["info"] = info
if defined(aclass_base):
params["aclass_base"] = aclass_base
return self.request( # type: ignore[return-value]
method="GET",
uri="/0/public/AssetPairs",
Expand Down
44 changes: 33 additions & 11 deletions src/kraken/spot/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def create_order( # pylint: disable=too-many-branches,too-many-arguments # noqa
Requires the ``Create and modify orders`` permission in the API key
settings.

If the traded asset is a tokenized asset, `extra_params` must contain
the key `"asset_class"` with value `"tokenized_asset"`.

- https://docs.kraken.com/api/docs/rest-api/add-order

:param ordertype: The kind of the order, one of: ``market``, ``limit``,
Expand Down Expand Up @@ -335,7 +338,16 @@ def create_order( # pylint: disable=too-many-branches,too-many-arguments # noqa
"volume": (
volume
if not truncate
else self.truncate(amount=volume, amount_type="volume", pair=pair)
else self.truncate(
amount=volume,
amount_type="volume",
pair=pair,
asset_class=(
extra_params.get("asset_class", "currency")
if extra_params
else "currency"
),
)
),
"stptype": stptype,
"starttm": starttm,
Expand Down Expand Up @@ -767,26 +779,33 @@ def truncate(
amount: Decimal | float | str,
amount_type: str,
pair: str,
asset_class: str = "currency",
) -> str:
"""
Kraken only allows volume and price amounts to be specified with a specific number of
decimal places, and these vary depending on the currency pair used.
Kraken only allows volume and price amounts to be specified with a
specific number of decimal places, and these vary depending on the
currency pair used.

This function converts an amount of a specific type and pair to a string that uses
the correct number of decimal places.
This function converts an amount of a specific type and pair to a string
that uses the correct number of decimal places.

This function uses caching. Run ``truncate.clear_cache()`` to clear.

:param amount: The floating point number to represent
:type amount: Decimal | float | str
:param amount_type: What the amount represents. Either ``"price"`` or ``"volume"``
:param amount_type: What the amount represents. Either ``"price"`` or
``"volume"``
:type amount_type: str
:param pair: The currency pair the amount is in reference to.
:type pair: str
:raises ValueError: If the ``amount_type`` is ``price`` and the price is less
than the costmin.
:raises ValueError: If the ``amount_type`` is ``volume`` and the volume is
less than the ordermin.
:param asset_class: The asset class of the base currency. Default is
``"currency"``. If the traded asset is a tokenized asset, set this
to ``"tokenized_asset"``.
:type asset_class: str, optional
:raises ValueError: If the ``amount_type`` is ``price`` and the price is
less than the costmin.
:raises ValueError: If the ``amount_type`` is ``volume`` and the volume
is less than the ordermin.
:raises ValueError: If no valid ``amount_type`` was passed.
:return: A string representation of the amount.
:rtype: str
Expand Down Expand Up @@ -826,7 +845,10 @@ def truncate(
if amount_type not in {"price", "volume"}:
raise ValueError("Amount type must be 'volume' or 'price'!")

pair_data: dict = self.__market.get_asset_pairs(pair=pair)
pair_data: dict = self.__market.get_asset_pairs(
pair=pair,
aclass_base=asset_class,
)
data: dict = pair_data[next(iter(pair_data))]

pair_decimals: int = int(data["pair_decimals"])
Expand Down
Loading
Loading