yccheok / jstock

JStock is a free stock market software for 28 countries.
https://jstock.org
664 stars 348 forks source link

Add annual return/xirr to portfolio #7

Open tarioch opened 10 years ago

tarioch commented 10 years ago

I would really love if the portfolio view would show the annual return as this would help compare between different investments that have been taken for different durations.

yccheok commented 10 years ago

Hi tarioch,

Yes. This is in fact one of my top wished feature. I would like to know, what is my return of investment in yearly basic. i.e., am I getting 20% return every year?

To calculate return,

net worth end of the year - net worth beginning of the year

To calculate net worth, we need to take consideration into

Thing doesn't straight forward. We need some proper planning before kick start on this feature.

yccheok commented 10 years ago

Here's the trello card : https://trello.com/c/E0pvlWqT

tarioch commented 10 years ago

I found a java implementation for xirr, might come in handy: https://github.com/ept/jxirr

Correct me if I'm wrong but what works quite well is every transaction is listed with the value, e.g.

-1000 buy stock +200 dividend +500 sell stock -1000 buy stock +2000 current stock value

(internal transactions such as splits, direct reinvestment and so on can be ignored)

now using the dates of the transactions, using xirr on this works quite well for me in a spreadsheet.

yccheok commented 10 years ago

In fact, this is one of my top wishlist features. My ideal case is drawn in the following picture.

img_20140311_215626

tarioch commented 10 years ago

Here are some more thoughts and a possible way to do it.

xirr takes into account the actual dates of value relevant actions and computes an interest rate, basically it tells me if, instead of investing in a stock, I would have put the same money transactions into an account, what would have been the interest rate of the account.

Could your chart be done like this:

Dividend gain rate xirr with -buy transaction: cash value -sell transaction: purchase value (not cash value) -not directly reinvested dividend: cash value -purchase value at end (not market value)

That way only dividend profits should be taken into account

Sales gain rate xirr with -buy transaction: cash value -sell transaction: cash value -not directly reinvested dividend: ignore -purchase value at end (not market value)

That way only sales gains should be taken into account

Paper gain rate xirr with -buy transaction: cash value -sell transaction: purchase value (not cash value) -not directly reinvested dividend: ignore -market value at end

That way only paper profit should be taken into account

I would also find it great if besides the yearly values, an overall annualized value would be available (given the algorithm above, the only change would be to take all transactions instead of only the ones from one year).

yccheok commented 10 years ago

Hi @tarioch,

Now, I just want to kick start this very exciting feature :)

I came across some obstacle while implementing it (I don't have any format financial or quantitative background)

http://quant.stackexchange.com/questions/12936/calculate-rate-of-return-of-a-stock-if-there-is-a-buy-transaction-occurs-during

Would you like to participate in such discussion?

Thanks.

yccheok commented 10 years ago

Hi @tarioch ,

After spending a few hours studying starting from http://www.mathsisfun.com/money/internal-rate-return.html, I can get a rough understanding on NPV, IRR & XIRR.

Code implementation shouldn't be much trouble, after looking at your pointer on those implementation (Java at GitHub, Stackoverflow....) But, I will surely spend time looking at StackOverflow discussion, as there seems to be edge case where Newton method doesn't work. We need to fall back to BisectionMethod in those edge case. jxirr might not handle those cases (I just did a rough search on keyword "bisection"). But, it is still too early to judge till I run their unit test.

What is more important is, I need to ensure my understanding on this subject is more or less accurate. I got some doubt in my mind still. Do you mind to take a look? http://quant.stackexchange.com/questions/12974/is-there-any-relationship-between-investment-return-of-a-stock-for-individual-y

Thank you.

tarioch commented 10 years ago

The reply you got looks good.

Wikipedia also has a nice article about the bisect algorithm http://en.wikipedia.org/wiki/Bisection_method

And here's an example implementation for xirr http://puneinvestor.wordpress.com/2013/10/01/calculate-xirr-in-ruby-bisection-method/

yccheok commented 10 years ago

Work in progress.

https://github.com/yccheok/xirr

Still need bisect algorithm and more unit test cases.

Contribution is very much welcomed.

yccheok commented 10 years ago

Hi @tarioch,

While implementing, I came across a roadblock, while trying to break down yearly XIRR into different segments.

Would you mind to take a look http://quant.stackexchange.com/questions/14013/break-down-xirr-to-different-segments

Also, I tried to understand your previous thread

Dividend gain rate
xirr with
-buy transaction: cash value
-sell transaction: purchase value (not cash value)
-not directly reinvested dividend: cash value
-purchase value at end (not market value)

I don't understand what do you mean by -sell transaction: purchase value (not cash value) -purchase value at end (not market value)

I would be very much appreciated, if you could provide an example.

Thank you.

tarioch commented 10 years ago

My thought is to ignore all the other profits. So when looking at dividend profits you're not interested in sales or paper profits. Therefore I cancel them out by using the original purchase price when selling them.

e.g.

buy 1000 for 1$ get dividend of 100$ and sell for 2$

Dividend profit would be:

-1000 +100 +1000

vs total profits

-1000 +100 +2000