# Average Last 5 Candles

##### Member
Hi all, anyone have a script that can do the average of the last 5 candles for price plot on screen. similar idea, but an average length price of the last 5 bars to apply to daily or intraday. Thank you

Hi all, anyone have a script that can do the average of the last 5 candles for price plot on screen. similar idea, but an average length price of the last 5 bars to apply to daily or intraday. Thank you

Each bar will have a bubble displaying the average of the last 5 closes

Code:
``````input length = 5;
AddChartBubble(!IsNaN(close), high, AsText(Sum(close, length) / length), Color.WHITE);``````

thank you Sleepz! having trouble adding it to script, am i missing a formula

thank you Sleepz! having trouble adding it to script, am i missing a formula

Just create a new study and copy/paste the above code to it as shown in the foloowing

this is perfect, works now.

Question, it is plotting the actual price, I may have explained incorrectly. looking for the actual price length of the candle. For example if a candle is a 50 cent move, over the last 5 candles average. not sure if i am explaining right.

Tank you sleepyz

this is perfect, works now.

Question, it is plotting the actual price, I may have explained incorrectly. looking for the actual price length of the candle. For example if a candle is a 50 cent move, over the last 5 candles average. not sure if i am explaining right.

Tank you sleepyz

See if this helps. It calculates the Average Diff (close-open) of the prior 5 bars, compares it to the Current bars Diff (close-open), and if the difference of the Currrent to the Average is > 50 cents, then a chart bubble with the Difference will be plotted. You can turn off the debugging bubbles if you like after testing that the code does what you want.

Code:
``````input length = 5;
input diff   = .50;
def avg_prior_x_bars = Sum(close[1] - open[1], length) / length;
def currentdiff      = close - open - avg_prior_x_bars ;
def cond             = currentdiff > diff;
AddChartBubble(!IsNaN(close) and cond, high, "Curr - Avg " + astext(currentdiff) , Color.WHITE);
input show_debugging = yes;
AddChartBubble(show_debugging and !IsNaN(close) and cond, high, "AvgDiff  " + " " +astext(avg_prior_x_bars) , Color.WHITE);
AddChartBubble(show_debugging and !IsNaN(close) and cond, high, "CurrDiff " + " " +astext(close-open) , Color.WHITE);``````

Thank you. will try this one out. Looks great!

Hi Sleepyz. I was using the .50 cents as just as an example. If there were 5 bars. Gme went from \$200 to \$202 thats a 2 dollar bar move. And the first GME bar was a move of \$2.00 lets say. the Second a move of \$5, the Third a move of \$3, the fourth a move of \$.75 and the fifth a move of \$2..the Firth bar would show not only the current price move but also the average of the last five. So each bar would show the current and last 5 average of the price moves. so the point is to show the average of the length of the price move for the last five bars. the 50 cents was just an example.

Last edited:
Hi Sleepyz. I was using the .50 cents as just as an example. If there were 5 bars. Gme went from \$200 to \$202 thats a 2 dollar bar move. And the first GME bar was a move of \$2.00 lets say. the Second a move of \$5, the Third a move of \$3, the fourth a move of \$.75 and the fifth a move of \$2..the Firth bar would show not only the current price move but also the average of the last five. So each bar would show the current and last 5 average of the price moves. so the point is to show the average of the length of the price move for the last five bars. the 50 cents was just an example.
You can change the input for the .50 to whatever you want. I just used it to match your example. If you want to see all of the bars have bubbles with the Curr Diff and Avg Diff, just set the 'input diff' to 0 (zero)

that worked, thank you

The indicator is fantastic. Can anyone please explain the meaning of the numbers 1 to 4.What number is bullish and which number is bearish. I figured out the calculation. TIA

The indicator is fantastic. Can anyone please explain the meaning of the numbers 1 to 4.What number is bullish and which number is bearish. I figured out the calculation. TIA
i didn't understand the posters number scheme, so i just made up the those numbers 1 to 4, they don't mean anything. i thought they could be used or changed to suit someone's need.

You can change the input for the .50 to whatever you want. I just used it to match your example. If you want to see all of the bars have bubbles with the Curr Diff and Avg Diff, just set the 'input diff' to 0 (zero)
Hi Sleepyz. Would there be a way to get the calculations to perhaps pop up as bars at the top of the screen instead of as bubbles. What i am interested in is the average price move of the last 5 candles depending on if i select, 5 min, 10 min etc. Much appreciated.

Hi Sleepyz. Would there be a way to get the calculations to perhaps pop up as bars at the top of the screen instead of as bubbles. What i am interested in is the average price move of the last 5 candles depending on if i select, 5 min, 10 min etc. Much appreciated.

Try this. After you add it to your chart, it will appear as a lower pane study. Drag it up to the upper pane, in essence making it an upper pane study. This aligns the bubbles at the top of the chart using the line plots.

Code:
``````declare lower;

input length = 5;
input agg    = aggregationPeriod.FIVE_MIN;
def avg_prior_x_bars = Sum(close(period=agg)[1] - open(period=agg)[1], length) / length;

AddChartBubble(!IsNaN(close) , 100, agg/60000+"m \n" + astext(avg_prior_x_bars) , if avg_prior_x_bars>=0 then color.light_green else color.pink);

plot x105    = 105;
plot x100    = 100;
plot x0      = 0;
x105.setdefaultColor(color.black);
x100.setdefaultColor(color.black);
x0.setdefaultColor(color.black);``````

thank you! i added this, but nothing is popping up. am i doing something wrong?

thank you! i added this, but nothing is popping up. am i doing something wrong?

As you wanted the ability to display different aggregation timeframes, the default in that code was 5 minutes. Your chart timeframe must be greater than or equal to 5 minutes for that setting for anything to display.

As I mentioned above, the default is 5 min. Your chart is set to 15 min. Change the input to 15 min or greater at the edit screen for NewStudy7 above.

i tried the 5 minute too, was just trying a few time frames.

Here is your 15 min chart with a edit to the study changing the 5 min default to 30 min. Here is a chart share of this picture. https://tos.mx/b9duJRu

Open up the shared chart link and you will see that the study is in the upper pane.

87k+ Posts
384 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?