Re: How does --unrealized work?

2019-07-02 Thread Pranesh Prakash
Dear Richar,
Thanks for this very useful example.

I don't use ledger's commodity conversion methods ({}, @, etc.).  I instead 
use the "trading accounts 
"
 
method of recording commodity conversions.  I wanted to test how that 
method would perform with this example.

2018/12/31 Opening Balances
Assets:Stocks 2 AAPL
Trading:Stocks:USD   -2 AAPL
Trading:USD:Stocks  $1000.00
Equity:Opening Balances$-1000.00

P 2019/02/01 AAPL $540.00

2019/02/02 Sell share
Assets:Stocks-1 AAPL
Trading:Stocks:USD1 AAPL
Trading:USD:Stocks  $-500.00
Income:Capital Gain  $-40.00
Assets:Checking  $540.00

P 2019/02/01 AAPL $540.00

2019/02/10 Buy share
Assets:Stocks 1 AAPL
Trading:Stocks:USD   -1 AAPL
Trading:USD:Stocks   $490.00
Assets:Checking $-490.00

P 2019/03/01 AAPL $525


I query unrealized gains+losses using 

$ ledger -f temp.ledg bal Trading -X '$' 
 $-60.00  Trading
   $-1050.00Stocks:USD
 $990.00USD:Stocks

 $-60.00

This shows the unrealized gains correctly at $-60.00 (it's unrealized 
income, so the sign is negative).  Similarly, if you record the lot price 
as '@ 500.00', you can get similar results with your example:


2018/12/31 Opening Balances
Assets:Stocks 2 AAPL @ $500.00
Equity:Opening Balances

P 2019/02/01 AAPL $540.00

2019/02/02 Broker
Assets:Stocks-1 AAPL @ $500.00
Income:Capital Gains $-40.00
Assets:Checking  $540.00

P 2019/02/10 AAPL $490.00

2019/02/10 Broker
Assets:Stocks 1 AAPL @ $490.00
Assets:Checking $-490.00

P 2019/03/01 AAPL $525.00


if you query for the unrealized gain now, you get:

$ ledger -f temp.ldg bal -X '$' --unrealized

$1100.00  Assets
  $50.00Checking
$1050.00Stocks
   $-1060.00  Equity
   $-1000.00Opening Balances
 $-70.00Unrealized Gains
  $10.00Unrealized Losses
 $-40.00  Income:Capital Gains

   0


That shows the correct figure.  However, if you follow the ledger syntax of 
recording sales as "1 AAPL {$500.00} @ $540.00", it doesn't work.  

Now for the additional bug:
As per the ledger manual 
, "1 
AAPL {$500.00}" and "1 AAPL @ $500.00" are equivalent.  However, I get 
different results depending on how I record the initial transaction.  

2018/12/31 Opening Balances
Assets:Stocks  2 AAPL {$500}
Equity:Opening Balances

P 2019/02/01 AAPL $540

2019/02/02 Broker
Assets:Stocks -1 AAPL @ $500 ;@ $540.00
Income:Capital Gains-$40.00
Assets:Checking $540.00

P 2019/02/10 AAPL $490

2019/02/10 Broker
Assets:Stocks  1 AAPL @ $490
Assets:Checking$-490.00

P 2019/03/01 AAPL $525

---

$ ledger -f temp.ldg bal -X '$' --unrealized
$1100.00  Assets
  $50.00Checking
$1050.00Stocks
   $-1060.00  Equity
   $-1050.00Opening Balances
 $-10.00Unrealized Gains
 $-40.00  Income:Capital Gains

   0


Instead of "Unrealized Gains" being "$-70.00" and "Unrealized Losses" being 
"$10.00", I get "Unrealized Gains" of "$-10.00", and "Opening Balances" of 
"$-1050.00" (rather than "$-1000.00").  If I use the {} syntax instead of 
the @ syntax, "Opening Balances" seems to use "AAPL" as its commodity, 
rather than "$".  Go figure.

Regards,
Pranesh



