CAGR formula translation
Forums › ProRealTime English forum › ProBuilder support › CAGR formula translation
- This topic has 10 replies, 4 voices, and was last updated 5 years ago by undefined.
-
-
09/21/2016 at 11:30 AM #1347009/21/2016 at 7:30 PM #13509
I’m sorry, I spent almost 2 hours doing maths formulas to solve the MathPow being not available in math instructions of the platform.. Didn’t solve the formula because of the fractionnal exponent. Seems that I could calculate something if the ‘nryears’ is below 3 but not above.
I have just sent a request to PRT dev team to add the MathPow possibility in the language, as it is not the first time i’m stuck with this! 🙁
If any math genius come here, I’ll be glad to have his opinion 🙂
09/21/2016 at 11:41 PM #13512Ok, I guess I’ve become a PRC-addict, even when I’m away from my trading station I feel the need to come and read what happened since last visit, haha
Assuming nryears is an integer, how about a recursive approach, for which the number of iterations would depend on the required precision in the end by the user?
Let’s say to simplify the formula writing that A=equity/startcapital and n=nryears
and let’s name B = (equity/startcapital)^(1/nryears) = A^(1/n)
so B^n = A = B * B^(n-1)
B = A / B ^(n-1)
and if I use i to rank the iterations from 1 to maximum i desired for sufficient precision and {} for index:
B{rank i+1} = ( B{rank i} + B{rank i} ) / 2
B{rank i+1} = ( B{rank i} + A / (B{rank i} ^(n-1) ) / 2
and a code with 2 loops to use the formula:
something like:
1234567891011121314151617B=1for i=1 to 10 //(try 20 and 30 and... to see how much is enough for required accuracy in the end)previousB=Bden=Bfor j=2 to nryears-1den=den*BnextB=(previousB + (equity/startcapital)/den)/2nextcagr=B-1However, I can’t launch prt and test it from here, so it’s probably bugged as I often need to make changes to first writing before getting it right… Also, I have never tried a loop inside a loop in PRT before, don’t know if it can handle it.
1 user thanked author for this post.
09/22/2016 at 6:53 AM #13516@Nicolas, I could not solve it either. But, I did come up with the following “indicator” – that can be added to the end of any strategy backtesting code.
1234567891011121314151617181920212223// yearly and monthly returnsonce startcapital=100000 // initial capital amountonce yearstartcap=startcapital // set initial capital for 1st yearonce mstartcap=startcapital // set initial capital for 1st monthif day<day[1] and month=1 then // new yearendcapital=startcapital+strategyprofit // ending capital for the year (previous year)prof=endcapital-yearstartcap // profit for the year (previous year)yearstartcap=endcapital // set starting capital for the new yearyearreturn=(prof/startcapital)*100 // yearly return as percentage (previous year)endifif day<day[1] and month>month[1] then // new monthmendcapital=startcapital+strategyprofit // ending capital for the month (previous month)mprof=mendcapital-mstartcap // profit for the month (previous month)mstartcap=mendcapital // set starting capital for the new monthmreturn=(mprof/startcapital)*100 // monthly return as percentage (previous month)endifgraph 0 coloured(0,0,255) // graph zero linegraph yearreturn coloured(0,200,0) as "Yearly Return %" // graph yearly return (prev year)graph mreturn coloured(154,0,154) as "Monthly Return %" // graph monthly return (prev month)It calculates yearly and monthly returns, and looks like this (see attachment).
I would appreciate any comments or suggestions for improvements.
Regards
Stef
1 user thanked author for this post.
09/22/2016 at 7:06 AM #1352209/22/2016 at 7:57 AM #13525Your code is good. It successfully returned the good value for the Wikipedia example attached.
Here is the code with the same value:
12345678910111213141516171819202122B=1equity = 13000startcapital = 9000nryears = 3for i=1 to 10 //(try 20 and 30 and... to see how much is enough for required accuracy in the end)previousB=Bden=Bfor j=2 to nryears-1den=den*BnextB=(previousB + (equity/startcapital)/den)/2nextcagr=B-1RETURN cagrWell done! Fractional exponent gimme headache yesterday, you desserve a beer again! 🙂 Maybe I’ll add a post in the Blog in your own name.
09/22/2016 at 6:26 PM #1355509/23/2016 at 5:37 AM #1357610/01/2016 at 3:06 PM #14037No problem for blog post if you think it can be useful to compensate for the lack of mathpow, with or without my name doesn’t matter, I don’t own the rights to recursivity
I am going to write a “carte de fidélité – bières” with squares to stamp the beers on, in case some day I happen to drive near your headquarters 😉
10/01/2016 at 4:05 PM #1403805/10/2019 at 8:31 PM #98204 -
AuthorPosts