# Calculate average without using built-in function

#### serendipity2020

##### Member
Plus
Hello

I'm learning thinkscript and want to calculate the sum of all the returns on the selected timeframe using below calculation.

Code:
``def return = (close / close[1] - 1) * 100;``

Now, when I select yearly timeframe, I want to calculate the sum of all the returns shown on the chart.
How do I do that without using sum function? Once sum is calculated, I'll get the total bars using barNumber and find the avg of the same.

I tried below but it doesn't give correct result.

Code:
``def totalReturn = if (isNaN(return)) then 0 else return + return[1];``

Solution
Hello

I'm learning thinkscript and want to calculate the sum of all the returns on the selected timeframe using below calculation.

Code:
``def return = (close / close[1] - 1) * 100;``

Now, when I select yearly timeframe, I want to calculate the sum of all the returns shown on the chart.
How do I do that without using sum function? Once sum is calculated, I'll get the total bars using barNumber and find the avg of the same.

I tried below but it doesn't give correct result.

Code:
``def totalReturn = if (isNaN(return)) then 0 else return + return[1];``

def totalReturn = if barnumber() == 1 then return else totalReturn[1] + return;
Hello

I'm learning thinkscript and want to calculate the sum of all the returns on the selected timeframe using below calculation.

Code:
``def return = (close / close[1] - 1) * 100;``

Now, when I select yearly timeframe, I want to calculate the sum of all the returns shown on the chart.
How do I do that without using sum function? Once sum is calculated, I'll get the total bars using barNumber and find the avg of the same.

I tried below but it doesn't give correct result.

Code:
``def totalReturn = if (isNaN(return)) then 0 else return + return[1];``

def totalReturn = if barnumber() == 1 then return else totalReturn[1] + return;

def totalReturn = if barnumber() == 1 then return else totalReturn[1] + return;
Thanks @halcyonguy
One more question. If I want to calculate say total return for last 4 bars only, in that case, how will this totalReturn look like?

So, basically, I'm planning to keep a input variable avgSince with default value of say 4, then I'd like this total return variable calculate the total return for last 4 bars only conditionally and if avgSince is say -1, then it would perform full calculation which you've provided already. Is that possible?

Thanks @halcyonguy
One more question. If I want to calculate say total return for last 4 bars only, in that case, how will this totalReturn look like?

So, basically, I'm planning to keep a input variable avgSince with default value of say 4, then I'd like this total return variable calculate the total return for last 4 bars only conditionally and if avgSince is say -1, then it would perform full calculation which you've provided already. Is that possible?

if length is -1 then calcs with all the bars on chart.
on setting page , study inputs ,there is a hint for the length. click on white question mark
didn't know what you wanted, so this draws a bubble on all the bars with 4 values.

Code:
``````#total_avg

def bn = BarNumber();
def na = Double.NaN;

input sum_length = 4;
#hint len: Enter a quantity of bars to sum. \n -1 = all the bars on the chart.

def return = (close / close[1] - 1) * 100;

def totalReturn;
def avg;
def len;
if sum_length == -1 then {
totalReturn = if BarNumber() == 1 then return else totalReturn[1] + return;
avg = totalReturn / bn;
len = bn;
} else {
totalreturn = sum(return, sum_length);
avg =  totalReturn / sum_length;
len = sum_length;
}

input test1 = yes;
return + " return\n" +
len + " Len\n" +
totalreturn + " tot\n" +
avg + " Avg"
, color.yellow, no);

addlabel(1, "Length = " + sum_length, color.yellow);
#``````

if length is -1 then calcs with all the bars on chart.
on setting page , study inputs ,there is a hint for the length. click on white question mark
didn't know what you wanted, so this draws a bubble on all the bars with 4 values.

Code:
``````#total_avg

def bn = BarNumber();
def na = Double.NaN;

input sum_length = 4;
#hint len: Enter a quantity of bars to sum. \n -1 = all the bars on the chart.

def return = (close / close[1] - 1) * 100;

def totalReturn;
def avg;
def len;
if sum_length == -1 then {
totalReturn = if BarNumber() == 1 then return else totalReturn[1] + return;
avg = totalReturn / bn;
len = bn;
} else {
totalreturn = sum(return, sum_length);
avg =  totalReturn / sum_length;
len = sum_length;
}

input test1 = yes;
return + " return\n" +
len + " Len\n" +
totalreturn + " tot\n" +
avg + " Avg"
, color.yellow, no);

addlabel(1, "Length = " + sum_length, color.yellow);
#``````
Thanks @halcyonguy This is exactly what I was looking for.

87k+ Posts
304 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?