Posts: 45
Threads: 10
Joined: Jun 2013
Reputation:
0
06-07-2014, 01:24 PM
(This post was last modified: 06-07-2014, 01:24 PM by yccheok.)
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?
Posts: 3,474
Threads: 95
Joined: Jul 2011
Reputation:
17
06-07-2014, 03:29 PM
(This post was last modified: 06-07-2014, 04:24 PM by Temperament.)
(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!
WB:-
1) Rule # 1, do not lose money.
2) Rule # 2, refer to # 1.
3) Not until you can manage your emotions, you can manage your money.
Truism of Investments.
A) Buying a security is buying RISK not Return
B) You can control RISK (to a certain level, hopefully only.) But definitely not the outcome of the Return.
NB:-
My signature is meant for psychoing myself. No offence to anyone. i am trying not to lose money unnecessary anymore.
Posts: 3,474
Threads: 95
Joined: Jul 2011
Reputation:
17
(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?
WB:-
1) Rule # 1, do not lose money.
2) Rule # 2, refer to # 1.
3) Not until you can manage your emotions, you can manage your money.
Truism of Investments.
A) Buying a security is buying RISK not Return
B) You can control RISK (to a certain level, hopefully only.) But definitely not the outcome of the Return.
NB:-
My signature is meant for psychoing myself. No offence to anyone. i am trying not to lose money unnecessary anymore.
Posts: 480
Threads: 42
Joined: Aug 2012
Reputation:
39
06-07-2014, 06:38 PM
(This post was last modified: 06-07-2014, 07:59 PM by AlphaQuant.)
(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!
Posts: 1,364
Threads: 106
Joined: Oct 2013
Reputation:
17
(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.
五体投地。
LC
Earth day - save the world everyday.
Posts: 275
Threads: 24
Joined: Nov 2010
Reputation:
3
I have an excel spreadsheet doing a few dozen XIRR and its like 20mb......
Posts: 456
Threads: 9
Joined: Apr 2011
Reputation:
21
06-07-2014, 10:50 PM
(This post was last modified: 06-07-2014, 11:41 PM by tanjm.)
(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.
Posts: 45
Threads: 10
Joined: Jun 2013
Reputation:
0
(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
Posts: 71
Threads: 2
Joined: Aug 2013
Reputation:
1
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
Posts: 480
Threads: 42
Joined: Aug 2012
Reputation:
39
07-07-2014, 08:49 AM
(This post was last modified: 07-07-2014, 08:55 AM by AlphaQuant.)
(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).
|