How to add new command line option to ledger?

44 views
Skip to first unread message

tripun goel

unread,
Jun 18, 2014, 11:00:49 AM6/18/14
to ledge...@googlegroups.com
I am experimenting with ledger and I am trying to add new command line option or a directive (whichever is easier). How to add a filter that shall round an amount to desired precision by rounding after performing required calculations (such as calculation values using lot price) on per transaction basis.
For example , this posting.
2014/06/18  TEST
 A                               10 AAA  @ 10.1234 EUR
 A                               10 BBB  @ 10.5692 EUR
 C
if we set a precision of 2
this should be calculated as 10*10.1234 = 101.234 rounded to 101.23 and 10*10.5692 rounded to 105.69  and final result is 101.23+105.69=206.92 EUR debited from C.

My question are :-
1. How does option layer works? What is it structure?
2. What are chain pre-post and chain-post handlers?
3. How to add a new '--' option to command line or add a directive?
Please provide at least a rough outline.

Martin Michlmayr

unread,
Jun 18, 2014, 8:57:50 PM6/18/14
to ledge...@googlegroups.com
* tripun goel <tri...@gmail.com> [2014-06-18 08:00]:
> 2. What are chain pre-post and chain-post handlers?

I don't know, and maybe this is the right place to implement it... but
I'm wondering if the rounding shouldn't take place somewhere in
src/amount.cc.

Save your example in a file and run:
ledger -f test.ledger bal --debug amount
to start with.

> 3. How to add a new '--' option to command line or add a directive?

To add a command line option, you need to add an OPT() so the command
line option is parsed correctly. You also need an OPTION() so it's
handled correctly. grep the source to see how other command line
options are handled.

To add a commodity or account directive, look at src/textual.cc

bool instance_t::general_directive(char * line)

For account directives:
instance_t::account_directive

For commodity directives:
instance_t::commodity_directive

--
Martin Michlmayr
http://www.cyrius.com/

Martin Michlmayr

unread,
Jun 19, 2014, 8:13:30 AM6/19/14
to ledge...@googlegroups.com
* Martin Michlmayr <t...@cyrius.com> [2014-06-18 20:57]:
> I don't know, and maybe this is the right place to implement it... but
> I'm wondering if the rounding shouldn't take place somewhere in
> src/amount.cc.

Now that I think about it some more, aren't all rounding problems
we've encountered so far related to valuing one commodity in terms of
another? If so, you'd need to find where this is done and implement
rounding there.

tripun goel

unread,
Jun 20, 2014, 10:14:25 AM6/20/14
to ledge...@googlegroups.com


I am debugging using code amount. The amount is converted to rational after parsing. No case of rounding.  All calculations performed likewise, changing this is very radical. I will follow up with this and post more information on this.

John Wiegley

unread,
Jun 30, 2014, 4:53:05 PM6/30/14
to ledge...@googlegroups.com
>>>>> Martin Michlmayr <t...@cyrius.com> writes:

> Now that I think about it some more, aren't all rounding problems we've
> encountered so far related to valuing one commodity in terms of another? If
> so, you'd need to find where this is done and implement rounding there.

That would be commodity_pool_t::exchange, in pool.cc, if that is the case.

John

tripun goel

unread,
Jul 1, 2014, 9:36:24 AM7/1/14
to ledge...@googlegroups.com
Hey John,

I found that there are two exchange functions in pool.cc both of which are called after in_place_round in amount.cc where I have implemented rounding currently.  Although the patch is not thoroughly tested but I have added 2-3 common tests for the same. Do you think it can not be the right place to add this feature?
I have inserted extra debug statements in my fork(in debug branch), below is an example log

D 1000.00 EUR
commodity EUR
  precision 2
  default
  
2012-01-01 *
   A                        2 AAA @ 10.122 EUR
   A                        1 BBB @ 20.123 EUR
   C

