# Stock Sales - Balance sheet unbalanced

David G Hamblen dhamblen at adelphia.net
Thu Jan 20 13:48:09 EST 2005

```Hi:

My problem is with the weighted-average computation causing the balance
sheet to be unbalanced.  I've been seeing this for several years now.  I
just compiled and installed 1.8.10 and the problem is unchanged. The
calculation seems to use absolute values when algebraic values should be
used.  The code in "Advanced Portfolio" gets it right.  The code in the
files "balance-sheet.scm" and "advanced-portfolio.scm" appears to attack
the calculation from different directions; so I wouldn't know how to
modify the code (not knowing scheme).

A sample set of transactions are as follows (each transaction is balanced
by an appropriate amount in a Equity (or profit) account):

0 - Set up initial cash (debit checking, credit income).

1 - Buy 100 shares of XYZ corp for \$500 on 1/1/00,
average cost=\$5.00/share (debit XYZ Asset, credit Checking) (ok)

2 - Sell all 100 shares of XYZ corp for \$500 on 1/1/01,
average cost = (\$500+\$500)/(100+100)= \$5.00/share (credit XYZ
Assets, debit Checking) (ok, but should simply be undefined 0/0).
Note there's no complicating realized gain/loss.

3 - Buy 10 shares of XYZ corp for \$200 on 1/1/02,
average cost = (\$500+\$500+\$200)/(100+100+10)=\$5.71/share
(wrong, the average price of my holdings should be \$20/share)

I entered a price of \$10/share into Price database using the price editor
in order to have a current value.  Using the "Weighted Average" option,
the Gnucash Balance Sheet shows Assets for XYZ corp of \$57.10, based on
the weighted average of absolute vales, Checking account assets of \$800,
and Total Equity of \$1000, leaving an imbalance of \$142.86.  If I use
"Most Recent" or "Nearest in Time", the Asset values are then listed
correctly, but then the "Unrealized Gains(Losses) line (which uses the
weighted average) is too large by the \$142.86 error.

The "Advanced Portfolio" OTOH, correctly shows a "Value" of \$100, "Money
In" of \$700, and "Money Out" of \$500 for a net "Gain" of -\$100 (as opposed
to the -\$42.86 on the balance sheet).

I've attached a test.xac file with just these entries.

Dave

-------------- next part --------------
<?xml version="1.0"?>
<gnc-v2>
<gnc:count-data cd:type="book">1</gnc:count-data>
<gnc:book version="2.0.0">
<book:id type="guid">b6052d597d73160f8a6965022f7cc5e9</book:id>
<gnc:count-data cd:type="commodity">1</gnc:count-data>
<gnc:count-data cd:type="account">4</gnc:count-data>
<gnc:count-data cd:type="transaction">4</gnc:count-data>
<gnc:commodity version="2.0.0">
<cmdty:space>AMEX</cmdty:space>
<cmdty:id>XYZ</cmdty:id>
<cmdty:name>XYZ</cmdty:name>
<cmdty:xcode>XYZ</cmdty:xcode>
<cmdty:fraction>10000</cmdty:fraction>
</gnc:commodity>
<gnc:pricedb version="1">
<price>
<price:id type="guid">48a14695453d488c5e2e5178294ffd73</price:id>
<price:commodity>
<cmdty:space>AMEX</cmdty:space>
<cmdty:id>XYZ</cmdty:id>
</price:commodity>
<price:currency>
<cmdty:space>ISO4217</cmdty:space>
<cmdty:id>USD</cmdty:id>
</price:currency>
<price:time>
<ts:date>2004-12-21 00:00:00 -0500</ts:date>
</price:time>
<price:source>user:price-editor</price:source>
<price:type>unknown</price:type>
<price:value>10000000/1000000</price:value>
</price>
</gnc:pricedb>
<gnc:account version="2.0.0">
<act:name>Checking</act:name>
<act:id type="guid">40e9891425607d3a670fc3e9c6a5cd07</act:id>
<act:type>BANK</act:type>
<act:commodity>
<cmdty:space>ISO4217</cmdty:space>
<cmdty:id>USD</cmdty:id>
</act:commodity>
<act:commodity-scu>100</act:commodity-scu>
<act:code>1001</act:code>
<act:slots>
<slot>
<slot:key>placeholder</slot:key>
<slot:value type="string">false</slot:value>
</slot>
<slot>
<slot:key>notes</slot:key>
<slot:value type="string"></slot:value>
</slot>
</act:slots>
</gnc:account>
<gnc:account version="2.0.0">
<act:name>XYZ corp</act:name>
<act:id type="guid">2423a3564cc24398fb45416a1993e46b</act:id>
<act:type>STOCK</act:type>
<act:commodity>
<cmdty:space>AMEX</cmdty:space>
<cmdty:id>XYZ</cmdty:id>
</act:commodity>
<act:commodity-scu>10000</act:commodity-scu>
<act:code>1002</act:code>
<act:slots>
<slot>
<slot:key>placeholder</slot:key>
<slot:value type="string">false</slot:value>
</slot>
<slot>
<slot:key>notes</slot:key>
<slot:value type="string"></slot:value>
</slot>
</act:slots>
</gnc:account>
<gnc:account version="2.0.0">
<act:name>Income</act:name>
<act:id type="guid">0f8c370f6247b4d838e2ce3bf305e642</act:id>
<act:type>INCOME</act:type>
<act:commodity>
<cmdty:space>ISO4217</cmdty:space>
<cmdty:id>USD</cmdty:id>
</act:commodity>
<act:commodity-scu>100</act:commodity-scu>
<act:code>4001</act:code>
<act:slots>
<slot>
<slot:key>placeholder</slot:key>
<slot:value type="string">false</slot:value>
</slot>
<slot>
<slot:key>notes</slot:key>
<slot:value type="string"></slot:value>
</slot>
</act:slots>
</gnc:account>
<gnc:transaction version="2.0.0">
<trn:currency>
<cmdty:space>ISO4217</cmdty:space>
<cmdty:id>USD</cmdty:id>
</trn:currency>
<trn:date-posted>
<ts:date>2000-01-01 00:00:00 -0500</ts:date>
</trn:date-posted>
<trn:date-entered>
<ts:date>2004-12-21 11:09:21 -0500</ts:date>
</trn:date-entered>
<trn:description>Initial transaction</trn:description>
<trn:splits>
<trn:split>
<split:id type="guid">e8ea8d22cd0fc1b9a5a9b79eb5329b63</split:id>
<split:reconciled-state>n</split:reconciled-state>
<split:value>100000/100</split:value>
<split:quantity>100000/100</split:quantity>
<split:account type="guid">40e9891425607d3a670fc3e9c6a5cd07</split:account>
</trn:split>
<trn:split>
<split:id type="guid">f048ef23eae08c95efa42007eb662cf7</split:id>
<split:reconciled-state>n</split:reconciled-state>
<split:value>-100000/100</split:value>
<split:quantity>-100000/100</split:quantity>
<split:account type="guid">0f8c370f6247b4d838e2ce3bf305e642</split:account>
</trn:split>
</trn:splits>
</gnc:transaction>
<gnc:transaction version="2.0.0">
<trn:id type="guid">39e49e2b88c09369663b3e663dfacb9b</trn:id>
<trn:currency>
<cmdty:space>ISO4217</cmdty:space>
<cmdty:id>USD</cmdty:id>
</trn:currency>
<trn:date-posted>
<ts:date>2000-01-01 00:00:00 -0500</ts:date>
</trn:date-posted>
<trn:date-entered>
<ts:date>2004-12-21 11:11:10 -0500</ts:date>
</trn:date-entered>
<trn:description>Purchase initial set of shares</trn:description>
<trn:splits>
<trn:split>
<split:id type="guid">2d02abc8dd267cfbafc61b04c1738d08</split:id>
<split:reconciled-state>n</split:reconciled-state>
<split:value>50000/100</split:value>
<split:quantity>1000000/10000</split:quantity>
<split:account type="guid">2423a3564cc24398fb45416a1993e46b</split:account>
</trn:split>
<trn:split>
<split:id type="guid">3c19f9460e54a0c440decd3699d830d2</split:id>
<split:reconciled-state>n</split:reconciled-state>
<split:value>-50000/100</split:value>
<split:quantity>-50000/100</split:quantity>
<split:account type="guid">40e9891425607d3a670fc3e9c6a5cd07</split:account>
</trn:split>
</trn:splits>
</gnc:transaction>
<gnc:transaction version="2.0.0">
<trn:id type="guid">c1a1243ecf1aaa495ba860430f937489</trn:id>
<trn:currency>
<cmdty:space>ISO4217</cmdty:space>
<cmdty:id>USD</cmdty:id>
</trn:currency>
<trn:date-posted>
<ts:date>2001-01-01 00:00:00 -0500</ts:date>
</trn:date-posted>
<trn:date-entered>
<ts:date>2004-12-21 11:13:24 -0500</ts:date>
</trn:date-entered>
<trn:description>Sell initial shares at original price</trn:description>
<trn:splits>
<trn:split>
<split:id type="guid">9c0fcc887fa9ff62991ed1cfa9cf7963</split:id>
<split:reconciled-state>n</split:reconciled-state>
<split:value>50000/100</split:value>
<split:quantity>50000/100</split:quantity>
<split:account type="guid">40e9891425607d3a670fc3e9c6a5cd07</split:account>
</trn:split>
<trn:split>
<split:id type="guid">86f82d785aa79d98b6d27c7db29dd920</split:id>
<split:action>Sell</split:action>
<split:reconciled-state>n</split:reconciled-state>
<split:value>-50000/100</split:value>
<split:quantity>-1000000/10000</split:quantity>
<split:account type="guid">2423a3564cc24398fb45416a1993e46b</split:account>
</trn:split>
</trn:splits>
</gnc:transaction>
<gnc:transaction version="2.0.0">
<trn:id type="guid">8e6f390fd804d1ed3a1473aa79323664</trn:id>
<trn:currency>
<cmdty:space>ISO4217</cmdty:space>
<cmdty:id>USD</cmdty:id>
</trn:currency>
<trn:date-posted>
<ts:date>2002-01-01 00:00:00 -0500</ts:date>
</trn:date-posted>
<trn:date-entered>
<ts:date>2004-12-21 11:14:37 -0500</ts:date>
</trn:date-entered>
<trn:description>Purchase a second set of shares at 20</trn:description>
<trn:splits>
<trn:split>
<split:id type="guid">b9b032fc5e0cb6234ced9ca473b98636</split:id>
<split:reconciled-state>n</split:reconciled-state>
<split:value>20000/100</split:value>
<split:quantity>100000/10000</split:quantity>
<split:account type="guid">2423a3564cc24398fb45416a1993e46b</split:account>
</trn:split>
<trn:split>
<split:id type="guid">8b9e330361ecea9bddd4b266b789bdb9</split:id>
<split:reconciled-state>n</split:reconciled-state>
<split:value>-20000/100</split:value>
<split:quantity>-20000/100</split:quantity>
<split:account type="guid">40e9891425607d3a670fc3e9c6a5cd07</split:account>
</trn:split>
</trn:splits>
</gnc:transaction>
</gnc:book>
</gnc-v2>

<!-- Local variables: -->
<!-- mode: xml        -->
<!-- End:             -->
```