Programming API

Currency

The currency module defines currencies of various types. As of this release, there are only a handful yet defined.

class tallywallet.common.currency.Currency[source]

This is an enumeration class which captures the codes defined in ISO 4217 for international currencies.

x.__init__(...) initializes x; see help(type(x)) for signature

Trade

class tallywallet.common.trade.TradePath

TradePath(rcv, work, out)

A 3-tuple of Currency objects, describing the path of a foreign exchange trade. The first element is the source currency, the second the reference currency of the account, and the third the destination currency.

x.__init__(...) initializes x; see help(type(x)) for signature

class tallywallet.common.trade.TradeGain

TradeGain(rcv, gain, out)

A 3-tuple of numerical values, describing the result of a change in foreign exchange rates. The first element is the value due to the prior rate, the second is the gain due to the rate change, and the third element is the final sum.

x.__init__(...) initializes x; see help(type(x)) for signature

Exchange

The exchange module contains functionality to enable conversion between currencies.

class tallywallet.common.exchange.Exchange

An exchange is a lookup container for currency exchange rates.

It behaves just like a Python dictionary, but has some extra methods.

The approach Tallywallet uses is to associate each rate against a key which is a 2-tuple of Currency. By convention, the first element of this key is the source currency, and the second is the destination. The values of the exchange mapping can therefore be considered as gain from one currency to the next.

get(key)

Return the value stored against key.

This method overrides the standard behaviour of dict.get. It infers the values of missing keys as follows:

  • The rate of a currency against itself is unity.
  • The rate of one currency against another is the reciprocal of the reverse rate (if defined).

x.__init__(...) initializes x; see help(type(x)) for signature

convert(val, path, fees=TradeFees(rcv=0, out=0))

Return the calculated outcome of converting the amount val via the TradePath path.

gain(val, path, prior=None, fees=TradeFees(rcv=0, out=0))

Calculate the gain related to a change in exchange rates. The self object contains the latest rates, and the historic ones are passed as an argument to this method.

Parameters:prior – An Exchange object which contains the rates existing prior to those in the self object.
Return type:TradeGain

Ledger

The ledger module defines Ledger and some associated classes.

class tallywallet.common.ledger.Role[source]

This enumeration contains definitions for the roles played by a column in a Ledger.

x.__init__(...) initializes x; see help(type(x)) for signature

class tallywallet.common.ledger.Column

Column(name, currency, role)

A 3-tuple, describing a column in a Ledger.

x.__init__(...) initializes x; see help(type(x)) for signature

class tallywallet.common.ledger.Ledger(*args, ref=<Currency.XTW: 'tallywallet'>)[source]

This class implements the fundamental operations you need to perform Adjusted Cost Base accounting.

Parameters:
  • ref – (optional) the base Currency type for the Ledger
  • args – One or more Column objects
equation[source]

The Fundamental Accounting Equation is this:

Assets - Liabilities = Capital + Income - Expenses - Dividends

Currency trading gains are counted as income. For practical purposes, the FAE is often rearranged to be:

Assets + Expenses + Dividends = Capital + Income + Liabilities

This property evaluates both sides of this second equation, and determines if they are equal or not.

Returns:A tuple of lhs, rhs, status
adjustments(exchange, cols=None)[source]

Calculates the effects of a change in exchange rates.

Supply an Exchange object and an optional sequence of columns. If no columns are specified then all are assumed.

The columns are recalculated (but not committed) against the new exchange rates.

This method will generate a sequence of 3-tuples; (TradeGain, Column, Exchange).

This output is compatible with the arguments accepted by the commit method.

commit(trade, col, exchange=None, **kwargs)[source]

Applies a trade to the ledger.

If you supply an exchange argument, trade may be a TradeGain object. In this usage, a trading account in the currency of the ledger column will accept any exchange gain or loss.

Otherwise, trade should be a number. It will be added to the specified column in the ledger.

value(name)[source]

Returns the current value of a column in the ledger.

Parameters:name – The name of the column

Output

Tallywallet has chosen RSON as the serialisation format for a Ledger. It’s a readable serial object notation which can be processed by text utilities or converted into Python objects.

tallywallet.common.output.metadata(ledger)[source]

Create an RSON string containing all the metadata required to recreate a working Ledger object.

tallywallet.common.output.journal(ledger, **kwargs)[source]

Create an RSON string representing the current state of a Ledger object.

Journal strings can be appended to metadata, and/or concatenated to form a time-series record of the Ledger accounts.