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];`