-V bal --debug amount.parse

    0ms  [INFO]  Ledger starting
    2ms  [DEBUG] Rational parsed = 1
    3ms  [DEBUG] amount.cc: parse
    3ms  [DEBUG] amount.cc: parse
    3ms  [DEBUG] Rational parsed = 1
    4ms  [DEBUG] amount.cc: parse
    4ms  [DEBUG] amount.cc: parse
    6ms  [INFO]  Parsing file "test.txt"
    6ms  [DEBUG] Rational parsed = 1000
    6ms  [DEBUG] amount.cc: parse
    7ms  [DEBUG] precision directive called 2
    7ms  [DEBUG] amount.cc: parse
    7ms  [DEBUG] Rational parsed = 5061/500
    7ms  [DEBUG] amount.cc: parse
    8ms  [DEBUG] amount.cc: parse
    8ms  [DEBUG] Rational parsed = 20123/1000
    8ms  [DEBUG] amount.cc: parse
    9ms  [DEBUG] amount.cc:in place round commodity EUR value 20.24
    9ms  [DEBUG] amount.cc:in place round commodity EUR value 20.12
    9ms  [DEBUG] amount.cc:+= value before 20.24
   10ms  [DEBUG] amount.cc:+= value after 40.36 2nd amt 20.12
   10ms  [DEBUG] pool.cc: cost_breakdown_t exchange cost comm EUR amount comm AAA
   10ms  [DEBUG] amount.cc: is_zero
   10ms  [DEBUG] pool.cc: void exchange
   11ms  [DEBUG] pool.cc: cost_breakdown_t exchange cost comm EUR amount comm BBB
   11ms  [DEBUG] amount.cc: is_zero
   11ms  [DEBUG] pool.cc: void exchange
   14ms  [INFO]  Read journal file (8ms)
   14ms  [INFO]  Found 1 transactions
   15ms  [DEBUG] amount.cc: parse
   15ms  [DEBUG] amount.cc: parse
   20ms  [DEBUG] post.cc:get_amount wrapper in lookup
   21ms  [DEBUG] post.cc:get_amount
   21ms  [DEBUG] filters.cc:calc_posts
   21ms  [DEBUG] post.cc:get_amount
   21ms  [DEBUG] filters.cc:calc_posts
   21ms  [DEBUG] post.cc:get_amount
   26ms  [DEBUG] account.cc: total 
   31ms  [DEBUG] amount.cc:in place round commodity EUR value 20.12
   32ms  [DEBUG] amount.cc:in place round commodity EUR value 20.24
   32ms  [DEBUG] balance.cc: amount added, value before 20.12
   32ms  [DEBUG] amount.cc:+= value before 20.12
   33ms  [DEBUG] amount.cc:+= value after 40.36 2nd amt 20.24
   33ms  [DEBUG] balance.cc: amount added, value after 40.36
   33ms  [DEBUG] amount.cc: is_zero
   34ms  [DEBUG] account.cc: total 
   34ms  [DEBUG] amount.cc: is_zero
   34ms  [DEBUG] format.cc:real_calc formatting
   36ms  [DEBUG] amount.cc:+= value before 20
   36ms  [DEBUG] amount.cc:+= value after 20 2nd amt 0
   37ms  [DEBUG] amount.cc:in place round commodity EUR value 20.12
   37ms  [DEBUG] amount.cc:in place round commodity EUR value 20.24
   37ms  [DEBUG] balance.cc: amount added, value before 20.12
   38ms  [DEBUG] amount.cc:+= value before 20.12
   38ms  [DEBUG] amount.cc:+= value after 40.36 2nd amt 20.24
   38ms  [DEBUG] balance.cc: amount added, value after 40.36
   38ms  [DEBUG] amount.cc: is_zero
           40.36 EUR  A
   40ms  [DEBUG] format.cc:real_calc formatting
   41ms  [DEBUG] amount.cc:+= value before 20
   41ms  [DEBUG] amount.cc:+= value after 20 2nd amt 0
   41ms  [DEBUG] amount.cc: is_zero
   42ms  [DEBUG] amount.cc: is_zero
          -40.36 EUR  C
   43ms  [DEBUG] format.cc:real_calc formatting
--------------------
   43ms  [DEBUG] format.cc:real_calc formatting
   44ms  [DEBUG] amount.cc:+= value before 20
   45ms  [DEBUG] amount.cc:+= value after 20 2nd amt 0
   45ms  [DEBUG] amount.cc:in place round commodity EUR value 20.12
   46ms  [DEBUG] balance.cc: amount added, value before -40.36
   46ms  [DEBUG] amount.cc:+= value before -40.36
   46ms  [DEBUG] amount.cc:+= value after -20.24 2nd amt 20.12
   47ms  [DEBUG] balance.cc: amount added, value after -20.24
   47ms  [DEBUG] amount.cc:in place round commodity EUR value 20.24
   47ms  [DEBUG] balance.cc: amount added, value before -20.24
   48ms  [DEBUG] amount.cc:+= value before -20.24
   48ms  [DEBUG] amount.cc:+= value after 0 2nd amt 20.24
   48ms  [DEBUG] balance.cc: amount added, value after 0
                   0
   49ms  [INFO]  Finished executing command (29ms)
   49ms  [INFO]  Ledger ended




--

---
You received this message because you are subscribed to a topic in the Google Groups "Ledger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ledger-cli/xGgpdJCGCq4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ledger-cli+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tripun goel

unread,
Jul 1, 2014, 10:55:11 AM7/1/14
to ledge...@googlegroups.com
 Another point is that exchange in pool.cc isn't even called in case of fixated price. However , this time in_place_round is called after calc_posts unlike before.
Here is a log
D 1000.00 EUR
commodity EUR
  precision 2
  default
  
2012-01-01 *
   A                        2 AAA {=10.122 EUR}
   A                        1 BBB {=20.123 EUR}
   C