On Saturday, 9 March 2019 02:40:49 UTC+5:30, Richard Lawrence wrote:
>
> Hi everyone, 
>
> In trying to answer Chary's question, I have realized I do not have a 
> very good grip on how to think about "unrealized" gains and losses, and 
> how ledger calculates them.  So I wrote a few test cases to help explain 
> it to myself.  (Can you tell I'm procrastinating a little bit? ;) I am 
> not sure, but I *may* have discovered a bug: see Example 3.  Can someone 
> explain this to me? 
>
> Example 1: you hold a certain quantity of stock throughout the 
> entire period, during which its value fluctuates.  Say you hold 2 
> shares of AAPL.  At the beginning of the 

Re: Possible bug: Re: How does --unrealized work?

2019-03-29 Thread Dániel Fancsali
Hello,

I think, the question of where ledger gets it is answered by this:

$ ledger reg -f list.ldg --revalued -X '$' -w

18-Dec-31 Opening Balances   Assets:Stocks
$1000$1000

 Equity:Opening Balances
$-10000

19-Feb-01 Commodities revalued   
  $80  $80

19-Feb-02 Broker Assets:Stocks
$-540$-460

 Assets:Checking
  $540  $80

19-Feb-10 Commodities revalued   
  $-50  $30

19-Feb-10 Broker Assets:Stocks
  $490 $520

 Assets:Checking
$-490  $30

19-Mar-01 Commodities revalued   
  $70 $100


It seems to me it is the gains are the sum of the positive, while the
losses the negative revaluations.

What I couldn't wrap my head around is this:

MAC-0073934:Private f7745312$ ledger reg -f list.ldg --lot-prices -w

18-Dec-31 Opening Balances   Assets:Stocks
2 AAPL {$500}2 AAPL {$500}

 Equity:Opening Balances
$-1000   $-1000


  2 AAPL {$500}

19-Feb-02 Broker Assets:Stocks
-1 AAPL {$540}   $-1000


  2 AAPL {$500}


-1 AAPL {$540}

 Assets:Checking
  $540$-460


  2 AAPL {$500}


-1 AAPL {$540}

19-Feb-10 Broker Assets:Stocks
1 AAPL {$490}$-460


  1 AAPL {$490}


  2 AAPL {$500}


-1 AAPL {$540}

 Assets:Checking
$-490$-950


  1 AAPL {$490}


  2 AAPL {$500}


-1 AAPL {$540}
Does that mean, ledger is unable to decide which one I really sold? Or am I
missing the point of the whole lot-pricing system?

Regards,
Dan

On Fri, 29 Mar 2019 at 06:28, Martin Michlmayr  wrote:

> * Richard Lawrence  [2019-03-22 13:10]:
> > I never got a reply, so since it's possible there's a bug here, I
> > thought I would bump this message.  Does anyone have any insights about
> > my Example 3?
>
> I think we need John to comment on this.  It certainly looks incorrect
> to me.
> --
> Martin Michlmayr
> https://www.cyrius.com/
>
> --
>
> ---
> 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.


Re: Possible bug: Re: How does --unrealized work?

2019-03-29 Thread Martin Michlmayr
* Richard Lawrence  [2019-03-22 13:10]:
> I never got a reply, so since it's possible there's a bug here, I
> thought I would bump this message.  Does anyone have any insights about
> my Example 3?

I think we need John to comment on this.  It certainly looks incorrect
to me.
-- 
Martin Michlmayr
https://www.cyrius.com/

-- 

--- 
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.


Possible bug: Re: How does --unrealized work?

2019-03-22 Thread Richard Lawrence
Hi all,

I never got a reply, so since it's possible there's a bug here, I
thought I would bump this message.  Does anyone have any insights about
my Example 3?

Best,
Richard

Richard Lawrence  writes:

> Hi everyone,
>
> In trying to answer Chary's question, I have realized I do not have a
> very good grip on how to think about "unrealized" gains and losses, and
> how ledger calculates them.  So I wrote a few test cases to help explain
> it to myself.  (Can you tell I'm procrastinating a little bit? ;) I am
> not sure, but I *may* have discovered a bug: see Example 3.  Can someone
> explain this to me?
>
> Example 1: you hold a certain quantity of stock throughout the
> entire period, during which its value fluctuates.  Say you hold 2
> shares of AAPL.  At the beginning of the period, each is worth $500.
> At the end, each is worth $525.
>
> 2018/12/31 Opening Balances
> Assets:Stocks  2 AAPL @ $500
> Equity:Opening Balances
>
> P 2019/01/01 AAPL $500
> P 2019/02/01 AAPL $515
> P 2019/03/01 AAPL $525
>
> The unrealized gain is then:
>   (2 AAPL * $525) - (2 AAPL * $500) = $1050 - $1000 = $50
> i.e.,
>   (end quantity * end price) - (beginning quantity * beginning price) 
>
> That is exactly what ledger reports, with --unrealized:
>
> ledger -f test.journal bal Assets:Stocks -X '$' --unrealized
>
>$1050  Assets:Stocks
> $-50  Equity:Unrealized Gains
> 
>$1000
>
> ==
> Example 2: you hold a certain quantity of stock throughout the
> entire period, but you also acquire more at some point in the
> middle.  Say you hold 2 AAPL at the beginning of the period, each
> worth $500. At some later point, when AAPL is up to $510, you buy
> one more share.  At the end of the period, all three shares are
> worth $525.  
>
> 2018/12/31 Opening Balances
> Assets:Stocks  2 AAPL @ $500
> Assets:Checking $1000.00
> Equity:Opening Balances
>
> P 2019/01/01 AAPL $500
>
> P 2019/02/01 AAPL $510
>
> 2019/02/02 Broker
> Assets:Stocks  1 AAPL @ $510.00
> Assets:Checking
> 
> P 2019/03/01 AAPL $525
>
> I would then expect the unrealized gain to be:
>   (3 AAPL * $525) - (2 AAPL * $500 + 1 AAPL * $510) = $65
>
> Again, this is just what ledger says:
>
> ledger -f test.journal bal Assets:Stocks -X '$' --unrealized 
> $1575.00  Assets:Stocks
>  $-65.00  Equity:Unrealized Gains
> 
> $1510.00
>
> ==
> Now for the problematic example:
>
> Example 3: you sell some stock at one point when the price
> rises, and reacquire it when the price drops.  Say you start holding
> 2 shares of AAPL, each worth $500.  Then, when the price is up to
> $540, you sell one share, resulting in a realized gain of $40.
> Later, the price drops to $490 and you buy another share.  At the
> end of the period, the price is $525, and you also have $50 in the bank.
>
> 2018/12/31 Opening Balances
> Assets:Stocks  2 AAPL @ $500
> Equity:Opening Balances
>
> P 2019/02/01 AAPL $540
>
> 2019/02/02 Broker
> Assets:Stocks -1 AAPL @ $540.00
> Assets:Checking
>
> P 2019/02/10 AAPL $490
>
> 2019/02/10 Broker
> Assets:Stocks  1 AAPL @ $490
> Assets:Checking
>   
> P 2019/03/01 AAPL $525
>
> Now, what is the unrealized gain here?  First of all, it's not clear how
> this should be calculated.  I thought of two reasonable ways to
> calculate it, which give different results. From one perspective, this
> example is the same as Example 1: you started and ended the period with
> 2 shares of AAPL, worth $500 each at the beginning and $525 at the end,
> for an unrealized gain of $50.  But following the logic of Example 2,
> counting unrealized gains only from the first time the share is
> acquired, we'd get:
>   $525 - $500 = $25 unrealized gain for Share #1 (held the whole period)
>   $525 - $490 = $35 unrealized gain for Share #3 (bought on 2019/02/10)
> leading to a total of $60 of unrealized gain.   
>
> Ledger actually does it differently.  It says we have $150 in
> unrealized gains, and $50 in unrealized losses:
>
> ledger -f test.journal bal Assets:Stocks -X '$' --unrealized 
> $1050.00  Assets:Stocks
> $-100.00  Equity
> $-150.00Unrealized Gains
>   $50.00Unrealized Losses
> 
>  $950.00
>
> Huh?  Here's what I think is going on:
>
> Consider Share #1, held for the whole period.
> It first gained $40 in value, lost $50, and then gained $35.
> So, $75 in unrealized gains, $50 in unrealized losses.
>
> Share #3 was bought at $490 and then moved to $525 at the end of 

Re: How does --unrealized work?

2019-03-12 Thread Chary Chary
Richard,

I have read your investigation, did the same testing and I agree with you. 
It is not logical for me either, how ledger  shows unrealized gains in your 
example.

But this is actually another reason I was looking for a such single "total 
income statement-like report", because one could use it for verification.

A delta between Net Worth of 2 balance sheets reports shall always be equal 
to total Net Income  over the time period between these 2 Balance Sheet 
reports.  This shall be true for any period and when expressed in any 
currency (-X option)

Regards.

On Friday, March 8, 2019 at 10:10:49 PM UTC+1, Richard Lawrence wrote:
>
> Hi everyone, 
>
> In trying to answer Chary's question, I have realized I do not have a 
> very good grip on how to think about "unrealized" gains and losses, and 
> how ledger calculates them.  So I wrote a few test cases to help explain 
> it to myself.  (Can you tell I'm procrastinating a little bit? ;) I am 
> not sure, but I *may* have discovered a bug: see Example 3.  Can someone 
> explain this to me? 
>
> Example 1: you hold a certain quantity of stock throughout the 
> entire period, during which its value fluctuates.  Say you hold 2 
> shares of AAPL.  At the beginning of the period, each is worth $500. 
> At the end, each is worth $525. 
>
> 2018/12/31 Opening Balances 
> Assets:Stocks  2 AAPL @ $500 
> Equity:Opening Balances 
>
> P 2019/01/01 AAPL $500 
> P 2019/02/01 AAPL $515 
> P 2019/03/01 AAPL $525 
>
> The unrealized gain is then: 
>   (2 AAPL * $525) - (2 AAPL * $500) = $1050 - $1000 = $50 
> i.e., 
>   (end quantity * end price) - (beginning quantity * beginning price) 
>
> That is exactly what ledger reports, with --unrealized: 
>
> ledger -f test.journal bal Assets:Stocks -X '$' --unrealized 
>
>$1050  Assets:Stocks 
> $-50  Equity:Unrealized Gains 
>  
>$1000 
>
> == 
> Example 2: you hold a certain quantity of stock throughout the 
> entire period, but you also acquire more at some point in the 
> middle.  Say you hold 2 AAPL at the beginning of the period, each 
> worth $500. At some later point, when AAPL is up to $510, you buy 
> one more share.  At the end of the period, all three shares are 
> worth $525.   
>
> 2018/12/31 Opening Balances 
> Assets:Stocks  2 AAPL @ $500 
> Assets:Checking $1000.00 
> Equity:Opening Balances 
>
> P 2019/01/01 AAPL $500 
>
> P 2019/02/01 AAPL $510 
>
> 2019/02/02 Broker 
> Assets:Stocks  1 AAPL @ $510.00 
> Assets:Checking 
> 
> P 2019/03/01 AAPL $525 
>
> I would then expect the unrealized gain to be: 
>   (3 AAPL * $525) - (2 AAPL * $500 + 1 AAPL * $510) = $65 
>
> Again, this is just what ledger says: 
>
> ledger -f test.journal bal Assets:Stocks -X '$' --unrealized 
> $1575.00  Assets:Stocks 
>  $-65.00  Equity:Unrealized Gains 
>  
> $1510.00 
>
> == 
> Now for the problematic example: 
>
> Example 3: you sell some stock at one point when the price 
> rises, and reacquire it when the price drops.  Say you start holding 
> 2 shares of AAPL, each worth $500.  Then, when the price is up to 
> $540, you sell one share, resulting in a realized gain of $40. 
> Later, the price drops to $490 and you buy another share.  At the 
> end of the period, the price is $525, and you also have $50 in the bank. 
>
> 2018/12/31 Opening Balances 
> Assets:Stocks  2 AAPL @ $500 
> Equity:Opening Balances 
>
> P 2019/02/01 AAPL $540 
>
> 2019/02/02 Broker 
> Assets:Stocks -1 AAPL @ $540.00 
> Assets:Checking 
>
> P 2019/02/10 AAPL $490 
>
> 2019/02/10 Broker 
> Assets:Stocks  1 AAPL @ $490 
> Assets:Checking 
>   
> P 2019/03/01 AAPL $525 
>
> Now, what is the unrealized gain here?  First of all, it's not clear how 
> this should be calculated.  I thought of two reasonable ways to 
> calculate it, which give different results. From one perspective, this 
> example is the same as Example 1: you started and ended the period with 
> 2 shares of AAPL, worth $500 each at the beginning and $525 at the end, 
> for an unrealized gain of $50.  But following the logic of Example 2, 
> counting unrealized gains only from the first time the share is 
> acquired, we'd get: 
>   $525 - $500 = $25 unrealized gain for Share #1 (held the whole period) 
>   $525 - $490 = $35 unrealized gain for Share #3 (bought on 2019/02/10) 
> leading to a total of $60 of unrealized gain.   
>
> Ledger actually does it differently.  It says we have $150 in 
> unrealized gains, and $50 in unrealized losses: 
>
> ledger -f 

How does --unrealized work?

2019-03-08 Thread Richard Lawrence
Hi everyone,

In trying to answer Chary's question, I have realized I do not have a
very good grip on how to think about "unrealized" gains and losses, and
how ledger calculates them.  So I wrote a few test cases to help explain
it to myself.  (Can you tell I'm procrastinating a little bit? ;) I am
not sure, but I *may* have discovered a bug: see Example 3.  Can someone
explain this to me?

Example 1: you hold a certain quantity of stock throughout the
entire period, during which its value fluctuates.  Say you hold 2
shares of AAPL.  At the beginning of the period, each is worth $500.
At the end, each is worth $525.

2018/12/31 Opening Balances
Assets:Stocks  2 AAPL @ $500
Equity:Opening Balances

P 2019/01/01 AAPL $500
P 2019/02/01 AAPL $515
P 2019/03/01 AAPL $525

The unrealized gain is then:
  (2 AAPL * $525) - (2 AAPL * $500) = $1050 - $1000 = $50
i.e.,
  (end quantity * end price) - (beginning quantity * beginning price) 

That is exactly what ledger reports, with --unrealized:

ledger -f test.journal bal Assets:Stocks -X '$' --unrealized

   $1050  Assets:Stocks
$-50  Equity:Unrealized Gains

   $1000

==
Example 2: you hold a certain quantity of stock throughout the
entire period, but you also acquire more at some point in the
middle.  Say you hold 2 AAPL at the beginning of the period, each
worth $500. At some later point, when AAPL is up to $510, you buy
one more share.  At the end of the period, all three shares are
worth $525.  

2018/12/31 Opening Balances
Assets:Stocks  2 AAPL @ $500
Assets:Checking $1000.00
Equity:Opening Balances

P 2019/01/01 AAPL $500

P 2019/02/01 AAPL $510

2019/02/02 Broker
Assets:Stocks  1 AAPL @ $510.00
Assets:Checking

P 2019/03/01 AAPL $525

I would then expect the unrealized gain to be:
  (3 AAPL * $525) - (2 AAPL * $500 + 1 AAPL * $510) = $65

Again, this is just what ledger says:

ledger -f test.journal bal Assets:Stocks -X '$' --unrealized 
$1575.00  Assets:Stocks
 $-65.00  Equity:Unrealized Gains

$1510.00

==
Now for the problematic example:

Example 3: you sell some stock at one point when the price
rises, and reacquire it when the price drops.  Say you start holding
2 shares of AAPL, each worth $500.  Then, when the price is up to
$540, you sell one share, resulting in a realized gain of $40.
Later, the price drops to $490 and you buy another share.  At the
end of the period, the price is $525, and you also have $50 in the bank.

2018/12/31 Opening Balances
Assets:Stocks  2 AAPL @ $500
Equity:Opening Balances

P 2019/02/01 AAPL $540

2019/02/02 Broker
Assets:Stocks -1 AAPL @ $540.00
Assets:Checking

P 2019/02/10 AAPL $490

2019/02/10 Broker
Assets:Stocks  1 AAPL @ $490
Assets:Checking
  
P 2019/03/01 AAPL $525

Now, what is the unrealized gain here?  First of all, it's not clear how
this should be calculated.  I thought of two reasonable ways to
calculate it, which give different results. From one perspective, this
example is the same as Example 1: you started and ended the period with
2 shares of AAPL, worth $500 each at the beginning and $525 at the end,
for an unrealized gain of $50.  But following the logic of Example 2,
counting unrealized gains only from the first time the share is
acquired, we'd get:
  $525 - $500 = $25 unrealized gain for Share #1 (held the whole period)
  $525 - $490 = $35 unrealized gain for Share #3 (bought on 2019/02/10)
leading to a total of $60 of unrealized gain.   

Ledger actually does it differently.  It says we have $150 in
unrealized gains, and $50 in unrealized losses:

ledger -f test.journal bal Assets:Stocks -X '$' --unrealized 
$1050.00  Assets:Stocks
$-100.00  Equity
$-150.00Unrealized Gains
  $50.00Unrealized Losses

 $950.00

Huh?  Here's what I think is going on:

Consider Share #1, held for the whole period.
It first gained $40 in value, lost $50, and then gained $35.
So, $75 in unrealized gains, $50 in unrealized losses.

Share #3 was bought at $490 and then moved to $525 at the end of the period.
So, that's another $35 in unrealized gains.
Together with Share #1's unrealized gains, that's a total of $110.

Share #2 was sold at the first price move, on 2019/02/02.  As far as I
can see, it should have *no* unrealized gains or losses.  (It has,
instead, a *realized* gain of $40.)

Ledger reports $150 in unrealized gains rather than $110, so it must be
including that $40 from Share #2 as unrealized gains.  That