# weighted average in Balance Sheet (patch)

David G Hamblen dhamblen at adelphia.net
Thu Mar 10 07:30:33 EST 2005

```
On Mon, 24 Jan 2005, David G Hamblen wrote:

> Christian Stimming wrote:
>
>> Derek Atkins schrieb:
>>
>>>> I usually use the "Nearest in Time" option;  the weighted average is not
>>>> particularly informative.  Weighted average is, however, the default; and
>>>> it appears that that it's used to determine the cost when computing the
>>>> unrealized gain.  Since it's calculated using absolute values, it's not
>>>> even the correct number.
>>>
>>>
>>> Hmm, then that's definitely a bug..
>>
>> However, there is a particular reason to have "Weighted Average" the
>> default for any of the multi-currency report: GnuCash has two kinds of
>> prices. There are *explicit* prices with an entry in the Price Editor. And
>> there are *implicit* prices whenever a transaction with multiple currencies
>> is booked.
>> .........
>> Christian
>
> For the stock case, weighted average would work fine (at least for my test
> case) if the average were computed using the algebraic rather than the
> absolute values  (with testing for  0/0 cases).  I'm not sure I understand
> the impact on the implicit pricings.  My example was buying 100 shares at \$5,
> selling it at a later date at \$5, and then at a still later date buying 10
> shares at \$20.  The weighted average appears to be calculated as
> (5*100+5*100+20*10)/(100+100+10)=\$5.71/sh.  I think it should be calculated
> as  (5*100-5*100+20*10)/(100-100+10)=\$20.00/sh.  The  individual prices are
> always positive, but the shares and the values can both be negative.   Seems
> like this ought to work with the implicit prices as well.  The gotcha in this
> is the after the second transaction I have zero shares with an undefined
> price (\$0/0); so we need to test and skip the case where there's no current
> position in the asset.
>
> Dave
>

I finally got things working to my satisfaction (patches below) by
eliminating all the "gnc:numeric-abs" functions from
scm/commodity-utilities.scm (except the one's around the timepair-dates).
This created a divide-by-zero problem for commodities which I no longer
own (\$0.00/0.0000 shares) which I temporarily fixed by "purchasing" 0.0001
share at \$1/sh.  A better(??) solution was to patch the function
"gnc:make-commodity-collector" in scm/report-utilities.scm to skip values
with a zero denominator.

I haven't done a thorough search to see where else these functions might
be used; so I don't know if this screws up the the computations with
multiple currencies.

---------------------------------------------------------------------
--- commodity-utilities.scm-src	2004-08-18 00:43:23.000000000 -0400
+++ commodity-utilities.scm	2005-03-08 11:06:20.000000000 -0500
@@ -125,10 +125,10 @@
(gnc:split-get-parent a)))
(account-comm (gnc:account-get-commodity
(gnc:split-get-account a)))
-	       (share-amount (gnc:numeric-abs
-			      (gnc:split-get-amount a)))
-	       (value-amount (gnc:numeric-abs
-			      (gnc:split-get-value a)))
+	       (share-amount
+			      (gnc:split-get-amount a))
+	       (value-amount
+			      (gnc:split-get-value a))
(transaction-date (gnc:transaction-get-date-posted
(gnc:split-get-parent a)))
(foreignlist
@@ -233,10 +233,10 @@
(gnc:split-get-parent a)))
(account-comm (gnc:account-get-commodity
(gnc:split-get-account a)))
-	     (share-amount (gnc:numeric-abs
-			    (gnc:split-get-amount a)))
-	     (value-amount (gnc:numeric-abs
-			    (gnc:split-get-value a)))
+	     (share-amount
+			    (gnc:split-get-amount a))
+	     (value-amount
+			    (gnc:split-get-value a))
(transaction-date (gnc:transaction-get-date-posted
(gnc:split-get-parent a)))
(foreignlist
@@ -547,10 +547,10 @@
(account-comm (gnc:account-get-commodity
(gnc:split-get-account a)))
;; Always use the absolute value here.
-		  (share-amount (gnc:numeric-abs
-				 (gnc:split-get-amount a)))
-		  (value-amount (gnc:numeric-abs
-				 (gnc:split-get-value a)))
+		  (share-amount
+				 (gnc:split-get-amount a))
+		  (value-amount
+				 (gnc:split-get-value a))
(tmp (assoc transaction-comm sumlist))
(comm-list (if (not tmp)
(assoc account-comm sumlist)
@@ -610,11 +610,11 @@
(map
(lambda (e)
(list (car e)
-	   (gnc:numeric-abs
+
GNC-DENOM-AUTO
-			     (logior (GNC-DENOM-SIGFIGS 8) GNC-RND-ROUND)))))
+			     (logior (GNC-DENOM-SIGFIGS 8) GNC-RND-ROUND))))
(gnc:get-exchange-totals report-commodity end-date)))

--- report-utilities.scm-src	2004-08-18 00:43:23.000000000 -0400
+++ report-utilities.scm	2005-03-09 11:22:09.000000000 -0500
@@ -387,7 +387,8 @@
;; and add it to the alist
(set! commoditylist (cons pair commoditylist))))
+	(if (not (= 0 (gnc:numeric-denom value)))	;;;dgh

;; helper function to walk an association list, adding each
;; (commodity -> collector) pair to our list at the appropriate

--------------------------------------------------------------------
Dave

```