ValueBuddies.com : Value Investing Forum - Singapore, Hong Kong, U.S.

Full Version: Anyone how to calculate rate of return in the following case?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
All sifus right here,

Currently, I'm implementing a feature of an open source project (https://github.com/yccheok/jstock/issues/7), which requires me to calculate the rate of return of a stock.

Let's take the following scenario.

User purchased 1000 units stock with unit price 1 dollar at 1st January 2013. He didn't perform any sell transaction & buy transaction in between. At 31st December 2013, the stock price reached 2 dollar.

The rate of return of the stock for year 2013 is 100%

(Value of investment at 31st December 2013 - Cost of investment at 1st January 2013) / (Cost of investment at 1st January 2013) * 100%

(1000 * $2 - 1000 * $1) / (1000 * $1) * 100% = 100%

But, what if the user had purchased another 1000 units of same stock with unit price 1 dollar at 1st March 2013. What should be the rate of return of the stock?
(06-07-2014, 01:24 PM)yccheok Wrote: [ -> ]All sifus right here,

Currently, I'm implementing a feature of an open source project (https://github.com/yccheok/jstock/issues/7), which requires me to calculate the rate of return of a stock.

Let's take the following scenario.

User purchased 1000 units stock with unit price 1 dollar at 1st January 2013. He didn't perform any sell transaction & buy transaction in between. At 31st December 2013, the stock price reached 2 dollar.

The rate of return of the stock for year 2013 is 100%

(Value of investment at 31st December 2013 - Cost of investment at 1st January 2013) / (Cost of investment at 1st January 2013) * 100%

(1000 * $2 - 1000 * $1) / (1000 * $1) * 100% = 100%

But, what if the user had purchased another 1000 units of same stock with unit price 1 dollar at 1st March 2013. What should be the rate of return of the stock?
My software says Ann. Ret.=112.6% @ 31/DEC/2013 & @ 62.4%UTD.
TR. ALL =100 %; No change UTD.
Anyone like to prove my software is wrong somewhere?
Thanks!
(06-07-2014, 03:29 PM)Temperament Wrote: [ -> ]
(06-07-2014, 01:24 PM)yccheok Wrote: [ -> ]All sifus right here,

Currently, I'm implementing a feature of an open source project (https://github.com/yccheok/jstock/issues/7), which requires me to calculate the rate of return of a stock.

Let's take the following scenario.

User purchased 1000 units stock with unit price 1 dollar at 1st January 2013. He didn't perform any sell transaction & buy transaction in between. At 31st December 2013, the stock price reached 2 dollar.

The rate of return of the stock for year 2013 is 100%

(Value of investment at 31st December 2013 - Cost of investment at 1st January 2013) / (Cost of investment at 1st January 2013) * 100%

(1000 * $2 - 1000 * $1) / (1000 * $1) * 100% = 100%

But, what if the user had purchased another 1000 units of same stock with unit price 1 dollar at 1st March 2013. What should be the rate of return of the stock?
My software says Ann. Ret.=112.6% @ 31/DEC/2013 & @ 62.4%UTD.
TR. ALL =100 %; No change UTD.
Anyone like to prove my software is wrong somewhere?
Thanks!
i have a question? Can you keep Ann Ret in your pocket or TR. ALL in your pocket? At the end of the day, what's really in our pocket that counts?
(06-07-2014, 01:24 PM)yccheok Wrote: [ -> ]User purchased 1000 units stock with unit price 1 dollar at 1st January 2013. At 31st December 2013, the stock price reached 2 dollar.

But, what if the user had purchased another 1000 units of same stock with unit price 1 dollar at 1st March 2013. What should be the rate of return of the stock?

If you are using excel, you should use the XIRR function.

But since you are on github, I suppose you are trying to write some software and are looking for the algorithm.

IRR is just the discount rate so as to put the NPV (net present value) of cashflows to 0.

In this case, the time from 1st Jan 2013 to 31Dec2013 is 12mths.
the time from 1st Mar 2013 to 31Dec2013 is 10mths.

So -1000(1+r)^1 -1000(1+r)^(10/12)+2*2000= 0
Solving, r == 1.1255 (which is what Temperament got).

