krakenexapi.wallet

Currency

They are intended to work as singletons, and therefore track of registered/created instances.

class krakenexapi.wallet.Currency(symbol: str, name: str, decimals: int, display_decimals: int, letter: Optional[str] = None, description: Optional[str] = None)[source]

Immutable singleton currency info dataclass.

Raises

AssertionError – If trying to create a new Currency singleton instance with an already registered symbol identifier.

symbol: str

Currency symbol used by Kraken Exchange.

name: str

Alternative currency symbol. (can be same as symbol, often without X/Z prefix)

decimals: int

Number of decimals used in computation (precision).

display_decimals: int

Number of decimals displayed to user.

letter: Optional[str] = None

Optional. Currency symbol/glyph.

description: Optional[str] = None

Optional. Short currency description/name.

property is_fiat

Returns True if currency is fiat, False if crypto.

Returns

boolTrue if fiat currency.

property is_staked_onchain

On-chain staked currency.

Returns

bool

property is_staked_offchain

Off-chain staked currency.

Returns

bool

property is_staked

Is a currency, staked on Kraken Exchange.

Returns

bool

format_value(value: float) → str[source]

Formats a given value according to the display_decimals of the currency.

If a currency symbol/letter exists, append it.

Parameters

value (float) – The value to be formatted.

Returns

str – Formatted string.

round_value(value: float) → float[source]

Round a given value to the maximum number of digits as specified in decimals of the currency.

Parameters

value (float) – Number to be rounded.

Returns

float – Number rounded to decimals digits.

classmethod find(symbol: str)krakenexapi.wallet.Currency[source]

Finds the singleton instance of the currency described by the symbol (Kraken Exchange identifier) string.

Returns

krakenexapi.wallet.Currency – Currency singleton instance.

Raises

KeyError – If no Currency exists for the symbol.

classmethod all_symbols(unique: bool = True) → Set[str][source]

Return a list of all instanciated Currency symbols.

This allows the retrieval of all Currency instances via find() (with/without) duplicates.

Parameters

unique (bool, optional) – Whethere the list of symbols allows for duplicate when used for retrieval, or not, by default True (no duplicates)

Returns

Set[str] – Currency symbol names (used on Kraken Exchange)

classmethod build_from_api(api: krakenexapi.api.BasicKrakenExAPIPublicMethods)[source]

Uses the api to query a list of all currencies on Kraken Exchange and builds singleton instances for each of it.

Parameters

api (BasicKrakenExAPIPublicMethods) – An API that allows to query public Kraken Exchange endpoints.

class krakenexapi.wallet.CurrencyPair(symbol: str, altname: str, name: str, pair_decimals: int, base: krakenexapi.wallet.Currency, quote: krakenexapi.wallet.Currency, ordermin: Optional[float] = None)[source]

Immutable singleton currency trading pair info dataclass.

Raises

AssertionError – If trying to create a new CurrencyPair singleton instance with an already registered symbol identifier.

symbol: str

Trading pair symbol (used on Kraken Exchange)

altname: str

Alternative name for trading pair.

name: str

Visual name (human readable).

base: krakenexapi.wallet.Currency

Base currency

quote: krakenexapi.wallet.Currency

Quote currency, determines value/price for base currency.

ordermin: Optional[float] = None

Minimum amount of base currency for a new order.

property is_fiat2crypto

Trading pair between crypto and fiat currency.

Returns

bool

property is_crypto2crypto

Trading pair between two crypto currencies.

Returns

bool

property is_fiat2fiat

Trading pair between two fiat currencies.

Returns

bool

classmethod find(symbol: str)krakenexapi.wallet.CurrencyPair[source]

Find the singleton CurrencyPair instance for the given symbol names. (as used on Kraken Exchange)

Returns

krakenexapi.wallet.CurrencyPair – The CurrencyPair singleton instance.

Raises

KeyError – If no CurrencyPair exists for the symbol.

classmethod all_symbols(unique: bool = True) → Set[str][source]

Return a list of symbol for all registered CurrencyPair instances.

Parameters

unique (bool, optional) – Whether the list should contain all registered symbol and name, or just enough to allow retrieval of all singleton instances, by default True

Returns

Set[str] – List of currency pair symbols. (used on Kraken Exchange)

