# How to use TS to get CAGR (compounded annual growth rates)?

#### eugchen

##### Member
Does anyone know how to use thinkscript to calculate compound annual growth rates?

on excel, the formula is something like this

=RRI(A7,B2,B7)

where A7 = how many years
B2 = start value
B7 = end value

so for example, lets say i want to calculate the 5 year CAGR rate for aapl. lets say aapl 5 years ago was 50, and the current price is 100.

if you plugged into this formula, it says that AAPL had an annual compounded growth rate of 14.87% each year for 5 years
thanks
eugene

CAGR formula in Excel: https://www.excel-easy.com/examples/cagr.html

• trendr

#### wtf_dude

##### Active member
I don't have a TOS solution for you, but I've used this for years. CAGR

#### Pensar

##### Well-known member
VIP
@eugchen I coded this up after researching the CAGR formula. I believe this will give the same results as your Excel version.

Code:
``````# CAGR Calculator
# Pensar, 03.05.2021

input yearsback = 5;
def agg = aggregationperiod.YEAR;
def o = open(period = agg)[yearsback];
def CAGR = Power(close/o,1/yearsback)-1;

# end code``````

Last edited:
• wtf_dude, XeoNoX and scott69

#### Pensar

##### Well-known member
VIP
@eugchen Here is a test label that returns the same value as your example you gave. The values can be changed as needed for you to check the results.
Code:
``````def begin = 50;
def end = 100;
def length = 5;
def x = Power(end/begin,1/length)-1;
AddLabel(1,"CAGR Test Result: " + AsPercent(x));``````

• XeoNoX and barbaros

#### eugchen

##### Member
@eugchen Here is a test label that returns the same value as your example you gave. The values can be changed as needed for you to check the results.
Code:
``````def begin = 50;
def end = 100;
def length = 5;
def x = Power(end/begin,1/length)-1;
AddLabel(1,"CAGR Test Result: " + AsPercent(x));``````
WOW, it works well!!!! but how do i get it to pertain to the live chart results?

in other words, i want

Def begin = price 5 years ago
Def End = current price right now

#### Pensar

##### Well-known member
VIP
WOW, it works well!!!! but how do i get it to pertain to the live chart results?

in other words, i want

Def begin = price 5 years ago
Def End = current price right now
@eugchen Look just above to post #3 - it has the proper code. What I posted later was a test code for you to verify the accuracy. Glad you like it!

#### eugchen

##### Member
oh man---im such a moron hahaha. i used BOTH CODES. gotcha.

one thing though...i used this code ONLY...but i dont see any chart label. not sure what im doing wrong. when i do the test label, i do see a chart label

# CAGR Calculator
# Pensar, 03.05.2021

input yearsback = 5;
def agg = aggregationperiod.YEAR;
def o = open(period = agg)[yearsback];
def CAGR = Power(close/o,1/yearsback)-1;

# end code

#### Pensar

##### Well-known member
VIP
oh man---im such a moron hahaha. i used BOTH CODES. gotcha.

one thing though...i used this code ONLY...but i dont see any chart label. not sure what im doing wrong. when i do the test label, i do see a chart label

# CAGR Calculator
# Pensar, 03.05.2021

input yearsback = 5;
def agg = aggregationperiod.YEAR;
def o = open(period = agg)[yearsback];
def CAGR = Power(close/o,1/yearsback)-1;

# end code
@eugchen I have no problem with the label on my end. What symbol is it not showing up on?
Or, is your ThinkorSwim color theme set to white? If so, you likely wouldnt see a white label. You may have to change the label color.

Last edited:

#### eugchen

##### Member
oh man!!!! i think its cause that code is white, and my background is white hahahha. changed it to black and its working!!!! thanks again, highly appreciate your time!

• Pensar

#### Pensar

##### Well-known member
VIP
@eugchen I was just about to ask you to re-read what I had posted . No problem, I like coding interesting things like this!

• eugchen

#### eugchen

##### Member
you are awesome indeed!

#### XeoNoX

##### Well-known member
VIP
wow @Pensar much props, i dont know how you reverse engineered

A2 = A1 * (1 + CAGR)n
end = start * (1 + CAGR)n
end/start = (1 + CAGR)n
(end/start)1/n = (1 + CAGR)
CAGR = (end/start)1/n - 1

i tried following it and got thrown in a loop, give the man a beer! • Pensar

#### Pensar

##### Well-known member
VIP
@XeoNoX This is what I used for the method - https://www.wikihow.com/Calculate-Compounded-Annual-Growth-Rate
To understand what the formula did, I also researched the Power function in the ThinkorSwim Learning Center and on other websites, such as -
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Power
https://en.wikipedia.org/wiki/Exponentiation
https://www.brightstorm.com/math/precalculus/polynomial-and-rational-functions/power-functions/

