Skip to content

Commit 981aa4a

Browse files
Resolve "spot.Trade.truncate not working for xStocks" (#404)
1 parent 4cedb97 commit 981aa4a

File tree

3 files changed

+150
-90
lines changed

3 files changed

+150
-90
lines changed

src/kraken/spot/market.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def get_asset_pairs(
152152
self: Market,
153153
pair: str | list[str] | None = None,
154154
info: str | None = None,
155+
aclass_base: str | None = None,
155156
*,
156157
extra_params: dict | None = None,
157158
) -> dict:
@@ -221,6 +222,8 @@ def get_asset_pairs(
221222
params["pair"] = pair
222223
if defined(info):
223224
params["info"] = info
225+
if defined(aclass_base):
226+
params["aclass_base"] = aclass_base
224227
return self.request( # type: ignore[return-value]
225228
method="GET",
226229
uri="/0/public/AssetPairs",

src/kraken/spot/trade.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def create_order( # pylint: disable=too-many-branches,too-many-arguments # noqa
108108
Requires the ``Create and modify orders`` permission in the API key
109109
settings.
110110
111+
If the traded asset is a tokenized asset, `extra_params` must contain
112+
the key `"asset_class"` with value `"tokenized_asset"`.
113+
111114
- https://docs.kraken.com/api/docs/rest-api/add-order
112115
113116
:param ordertype: The kind of the order, one of: ``market``, ``limit``,
@@ -335,7 +338,16 @@ def create_order( # pylint: disable=too-many-branches,too-many-arguments # noqa
335338
"volume": (
336339
volume
337340
if not truncate
338-
else self.truncate(amount=volume, amount_type="volume", pair=pair)
341+
else self.truncate(
342+
amount=volume,
343+
amount_type="volume",
344+
pair=pair,
345+
asset_class=(
346+
extra_params.get("asset_class", "currency")
347+
if extra_params
348+
else "currency"
349+
),
350+
)
339351
),
340352
"stptype": stptype,
341353
"starttm": starttm,
@@ -767,26 +779,33 @@ def truncate(
767779
amount: Decimal | float | str,
768780
amount_type: str,
769781
pair: str,
782+
asset_class: str = "currency",
770783
) -> str:
771784
"""
772-
Kraken only allows volume and price amounts to be specified with a specific number of
773-
decimal places, and these vary depending on the currency pair used.
785+
Kraken only allows volume and price amounts to be specified with a
786+
specific number of decimal places, and these vary depending on the
787+
currency pair used.
774788
775-
This function converts an amount of a specific type and pair to a string that uses
776-
the correct number of decimal places.
789+
This function converts an amount of a specific type and pair to a string
790+
that uses the correct number of decimal places.
777791
778792
This function uses caching. Run ``truncate.clear_cache()`` to clear.
779793
780794
:param amount: The floating point number to represent
781795
:type amount: Decimal | float | str
782-
:param amount_type: What the amount represents. Either ``"price"`` or ``"volume"``
796+
:param amount_type: What the amount represents. Either ``"price"`` or
797+
``"volume"``
783798
:type amount_type: str
784799
:param pair: The currency pair the amount is in reference to.
785800
:type pair: str
786-
:raises ValueError: If the ``amount_type`` is ``price`` and the price is less
787-
than the costmin.
788-
:raises ValueError: If the ``amount_type`` is ``volume`` and the volume is
789-
less than the ordermin.
801+
:param asset_class: The asset class of the base currency. Default is
802+
``"currency"``. If the traded asset is a tokenized asset, set this
803+
to ``"tokenized_asset"``.
804+
:type asset_class: str, optional
805+
:raises ValueError: If the ``amount_type`` is ``price`` and the price is
806+
less than the costmin.
807+
:raises ValueError: If the ``amount_type`` is ``volume`` and the volume
808+
is less than the ordermin.
790809
:raises ValueError: If no valid ``amount_type`` was passed.
791810
:return: A string representation of the amount.
792811
:rtype: str
@@ -826,7 +845,10 @@ def truncate(
826845
if amount_type not in {"price", "volume"}:
827846
raise ValueError("Amount type must be 'volume' or 'price'!")
828847

829-
pair_data: dict = self.__market.get_asset_pairs(pair=pair)
848+
pair_data: dict = self.__market.get_asset_pairs(
849+
pair=pair,
850+
aclass_base=asset_class,
851+
)
830852
data: dict = pair_data[next(iter(pair_data))]
831853

832854
pair_decimals: int = int(data["pair_decimals"])

0 commit comments

Comments
 (0)