classmethod build_from_api(api: krakenexapi.api.BasicKrakenExAPIPublicMethods)[source]

Build a list of CurrencyPair and optionally Currency singleton instances.

Uses the public Kraken Exchange API to retrieve a list of all available currency trading pairs and build instances for each of it.

Parameters

api (BasicKrakenExAPIPublicMethods) – An API object that allows querying the public Kraken Exchange endpoints.

Transactions

class krakenexapi.wallet.TradingTransaction(currency_pair: krakenexapi.wallet.CurrencyPair, price: float, amount: float, cost: float, fees: float, timestamp: datetime.datetime, txid: str, otxid: Optional[Union[float, str]] = None)[source]

A trading transaction.

Subclasses should be used to mark buy / sell type.

currency_pair: krakenexapi.wallet.CurrencyPair

Currency pair, of base and quote currency. quote currency determines the price. base is the currency being traded.

price: float

Price of (crypto) currency.

amount: float

Amount of currency.

cost: float

Cost of base currency (currency_pair) in quote currency.

fees: float

Fees for transactions, in quote currency.

timestamp: datetime.datetime

Timestamp of transaction.

txid: str

Transaction ID.

otxid: Optional[Union[float, str]] = None

Optional. Order ID associated with transaction.

property base_currency

Base currency being traded.

Returns

Currency

property quote_currency

Quote currency. Determines the price and value of the base_currency currency.

Returns

Currency

class krakenexapi.wallet.CryptoBuyTransaction(currency_pair: krakenexapi.wallet.CurrencyPair, price: float, amount: float, cost: float, fees: float, timestamp: datetime.datetime, txid: str, otxid: Optional[Union[float, str]] = None)[source]

Bases: krakenexapi.wallet.TradingTransaction

Crypto currency buy transaction.

class krakenexapi.wallet.CryptoSellTransaction(currency_pair: krakenexapi.wallet.CurrencyPair, price: float, amount: float, cost: float, fees: float, timestamp: datetime.datetime, txid: str, otxid: Optional[Union[float, str]] = None)[source]

Bases: krakenexapi.wallet.TradingTransaction

Crypto currency sell transaction.

class krakenexapi.wallet.FundingTransaction(currency: krakenexapi.wallet.Currency, amount: float, timestamp: datetime.datetime, fees: float = 0.0, lxid: Optional[Union[int, float, str]] = None)[source]

A funding transaction.

Subclasses show whether it is a deposit or withdrawal.

currency: krakenexapi.wallet.Currency

Currency.

amount: float

Amount of currency in transaction.

timestamp: datetime.datetime

Timestamp of transaction.

fees: float = 0.0

Fees for transaction.

lxid: Optional[Union[int, float, str]] = None

Ledger ID. (more exact than timestamp if used in API queries)

class krakenexapi.wallet.DepositTransaction(currency: krakenexapi.wallet.Currency, amount: float, timestamp: datetime.datetime, fees: float = 0.0, lxid: Optional[Union[int, float, str]] = None)[source]

Bases: krakenexapi.wallet.FundingTransaction

Deposit transaction of (fiat) currency to Kraken Exchange.

class krakenexapi.wallet.WithdrawalTransaction(currency: krakenexapi.wallet.Currency, amount: float, timestamp: datetime.datetime, fees: float = 0.0, lxid: Optional[Union[int, float, str]] = None)[source]

Bases: krakenexapi.wallet.FundingTransaction

Withdrawal transaction from the Kraken Exchange.

Assets and Wallet

class krakenexapi.wallet.Asset(currency: krakenexapi.wallet.Currency, api: krakenexapi.api.BasicKrakenExAPIPrivateUserDataMethods, quote_currency: Optional[krakenexapi.wallet.Currency] = None)[source]

Wrapper around a Currency and api for easy asset win/loss computation.

property has_transactions

Return True if the currency has transactions.

Returns

boolTrue if transactions exist.

property currency

The asset currency.

Returns

Currency

property amount

Amount of currency the user has on Kraken Exchange since the last update.

Returns

float – Amount of currency.

property amount_buy

Total amount of currency being bought.

Retrieved from API via transactions.

Returns

float – total amount of currency bought.

property amount_sell

Total amount of currency being sold.

Retrieved from API via transactions.

Returns

float – total amount of currency sold.

property amount_by_transactions

