# Reporting: weighted average price source

Charles Day cedayiv at gmail.com
Fri Jul 4 14:30:46 EDT 2008

On Fri, Jul 4, 2008 at 2:58 AM, Christian Stimming <stimming at tuhh.de> wrote:

> Am Freitag, 4. Juli 2008 04:31 schrieb Charles Day:
> > For reports, does anyone know why the calculation for price source
> > "weighted average" uses the absolute value in its calculations? It seems
> to
> > me that this gives incorrect results.
> >
> > In most cases the figures for the "value" and "amount" fields of splits
> are
> > either both negative or both positive, which makes the sign irrelevant.
> > Sometimes the "amount" is zero and the "value" is positive, such as in
> > capital gains. However there are some cases when "amount" could be zero
> > while "value" is negative and needs to stay negative, such as a return of
> > capital (see bug 538800).
> >
> > I am inclined to change this code to take away the use of absolute value,
> > but wanted to check with everyone to see if there is some particular
> reason
> > that it needs to be this way.
>
> If I recall correctly, this code comes from the old days when I added this
> to
> track "my personal exchange rate" between USD and EUR. Without this code, I
> think if I bought x USD for y EUR and later sold x USD for y EUR, the
> resulting rate turned out zero instead of the expected value (which is x
> divided by y). Using absolute values here gave the intended results.
>

Ah, I see... so is the "weighted average" price source defined as "weighted
average price experienced during buys and sells, not including transaction
fees such as commission"? Because if so, then do you think that exchanges
with a zero "amount", such as capital gains and losses, should be excluded?

Example: Buy 5 USD for 4 EUR (exchange rate = 0.8), then sell 5 USD for 3
EUR (exchange rate = 0.6), giving a 1 EUR capital loss. I am guessing from
your response that would expect to see the weighted average price of the buy
and the sell alone, which is (4 + 3) / (5 + 5) = 0.7. However, the current
formula includes the capital loss in the calculation: (4 + 3 + 1) / (5 + 5 +
0) = 0.8.  Of course, this assumes that you actually record the capital
loss. (GnuCash doesn't require you to, but failing to record the loss puts
the books out of balance.)

Now on the other hand, what I am proposing is to get rid of the absolute
value but keep the zero "amount" exchanges. That would change the definition
to "weighted average cost for shares currently held, not including
transaction fees such as commission".  A few users have been asking for this
option, which I maybe could add.

-Charles

That's just what I vaguely remembered.
>
> Christian
>
> >
> > Cheers,
> > Charles
> >
> > >From src/report/report-system/commodity-utilities.scm, starting at line
> > > 558:
> >
> >                   ;; Always use the absolute value here.
> >                   (share-amount (gnc-numeric-abs
> >                                  (xaccSplitGetAmount a)))
> >                   (value-amount (gnc-numeric-abs
> >                                  (xaccSplitGetValue a)))
> > _______________________________________________
> > gnucash-devel mailing list
> > gnucash-devel at gnucash.org
> > https://lists.gnucash.org/mailman/listinfo/gnucash-devel
>