-V bal --debug amount.parse

    0ms  [INFO]  Ledger starting
    5ms  [DEBUG] Rational parsed = 1
    5ms  [DEBUG] amount.cc: parse
    5ms  [DEBUG] amount.cc: parse
    6ms  [DEBUG] Rational parsed = 1
    6ms  [DEBUG] amount.cc: parse
    7ms  [DEBUG] amount.cc: parse
   12ms  [INFO]  Parsing file "test.txt"
   13ms  [DEBUG] Rational parsed = 1000
   13ms  [DEBUG] amount.cc: parse
   14ms  [DEBUG] precision directive called 2
   15ms  [DEBUG] Rational parsed = 5061/500
   15ms  [DEBUG] amount.cc: parse
   16ms  [DEBUG] amount.cc: parse
   16ms  [DEBUG] Rational parsed = 20123/1000
   16ms  [DEBUG] amount.cc: parse
   17ms  [DEBUG] amount.cc: parse
   18ms  [DEBUG] amount.cc: is_zero
   18ms  [DEBUG] amount.cc: is_zero
   19ms  [INFO]  Read journal file (11ms)
   19ms  [INFO]  Found 1 transactions
   20ms  [DEBUG] amount.cc: parse
   20ms  [DEBUG] amount.cc: parse
   22ms  [DEBUG] post.cc:get_amount wrapper in lookup
   22ms  [DEBUG] post.cc:get_amount
   23ms  [DEBUG] filters.cc:calc_posts
   23ms  [DEBUG] post.cc:get_amount
   23ms  [DEBUG] filters.cc:calc_posts
   23ms  [DEBUG] post.cc:get_amount
   23ms  [DEBUG] filters.cc:calc_posts
   23ms  [DEBUG] post.cc:get_amount
   25ms  [DEBUG] account.cc: total 
   27ms  [DEBUG] amount.cc:in place round commodity EUR value 20.24
   27ms  [DEBUG] amount.cc:in place round commodity EUR value 20.12
   27ms  [DEBUG] balance.cc: amount added, value before 20.24
   27ms  [DEBUG] amount.cc:+= value before 20.24
   28ms  [DEBUG] amount.cc:+= value after 40.36 2nd amt 20.12
   28ms  [DEBUG] balance.cc: amount added, value after 40.36
   29ms  [DEBUG] amount.cc: is_zero
   34ms  [DEBUG] account.cc: total 
   35ms  [DEBUG] amount.cc:in place round commodity EUR value -20.24
   35ms  [DEBUG] amount.cc:in place round commodity EUR value -20.12
   36ms  [DEBUG] balance.cc: amount added, value before -20.24
   36ms  [DEBUG] amount.cc:+= value before -20.24
   36ms  [DEBUG] amount.cc:+= value after -40.36 2nd amt -20.12
   36ms  [DEBUG] balance.cc: amount added, value after -40.36
   37ms  [DEBUG] amount.cc: is_zero
   37ms  [DEBUG] format.cc:real_calc formatting
   39ms  [DEBUG] amount.cc:+= value before 20
   39ms  [DEBUG] amount.cc:+= value after 20 2nd amt 0
   40ms  [DEBUG] amount.cc:in place round commodity EUR value 20.24
   40ms  [DEBUG] amount.cc:in place round commodity EUR value 20.12
   40ms  [DEBUG] balance.cc: amount added, value before 20.24
   40ms  [DEBUG] amount.cc:+= value before 20.24
   40ms  [DEBUG] amount.cc:+= value after 40.36 2nd amt 20.12
   41ms  [DEBUG] balance.cc: amount added, value after 40.36
   41ms  [DEBUG] amount.cc: is_zero
           40.36 EUR  A
   43ms  [DEBUG] format.cc:real_calc formatting
   44ms  [DEBUG] amount.cc:+= value before 20
   44ms  [DEBUG] amount.cc:+= value after 20 2nd amt 0
   45ms  [DEBUG] amount.cc:in place round commodity EUR value -20.24
   45ms  [DEBUG] amount.cc:in place round commodity EUR value -20.12
   45ms  [DEBUG] balance.cc: amount added, value before -20.24
   46ms  [DEBUG] amount.cc:+= value before -20.24
   46ms  [DEBUG] amount.cc:+= value after -40.36 2nd amt -20.12
   46ms  [DEBUG] balance.cc: amount added, value after -40.36
   46ms  [DEBUG] amount.cc: is_zero
          -40.36 EUR  C
   47ms  [DEBUG] format.cc:real_calc formatting
--------------------
   49ms  [DEBUG] format.cc:real_calc formatting
   50ms  [DEBUG] amount.cc:+= value before 20
   50ms  [DEBUG] amount.cc:+= value after 20 2nd amt 0
   51ms  [DEBUG] balance.cc: amount added, value before 2
   51ms  [DEBUG] amount.cc:+= value before 2
   51ms  [DEBUG] amount.cc:+= value after 0 2nd amt -2
   51ms  [DEBUG] balance.cc: amount added, value after 0
   51ms  [DEBUG] balance.cc: amount added, value before 1
   51ms  [DEBUG] amount.cc:+= value before 1
   52ms  [DEBUG] amount.cc:+= value after 0 2nd amt -1
   52ms  [DEBUG] balance.cc: amount added, value after 0
                   0
   53ms  [INFO]  Finished executing command (30ms)
   53ms  [INFO]  Ledger ended
Reply all
Reply to author
Forward
0 new messages