Hey there,
First, just for the record, I "just" stumbled upon a temporary solution
while investigating the issue:
--------------------
2002/03/20 Buy Actions
    Assets:BrokerA      10.00 Foo {5.00 EUR}
    Assets:Checking    -50.00 EUR

2002/03/25 Buy Actions
    Assets:BrokerA      15.00 Foo {10.00 EUR}
    Assets:Checking   -150.00 EUR

2002/03/25 Price adjustment
    Assets:BrokerA      25.00 Foo {8.00 EUR}
    Assets:BrokerA     -10.00 Foo {5.00 EUR}
    Assets:BrokerA     -15.00 Foo {10.00 EUR}
    Void

2002/05/15 Buy Actions
    Assets:BrokerB      10.00 Foo {50.00 EUR}
    Assets:Checking    -500.00 EUR

2002/05/25 Buy Actions
    Assets:BrokerB      15.00 Foo {100.00 EUR}
    Assets:Checking   -1500.00 EUR

2002/05/25 Price adjustment
    Assets:BrokerB      25.00 Foo {80.00 EUR}
    Assets:BrokerB     -10.00 Foo {50.00 EUR}
    Assets:BrokerB     -15.00 Foo {100.00 EUR}
    Void
--------------------
And considering the "Void" account a garbage one.

Now, concerning the issue itself,
my investigation in the source code led me to that (I didn’t go further
yet as my head started burning. Please pardon me if I don’t use the
correct C++ terms as it’s not my world at all):