I suggest you read on the time value of money and discounting cashflows to present.

The main problem with implementing this in code is perhaps the curse of dimensionality - if you have a lot of cashflows and is not using goalseek/solver, then you need to have to solve the IRR in the implicit equation using numerical analysis - not that difficult but quite a pain.

Good luck with the coding!
(06-07-2014, 06:38 PM)AlphaQuant Wrote: [ -> ]...not using goalseek/solver, then you need to have to solve the IRR in the implicit equation using numerical analysis

Bravo! No wonder you are AlphaQuant.

五体投地。

Heart LC



Earth day - save the world everyday.
I have an excel spreadsheet doing a few dozen XIRR and its like 20mb......
(06-07-2014, 06:38 PM)AlphaQuant Wrote: [ -> ]The main problem with implementing this in code is perhaps the curse of dimensionality - if you have a lot of cashflows and is not using goalseek/solver, then you need to have to solve the IRR in the implicit equation using numerical analysis - not that difficult but quite a pain.

You are really having only a one dimensional problem (or to put it in another way - with a single degree of freedom) but with multiple inputs. You can do it very simply without resorting to a more difficult algo (like simulated annealing, newton-raphson, hill-climbing etc).

Supposing you have a function FV(x) in which all your cashflows forward valued to today using a common rate of return x, and you simply want to find a x such that FV(x) - Value of Portfolio = 0.

The simplest approach is given an initial guess of x, you perturb it up and perturb it down. You get

FV(x+perturbation) and FV(x-perturbation). Pick the pertubation that minimizes the difference between FV and the value of portfolio. Repeat until your difference changes sign, then you can then bracket the value of r and refine your search (by changing the perturbation value and repeating). Or even simpler, just choose a perturbation value that is to your desired accuracy (e.g. 0.1%) - then it boils down to a straightforward linear search.

And obviously your x, once found, can be saved as the initial guess, the next time you want to recalculate the return.
(06-07-2014, 06:38 PM)AlphaQuant Wrote: [ -> ]
(06-07-2014, 01:24 PM)yccheok Wrote: [ -> ]User purchased 1000 units stock with unit price 1 dollar at 1st January 2013. At 31st December 2013, the stock price reached 2 dollar.

But, what if the user had purchased another 1000 units of same stock with unit price 1 dollar at 1st March 2013. What should be the rate of return of the stock?

If you are using excel, you should use the XIRR function.

But since you are on github, I suppose you are trying to write some software and are looking for the algorithm.

IRR is just the discount rate so as to put the NPV (net present value) of cashflows to 0.

In this case, the time from 1st Jan 2013 to 31Dec2013 is 12mths.
the time from 1st Mar 2013 to 31Dec2013 is 10mths.

So -1000(1+r)^1 -1000(1+r)^(10/12)+2*2000= 0
Solving, r == 1.1255 (which is what Temperament got).

I suggest you read on the time value of money and discounting cashflows to present.

The main problem with implementing this in code is perhaps the curse of dimensionality - if you have a lot of cashflows and is not using goalseek/solver, then you need to have to solve the IRR in the implicit equation using numerical analysis - not that difficult but quite a pain.

Good luck with the coding!

Thanks. I will start to read some regarding XIRR. I have 0 knowledge in quant.

Also, I need to take consideration into selling activities & dividend receiving in between. Does XIRR will able to tackle such? If not, what else area I need to study into?

Thanks
Try reading the few pages before and after this. Cory explained something about XIRR. Should be quite clear.

http://www.valuebuddies.com/thread-2152-...l#pid80276
(07-07-2014, 12:30 AM)yccheok Wrote: [ -> ]Also, I need to take consideration into selling activities & dividend receiving in between. Does XIRR will able to tackle such? If not, what else area I need to study into?

This is just cashflow discounting.

When you inject capital (buying), the cashflow is -ve.
When you receive capital (dividends, principal repayment,selling), cashflow is +ve.

Sum of discounted cashflows to present =0.
Solve for r.

Keywords to google: IRR (internal rate of return), time value of money, NPV (net present value).
Pages: 1 2 3