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;
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

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

#https://usethinkscript.com/threads/calculate-average-without-using-built-in-function.18026/#post-138431

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;
addchartbubble(test1, low,
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

#https://usethinkscript.com/threads/calculate-average-without-using-built-in-function.18026/#post-138431

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;
addchartbubble(test1, low,
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.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
296 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top