The method commodity_pool_t::find is called before adding "Foo {80.00
EUR}", which looks for an existing commodity in annotated_commodities
(pool.cc). However, this is a std::map array, which assumes that all
values in the array can be compared one to each other with the "<"
operator. In our case, he was trying to compare "Foo {80.00 EUR}" with
the existing "Foo {8.00 EUR}" and he fails since amounts cannot be
compared for that (amount.cc, amount_t::compare, "commodity() !=
amt.commodity()")

Maybe, for the particular case of including new commodities to the pool,
we should make the "<" operator more permissive and replace it with a
lexicographic order (Foo == Foo, 8.00 < 80.00)? I’m actually quite
surprised it doesn’t blow up in other places for the same reason too...

Probably, the insert method of std::map assumes that keys are completly
sortable and skips some checks when it includes it, which results in a
possibly corrupted array.

Until I understand further the code, I leave it to someone else who
understands it better.

Hope it helps!

Kind regards,

(Wed, Oct 25, 2017 at 09:05:31AM +0000) Ismael Bouya :
> Hi Martin,
> Thanks for your suggestion.
> To be honest I’m quite unhappy with this solution, since I use a lot the
> --lot-prices feature of ledger, and your solution makes it unusable on
> the long term...
> (also, I’ll end up averaging hundreds of postings which can be a long
> computation...)
> 
> Kind regards,
> 
> (Wed, Oct 25, 2017 at 02:48:02AM +0000) Martin Blais :
> > Hi Ismael,
> > 
> > In your specific case, what I would do is avoid even doing the adjustments,
> > and when I need to make a sale, just sum up the cost bases in all the
> > historical lots to obtain the average cost basis at that point and record a
> > sale with that basis. The sum of the units, and the sum of the cost bases
> > (signed units x cost price), should separately be correct even if the
> > postings don't reduce each other.
> > 
> > I hope this helps,
> > 
> > 
> > On Tue, Oct 24, 2017 at 6:03 PM, Ismaël Bouya <
> > ismael.bouya...@normalesup.org> wrote:
> > 
> > > In France, the taxation model when buying and selling actions is based on
> > > mean value of the actions rather than "older first". This means that I 
> > > have
> > > to use tweaks like that to have them correct in ledger:
> > > ----------------------
> > > 2002/03/20 Buy Actions
> > >     Assets:Broker       10.00 Foo {5.00 EUR}
> > >     Assets:Checking    -50.00 EUR
> > >
> > > 2002/03/25 Buy Actions
> > >     Assets:Broker       15.00 Foo {10.00 EUR}
> > >     Assets:Checking   -150.00 EUR
> > >
> > > ; In France, we consider the mean value of an action for taxation
> > > 2002/03/25 Price adjustment
> > >     Assets:Broker       25.00 Foo {8.00 EUR}
> > >     Assets:Broker      -10.00 Foo {5.00 EUR}  @ 1 Foo {8.00 EUR}
> > >     Assets:Broker      -15.00 Foo {10.00 EUR} @ 1 Foo {8.00 EUR}
> > > -------------------
> > >
> > > Now, the problem comes when I buy some actions again, even with another
> > > account:
> > > --------------------
> > > 2002/03/20 Buy Actions
> > >     Assets:BrokerA      10.00 Foo {5.00 EUR}
> > >     Assets:Checking    -50.00 EUR
> > >
> > > 2002/03/25 Buy Actions
> > >     Assets:BrokerA      15.00 Foo {10.00 EUR}
> > >     Assets:Checking   -150.00 EUR
> > >
> > > ; In France, we consider the mean value of an action for taxation
> > > 2002/03/25 Price adjustment
> > >     Assets:BrokerA      25.00 Foo {8.00 EUR}
> > >     Assets:BrokerA     -10.00 Foo {5.00 EUR}  @ 1 Foo {8.00 EUR}
> > >     Assets:BrokerA     -15.00 Foo {10.00 EUR} @ 1 Foo {8.00 EUR}
> > >
> > > 2002/05/15 Buy Actions
> > >     Assets:BrokerB      10.00 Foo {50.00 EUR}
> > >     Assets:Checking    -500.00 EUR
> > >
> > > 2002/05/25 Buy Actions
> > >     Assets:BrokerB      15.00 Foo {100.00 EUR}
> > >     Assets:Checking   -1500.00 EUR
> > >
> > > 2002/05/25 Price adjustment
> > >     Assets:BrokerB      25.00 Foo {80.00 EUR}
> > >     Assets:BrokerB     -10.00 Foo {50.00 EUR}  @ 1 Foo {80.00 EUR}
> > >     Assets:BrokerB     -15.00 Foo {100.00 EUR} @ 1 Foo {80.00 EUR}
> > > --------------------
> > >
> > > This example fails with "Error: Cannot compare amounts with different
> > > commodities: 'Foo {8.00 EUR}' and 'Foo {80.00 EUR}'"
> > >
> > > I don’t understand where this error comes from, the two accounts are not
> > > even related...
> > >
> > > Any hint how I could achieve my goal?
> > >
> > > Kind regards,
> > >
> > > --
> > >
> > > ---
> > > You received this message because you are subscribed to the Google Groups
> > > "Ledger" group.
> > > To unsubscribe from this group and stop receiving emails from it, send an
> > > email to ledger-cli+unsubscr...@googlegroups.com.
> > > For more options, visit https://groups.google.com/d/optout.
> > >
> > 
> > -- 
> > 
> > --- 
> > You received this message because you are subscribed to the Google Groups 
> > "Ledger" group.
> > To unsubscribe from this group and stop receiving emails from it, send an 
> > email to ledger-cli+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
> 
> >    Hi Ismael,
> >    In your specific case, what I would do is avoid even doing the
> >    adjustments, and when I need to make a sale, just sum up the cost bases 
> > in
> >    all the historical lots to obtain the average cost basis at that point 
> > and
> >    record a sale with that basis. The sum of the units, and the sum of the
> >    cost bases (signed units x cost price), should separately be correct even
> >    if the postings don't reduce each other.
> >    I hope this helps,
> >    On Tue, Oct 24, 2017 at 6:03 PM, Ismaël Bouya
> >    <[1]ismael.bouya...@normalesup.org> wrote:
> > 
> >      In France, the taxation model when buying and selling actions is based
> >      on mean value of the actions rather than "older first". This means that
> >      I have to use tweaks like that to have them correct in ledger:
> >      ----------------------
> >      2002/03/20 Buy Actions
> >          Assets:Broker       10.00 Foo {5.00 EUR}
> >          Assets:Checking    -50.00 EUR
> > 
> >      2002/03/25 Buy Actions
> >          Assets:Broker       15.00 Foo {10.00 EUR}
> >          Assets:Checking   -150.00 EUR
> > 
> >      ; In France, we consider the mean value of an action for taxation
> >      2002/03/25 Price adjustment
> >          Assets:Broker       25.00 Foo {8.00 EUR}
> >          Assets:Broker      -10.00 Foo {5.00 EUR}  @ 1 Foo {8.00 EUR}
> >          Assets:Broker      -15.00 Foo {10.00 EUR} @ 1 Foo {8.00 EUR}
> >      -------------------
> > 
> >      Now, the problem comes when I buy some actions again, even with another
> >      account:
> >      --------------------
> >      2002/03/20 Buy Actions
> >          Assets:BrokerA      10.00 Foo {5.00 EUR}
> >          Assets:Checking    -50.00 EUR
> > 
> >      2002/03/25 Buy Actions
> >          Assets:BrokerA      15.00 Foo {10.00 EUR}
> >          Assets:Checking   -150.00 EUR
> > 
> >      ; In France, we consider the mean value of an action for taxation
> >      2002/03/25 Price adjustment
> >          Assets:BrokerA      25.00 Foo {8.00 EUR}
> >          Assets:BrokerA     -10.00 Foo {5.00 EUR}  @ 1 Foo {8.00 EUR}
> >          Assets:BrokerA     -15.00 Foo {10.00 EUR} @ 1 Foo {8.00 EUR}
> > 
> >      2002/05/15 Buy Actions
> >          Assets:BrokerB      10.00 Foo {50.00 EUR}
> >          Assets:Checking    -500.00 EUR
> > 
> >      2002/05/25 Buy Actions
> >          Assets:BrokerB      15.00 Foo {100.00 EUR}
> >          Assets:Checking   -1500.00 EUR
> > 
> >      2002/05/25 Price adjustment
> >          Assets:BrokerB      25.00 Foo {80.00 EUR}
> >          Assets:BrokerB     -10.00 Foo {50.00 EUR}  @ 1 Foo {80.00 EUR}
> >          Assets:BrokerB     -15.00 Foo {100.00 EUR} @ 1 Foo {80.00 EUR}
> >      --------------------
> > 
> >      This example fails with "Error: Cannot compare amounts with different
> >      commodities: 'Foo {8.00 EUR}' and 'Foo {80.00 EUR}'"
> > 
> >      I don’t understand where this error comes from, the two accounts are 
> > not
> >      even related...
> > 
> >      Any hint how I could achieve my goal?
> > 
> >      Kind regards,
> > 
> >      --
> > 
> >      ---
> >      You received this message because you are subscribed to the Google
> >      Groups "Ledger" group.
> >      To unsubscribe from this group and stop receiving emails from it, send
> >      an email to [2]ledger-cli+unsubscr...@googlegroups.com.
> >      For more options, visit [3]https://groups.google.com/d/optout.
> > 
> >    --
> > 
> >    ---
> >    You received this message because you are subscribed to the Google Groups
> >    "Ledger" group.
> >    To unsubscribe from this group and stop receiving emails from it, send an
> >    email to [4]ledger-cli+unsubscr...@googlegroups.com.
> >    For more options, visit [5]https://groups.google.com/d/optout.
> > 
> > References
> > 
> >    Visible links
> >    1. mailto:ismael.bouya...@normalesup.org
> >    2. mailto:ledger-cli+unsubscr...@googlegroups.com
> >    3. https://groups.google.com/d/optout
> >    4. mailto:ledger-cli+unsubscr...@googlegroups.com
> >    5. https://groups.google.com/d/optout
> 
> 
> -- 
> Ismael
> 
> -- 
> 
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Ledger" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to ledger-cli+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



-- 
Ismael

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"Ledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ledger-cli+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: PGP signature

Reply via email to