So to the best of my understanding, this is how the power function works (at least in this case) -
Code:
``````AddLabel(1,Power(10,5)); # 10 raised to the power of 5
AddLabel(1,10*10*10*10*10); # 10 multiplied out 5 times``````
Of course, I'm not a math expert by any means.

• XeoNoX

#### XeoNoX

##### Well-known member
VIP
lol, oh ok thaks for the link...this was much better explination of the forumula... https://www.wikihow.com/Calculate-Compounded-Annual-Growth-Rate

power/exponents im familiar with..... it was trying to figure out the crazy way they described the formula in the original post, often times i see formulas described like in the excel or just variables all over the place that they arent explaining what they represent and its confusing, i prefer how wikihow explained, its much easier to follow. Nice research bro.

• Pensar

#### eugchen

##### Member
Pensar, one last question....how would i alter this wonderful code to show me the CAGR from the beginning of the charts data?

so for instance, lets say XXX chart goes back 5.5 years. i want to see the CAGR for the period of 5.5 years. and then lets say stock YYY goes back 24.5 years. and i want a chart label showing CAGR for the 24.5 years. in other words, sort of an all time CAGR for each stock. is that possible?

i currently have 4 labels...1. 5 year CAGR , 2. 10 year CAGR, 3. 15 year CAGR, 4. 20 year CAGR. looking for an all time CAGR one.

thanks!!!!

#### Pensar

##### Well-known member
VIP
Pensar, one last question....how would i alter this wonderful code to show me the CAGR from the beginning of the charts data?
@eugchen So here is what I came up with. I checked it against AAPL, and used the same test code as above for verification. When I ran it on a max daily chart, everything checked out, but when I ran it on a max yearly chart, the beginning value was around \$0.30 vs the actual \$0.13 starting price of the AAPL chart, so see what it gives you. I'm including the test code with the CAGR chart label since I've coded it to be tied directly to that label's variables for verification. Just delete that portion of the code after you check the results for accuracy.

Code:
``````# CAGR Total Chart Timeframe
# Pensar, 03.06.2021

def agg = aggregationperiod.YEAR;
def c = close(period = agg);
def yearbars = if c then yearbars + 1 else yearbars;
def o = First(open(period = agg));
def CAGR = Power(c/o,1/yearbars)-1;

# end code

input begin = 0.13;
input end = 121.42;
input length = 37;
def x = Power(end/begin,1/length)-1;
AddLabel(1,"CAGR Test Result: " + AsPercent(x));

# end code``````

Last edited:

#### eugchen

##### Member
fantastic! but few questions...

is the code using the opening price of the first candle seen on TOS for a given stock?

for instance, lets look at SQ and TSLA.

the opening price i have down for SQ on TOS is 11.2 on 11/19/15. yesterdays close was 216.44. this was over a 5.3 year period. putting into the CAGR calculator, the answer should be 74.82%.

but on the code, if i have 6 month DAY chart, the value shown is 85.37%. on the MAX DAY chart, i see a value of 52.66%.

the opening price i have down for TSLA on TOS is 3.8 on 6/1/10. yesterdays close was 597.95. this was over a 10.77 year period. putting into the CAGR calculator, the answer should be 59.93%.

but on the code, if i have 6 month DAY chart, the value shown is 165.39%. on the MAX DAY chart, i see a value of 52.43%.

Please let me know if you are getting the same data on the same settings.

Last edited:

#### Pensar

##### Well-known member
VIP
fantastic! but few questions...

is the code using the opening price of the first candle seen on TOS for a given stock?

for instance, lets look at SQ and TSLA.

the opening price i have down for SQ on TOS is 11.2 on 11/19/15. yesterdays close was 216.44. this was over a 5.3 year period. putting into the CAGR calculator, the answer should be 74.82%.

but on the code, if i have 6 month DAY chart, the value shown is 85.37%. on the MAX DAY chart, i see a value of 52.66%.

the opening price i have down for TSLA on TOS is 3.8 on 6/1/10. yesterdays close was 597.95. this was over a 10.77 year period. putting into the CAGR calculator, the answer should be 59.93%.

but on the code, if i have 6 month DAY chart, the value shown is 165.39%. on the MAX DAY chart, i see a value of 52.43%.

Please let me know if you are getting the same data on the same settings.
@eugchen To the best of my knowledge, ThinkScript only works with the data on the chart. So if you have a 6 month 1 day chart timeframe, it will only read 6 months worth of data. I do notice inaccuracies with the value it is pulling for the first bar, as I noted earlier, I'll see if I can iron it out or not.

• eugchen

thanks pensar!