-
Notifications
You must be signed in to change notification settings - Fork 167
Amounts
Financial amounts must be kept exactly, but float and double types are susceptible to rounding errors. Also, both prices and volumes trade in discrete increments, sometimes in half- or quarter- units. So Coin Trader has an Amount
base class which has basic arithmatic operations on it, and two subclasses DecimalAmount
and DiscreteAmount
which have different numerical representations. DecimalAmount
is backed by a BigDecimal
with MathContext.DECIMAL128
rounding for fine-grained (continuous) number representation. The DiscreteAmount
class represents numbers using a two-number representation: a count
and a basis
. The basis
is the smallest increment in the counting system, like pennies are the smallest unit for US dollars. Swiss Francs are rounded to the nearest nickel, and are represented in a DiscreteAmount
with a basis of 0.05. The count
is the number of increments in the value. For Bitcoin, the count
is the number of Satoshis, and the basis
is 1e-8. To avoid float rounding, DiscreteAmount
actually stores 1/basis as a long. This "inverted basis" is the number of increments in a whole unit. Most of the Amount
methods are accessible from the base class, but DiscreteAmount
additionally offers increment()
and decrement()
which are useful for bidding one pip higher or lower than a target price.