Total of bought (amount_buy) and sold (amount_sell) amount.

Returns

float – Amount of currency just by buy/sell transactions.

Notes

\[amount = amount_{buy} - amount_{sell}\]
property price_buy_avg

Average price of all buy transactions.

Returns

float – average buy price

Notes

Price calculation

\[price_{buy} = \frac{\sum cost_{buy}}{\sum amount_{buy}}\]
property price_sell_avg

Average price of all sell transactions.

Returns

float – average sell price

See also

price_buy_avg

property price_avg

Average price based on cost by transaction and current amount.

Returns

float – average price

See also

cost, amount

price_for_noloss(fees_sell: float = 0.26) → float[source]

Minimum price that should be used for sell if no loss should be incurred. Prices larger than the noloss price will be wins.

Parameters

fees_sell (float, optional) – Kraken Exchange sell fees, by default 0.26 (maximum for market orders)

Returns

float – noloss price (based on quote currency)

Note

Current computation may not be completely correct but should be rather close. Please verify manually! (e.g. adjust price ±5%)

\[price_{no loss} = price_{avg} * \frac{1 + fees_{total}}{1 - fees_{sell}}\]

with \(fees_{total}\) being all fees incurred through transactions

property cost_buy

Sum of costs of buy transactions, based on quote currency.

Returns

float – total costs of buy transactions (without fees)

property cost_sell

Sum of costs of sell transactions, based on quote currency.

Returns

float – total costs of sell transactions (without fees)

property cost

Total costs computed by cost_buy and cost_sell.

So, only costs computed by transactions. Negative costs means that costs of buy is higher than sell, positive costs means the opposite. (Positive costs would mean a win based on transactions alone.)

Returns

float – Difference of costs for buy and sell

Notes

\[cost = - cost_{buy} + cost_{sell}\]

See also

cost_buy, cost_sell, fees

property fees_buy

Fees incurred by buy transactions.

Returns

float – total sum of fees

property fees_sell

Fees incurred by sell transactions.

Returns

float – total sum of fees

property fees

Total sum of fees for both buy and sell transactions.

Returns

float – total sum of fees

property fees_percentage

Percentage of fees compared to costs, of transactions.

Returns

float – fee percentage, see Kraken Exchange fees

Notes

\[fees_{\%} = 100 * \frac{fees}{cost_{buy} + cost_{sell}}\]
property is_loss

Loss based on transactions. Loss if costs for buy higher than sell.

Returns

boolTrue if loss (i.e. higher costs for buy)

class krakenexapi.wallet.Fund(currency: krakenexapi.wallet.Currency, api: krakenexapi.api.BasicKrakenExAPIPrivateUserDataMethods)[source]

Wrapper around fiat Currency info and computations.

property has_transactions

Return True if the currency has ledger entries for deposit and withdrawal.

Returns

boolTrue if transactions exist.

property currency

The fund fiat currency.

Returns

Currency

property amount

Amount of currency the user has on Kraken Exchange since the last update.

Returns

float – Amount of currency.

property amount_deposit

Total amount of currency being deposited.

Retrieved from API via ledger entries.

Returns

float – total amount of currency deposited.

property amount_withdrawal

Total amount of currency being withdrawn.

Retrieved from API via ledger entries.

Returns

float – total amount of currency withdrawn.

property amount_by_transactions

Total of deposited (amount_deposit) and withdrawn (amount_withdrawal) amount.

Returns

float – Amount of currency just by deposit/withdrawal transactions.

Notes

\[amount = amount_{deposit} - amount_{withdrawal}\]
property fees_deposit

Fees incurred by deposit transactions (ledgers).

Returns

float – total sum of fees

property fees_withdrawal

Fees incurred by withdrawal transactions (ledgers).

Returns

float – total sum of fees

property fees

Total sum of fees for both deposit and withdrawal transactions.

Returns

float – total sum of fees

property fees_percentage

Percentage of fees compared to amount of fiat currency.

Returns

float – fee percentage

Notes

\[fees_{\%} = 100 * \frac{fees}{amount_{deposit} + amount_{withdrawal}}\]
class krakenexapi.wallet.Wallet(api: krakenexapi.api.BasicKrakenExAPI)[source]
_update()[source]

Update assets and funds.

_update_assets()[source]

Update internal dictionary of Asset.

If no assets exist, create an initial dictionary of assets.

