Next [was Re: Stock trades and realized gains/losses]

Derek Atkins warlord@MIT.EDU
11 Jan 2003 12:01:57 -0500


Linas,

Thank you for this great summary.  Mind if I ask a few clarifying
questions?

linas@linas.org (Linas Vepstas) writes:

> Transaction balancing: If all splits in a transaction are
> in the same 'currency' as the transaction currency, then the
> sum of the splits *must* equal zero.  This is the old, existing
> double-entry requirement as implemented in Gnucash, and doesn't 
> change.

Are you referring to split->amount or split->value?  I thought that
regardless of what commodity the split->account points to, the
split->value of all splits in a transaction must equal zero.  This
should be true regardless of whether it's a one-commodity,
two-commodity, or N-commodity transaction.

Obviously if the split->account commodity and the txn currency are the
same, then the split->amount == split->value, so if you have a
one-commodity txn both split->amount and split->value should sum to
zero.

> If some splits are in one commodity, and others in another, then
> we can't force a zero balance as above.  Instead, we will force
> a different requirement, the 'double-balance' requirement:

Well, we certainly cannot force a zero-balance on the split->amount,
but we can still force a zero-balance on the split->value.

> -- All splits that are *not* in the transaction currency C
>    must be made a part of a lot.   (Thus, for example, 
> 	the transaction currency C is dollars, whereas the split
> 	commodity is 'S', shares of RHAT.  If a transaction
> 	has C in dollars, and 'S' in RHAT, then the S split must 
> 	be made a part of a Lot.)

Agreed.

> -- The lot will have a designated 'lot currency' L that must
>    be the same as the C of every split in the lot.  One
> 	cannot enter a split into the lot if C != L.  (That is,
> 	if I'm working with a Lot of RHAT shares, then *all*
> 	splits in the lot must belong to dollar-denominated
> 	transactions.)

Agreed.  However, I think your example needs to be, "If I'm working
with a Lot of RHAT shares denoted in USD, then ...

> -- When a lot is closed, we must have the 'double-balance'
>    condition:  The sum total of all 'S' is zero, and the
> 	sum total of all 'C' is zero.  Thus, if I buy 100 shares
> 	of RHAT for $5 and sell 100 shares of RHAT for $10, then
> 	I *must* also add an 'adjusting transaction' for zero
> 	shares of RHAT, at $500.  If there is no adjusting transaction,
> 	then the lot cannot be closed.  If sum 'S' is zero,
> 	while sum 'C' is not zero, then the lot is declared to 
> 	be 'out-of-balance', and an 'adjusting transaction' must 
> 	be forced.

I do like this idea a LOT (no pun intended).  It would just require a
small change to gnc_los_is_closed(), but would require a bit for work
to determine if a lot in "out of balance"...

> It is only by 'closing a lot' that I am able to regain 
> 'perfect balance' in the books.   That is, the 'double-balance'
> requirement is the generalization of the 'double-entry'
> requirement for stock accounts.

True, and one could always "forward" stocks from one lot to another in
order to close a lot -- it would then open another lot (perhaps in
another book).  Actually, I think we have a better invariant for
balance: the ratio of split->amount to split->value for the "purchase"
must be maintained through the sums as well.  In other words, the sum
of split->amount over all splits in the lot must maintain the same
ratio with the sum of split->value over all splits in the lot.

> Note that because the 'adjusting transaction' has one split
> in dollars, and another split in RHAT shares (albeit for zero 
> RHAT shares), it evades the old double-entry requriement,
> and will not be flagged as 'out of balance'.  Note also 
> that because the 'adjusting transaction' contains a split
> holding S (albeit zero S), it *must* be a part of a Lot.

But it IS in balance by the old rules.  The split->value is in
balance.

> I think this satisfies Matthews requirement: the ablity 
> to add any number of 'adjusting transactions' that will
> credit an 'unrealized gain/loss' account while the lot
> is open, and force the monetary value of the lot to
> balance when the shares are finally sold.

Yes, this does work.  It obviously means that my ratio method does
not....

> I think it can also be implemented with little or no change 
> the ledger GUI, which is good. But ...

Let me leave the more complicated stuff for another email..  Let's see
if we can come to closure on the simple stuff first?

Are we close?

-derek

-- 
       Derek Atkins, SB '93 MIT EE, SM '95 MIT Media Laboratory
       Member, MIT Student Information Processing Board  (SIPB)
       URL: http://web.mit.edu/warlord/    PP-ASEL-IA     N1NWH
       warlord@MIT.EDU                        PGP key available