Check if new transactions found, then update all assets. If _last_txid is not None then query a subset of trading transactions to reduce traffic/calls.

_update_funds()[source]

Update internal dictionary of Fund.

If no assets exist, create an initial dictionary of funds.

Check if new funding transactions found, then update all funds. If _last_lxid is not None then query a subset of funding transactions to reduce traffic/calls.

static get_all_account_currencies(api: krakenexapi.api.BasicKrakenExAPIPrivateUserDataMethods) → List[krakenexapi.wallet.Currency][source]

Retrieve a full list of currencies used on Kraken Exchange by the user.

Current balance might not even show currency because sold, withdrawn or staked etc.

  1. Will look for currencies by trading transactions,

  2. then will look at deposit/withdrawals ledger entries,

  3. WIP then staking/transfering ledger entries,

  4. Currencies listed in current account balance.

Parameters

api (BasicKrakenExAPIPrivateUserDataMethods) – An API object that allows querying private endpoints

Returns

List[Currency] – List of all Currencies used by trader.

static get_account_crypto_currencies(api: krakenexapi.api.BasicKrakenExAPIPrivateUserDataMethods) → List[krakenexapi.wallet.Currency][source]

Return a list of crypto currencies used by the trader.

Parameters

api (BasicKrakenExAPIPrivateUserDataMethods) – An API instance with access to private Kraken Exchange endpoints.

Returns

List[Currency] – List of crypto currencies

static get_account_fiat_currencies(api: krakenexapi.api.BasicKrakenExAPIPrivateUserDataMethods) → List[krakenexapi.wallet.Currency][source]

Return a list of fiat currencies used by the trader.

Parameters

api (BasicKrakenExAPIPrivateUserDataMethods) – An API instance with access to private Kraken Exchange endpoints.

Returns

List[Currency] – List of fiat currencies

static build_assets_from_api(api: krakenexapi.api.BasicKrakenExAPIPrivateUserDataMethods, fiat_currency: Optional[krakenexapi.wallet.Currency] = None) → List[krakenexapi.wallet.Asset][source]

Build a list of Asset from the list of crypto currencies of the trader.

Optionally set a quote currency for computations.

Parameters
  • api (BasicKrakenExAPIPrivateUserDataMethods) – An API instance with access to private Kraken Exchange endpoints.

  • fiat_currency (Optional[Currency], optional) – Fiat currency for asset value computations, by default None

Returns

List[Asset] – List of crypto currency assets.

static build_funds_from_api(api: krakenexapi.api.BasicKrakenExAPIPrivateUserDataMethods)[source]

Build a list of Fund from the list of fiat currencies of the trader.

Parameters

api (BasicKrakenExAPIPrivateUserDataMethods) – An API instance with access to private Kraken Exchange endpoints.

Returns

List[Fund] – List of fiat currency funds.

static build_funding_transactions(api: krakenexapi.api.BasicKrakenExAPIPrivateUserDataMethods, start: Optional[Union[int, float, str]] = None, sort: bool = True) → List[krakenexapi.wallet.FundingTransaction][source]

Build a list of funding transactions based on ledger entries.

Parameters
  • api (BasicKrakenExAPIPrivateUserDataMethods) – An API instance to query private Kraken Exchange endpoints

  • start (Optional[Union[int, float, str]], optional) – Start timestamp or ledger LXID, by default None

  • sort (bool, optional) – Whether to sort transactions by timestamp ascending, by default True

Returns

List[FundingTransaction] – List of funding transactions

Raises

RuntimeError – On unknown funding (deposit/withdrawal) transaction type.

static build_trading_transactions(api: krakenexapi.api.BasicKrakenExAPIPrivateUserDataMethods, start: Optional[Union[int, float, str]] = None, sort: bool = True) → List[krakenexapi.wallet.TradingTransaction][source]

Build a list of trading transactions.

Parameters
  • api (BasicKrakenExAPIPrivateUserDataMethods) – An API instance to query private Kraken Exchange endpoints.

  • start (Optional[Union[int, float, str]], optional) – Start unix timestamp or transaction TXID, by default None

  • sort (bool, optional) – Whether to sort transactions by timestamp ascending, by default True

Returns

List[TradingTransaction] – List of trading transactions

Raises

RuntimeError – On unknown transaction type.