# Trailing 5 Day Closing Average

#### mbrandau

##### New member
VIP
Hello all,

I am a newbie of sorts, specifically to ThinkorSwim. I have my charts set up almost perfectly, the way I prefer. There is 1 thing I am looking for which is a numerical number somewhere on each chart that states the past 5 days closing average. Just a number, no trend lines, no multiple numbers. Just 1 number that tells me the average last 5 days closing average, including most recent. Is there something I can use or a simple script someone can make for me? This would help enormously as currently I have several every morning that I do math on in Excel and it takes up a lot of time.

Thank you in advance.

Mark

A fold should do the trick!

Set this to the day chart. It will add a label of the average close for the last 5 days.

Code:
``````# Dan Dude's Averager'r
# Get average last 5 periods

input length = 5; # last 5 periods

def price = close;
# The fold
plot sum = fold n = 0 to length
with p
do p + GetValue(price, n, length - 1) / length;
# end the fold
AddLabel(yes, "The Average for the Last " + length + " Days is " + sum);``````

Hope this helps!

Last edited:
Will this work on my preferred chart of 20 days, 1 hour?

Hmm...the average number seems to change with the different chart times. 180 days, 20 days, 5 days, etc all give different averages which should be impossible

No, you had to set the chart manually.

*I edited the script below to set the agg period for you. It will now pull the close of the last 5 days on any chart.

Code:
``````# Dan Dude's Averager'r
# Get average last 5 periods

input length = 5; # last 5 periods

input Agg = AggregationPeriod.DAY;

def price = close(period = agg);
# The fold
plot sum = fold n = 0 to length
with p
do p + GetValue(price, n, length - 1) / length;
# end the fold

AddLabel(yes, "The Average for the Last " + length + " Days is " + sum);``````

Yeah man, thank you very much. Works on all charts. Average doesn't change with chart time periods. I tested it manually and perfect. Thank you this saves me probably 60-90 minutes in the morning.

No Problem. I'm happy to help and it helps me learn as well.

Thanks for the like.

I may be able to help further. The code below is modified for a watchlist. It SHOULD highlight(green/red) the cells if the average is + - 5% of the current price.

Since it's set to the current market price and the market isn't open, I can't test it yet. I'm happy to follow up on Monday if you feel that this will help.

Code:
``````# Dan Dude's Averager'r for the watchlist
# Get average last 5 periods

input length = 5; # last 5 periods

input percentChange = 5; #<---- CHANGE THIS IF YOU WANT TO EDIT THE PERCENT

input AggCurrent = AggregationPeriod.MIN;

input Agg = AggregationPeriod.DAY;

def currentPrice = close(period = AggCurrent);

def Avgprice = close(period = agg);
# The fold
plot sum = fold n = 0 to length
with p
do p + GetValue(Avgprice, n, length - 1) / length;
# end the fold

AddLabel(yes, sum);

AssignBackgroundColor(if AvgPrice >= currentPrice * (1 + (percentChange/100)) then color.dark_green else if AvgPrice < currentPrice * (1 - (percentChange/100)) then color.dark_red else color.current);``````

So looking this morning, as soon as there is activity we get a return of N/A on the script you have. Not the most recent which you posted concerning +/- 5% but the one before it. +/- 5% is not relevant to me. The only thing, for additional convenience, that I would ask for would be Green highlight for above 5 day close average and Red for below 5 day close average. But the most troubling part right now is the N/A with activity. So that tells me there is something wrong with the definition of Close in the script.

@mbrandau

Hmmm, this might fix it. * I can check in later

The issue with the N/A was using a shorter time frame than DAY on the watchlist. That issue is fixed, but I'm not sure if this will take the present price until market open

Code:
``````# Dan Dude's Averager'r for the watchlist
# Get average last 5 periods

input length = 5; # last 5 periods

#input AggCurrent = AggregationPeriod.MIN; #<= Can't do this

input Agg = AggregationPeriod.DAY;

input percentChange = 5; #

def currentPrice = ask;

def Avgprice = close(period = agg);
# The fold
plot sum = fold n = 0 to length
with p
do p + GetValue(Avgprice, n, length - 1) / length;
# end the fold

AddLabel(yes, sum);

AssignBackgroundColor(if AvgPrice >= currentPrice * (1 + (percentChange/100)) then color.dark_red else if AvgPrice < currentPrice * (1 - (percentChange/100)) then color.dark_green else color.BLACK);``````

So here's my observations from today. In pre-market this morning, those with any volume at all produced an N/A for calculation. Those with no volume in pre-market gave the average 5 day close price.

As the day went on, the average calculation was a moving number. I observed this while watching SAVE because it was a volatile stock today. So as the day went on, the average changed. I am looking for just a static number, taking EOD close amounts, L5 closes and place it on the chart somewhere.

In addition, for additional convenience I would want the highlight to be Green if last close price was above the 5 day average and red if below.

Not sure why the average was a moving object as the day went on. I saw your script and it clearly said close. But then again, I'm not a programmer.

Thank you in advance.

@mbrandau,

That's strange. I had this column on my watchlist and had ZERO n/a errors. Did you set the column to DAY when you added the script?

Code:
``````# Dan Dude's Averager'r for the watchlist
# Get average last 5 periods

input length = 5; # last 5 periods

#input AggCurrent = AggregationPeriod.MIN; #<= Can't do this

input Agg = AggregationPeriod.DAY;

input percentChange = 5; #

def currentPrice = ask;

def Avgprice = close(period = agg);
# The fold
plot sum = fold n = 0 to length
with p
do p + GetValue(Avgprice, n, length - 1) / length;
# end the fold

AddLabel(yes, currentPrice);

AssignBackgroundColor(if AvgPrice >= currentPrice * (1 + (percentChange/100)) then color.dark_red else if AvgPrice < currentPrice * (1 - (percentChange/100)) then color.dark_green else color.BLACK);``````

When you say " Did you set the column to DAY when you added the script? " - what do you mean? The chart period? Or some designation within the Study itself? I didn't see anything where I could change the study. If that is what you meant, could you show me or direct me somehow? If it is the Chart period, that may be an issue as my preferred Chart setup is 20 Days, 1 Hour.

@mbrandua,

No problem.

1. Click the great to edit the columns in your watchlist.

2. Double click the 'icon' to the left of the custom column with the code

3. The upper-left hand side of the window will have the 'Column Name' => Followed by a box that will become a drop down menu when you click on it. Click it.

4. You can select the period for the column in the watchlist. Set this particular one to Day.

No, this is not a watchlist. This is a script on the chart study

I just tried the last script you placed here and this does not work on the studies chart at all. So I went back to your 1st revision, 2 post. Doesn't solve any of the moving averages though or colors

Odd.

This is a screenshot of the column in a watchlist on my TOS....

Ok like I said, this is not a watch list. This is placed on the chart itself. You and I were talking the same convo last night but now not so much. How do you add a screen shot to the post here? I don't have links.

All good. If you search the thread you'll find that I've been referencing the script as a watchlist script all along, and that the very first line states that's what it was for.

But hey, that makes things easier moving forward!

*In order to upload an image here you have to reference a link somewhere.

Yeah man, I got ya. But that wasn't what I wanted and to be honest you had it working with that understanding last night. But now the moving averages, etc. Anyway. Maybe someone else can help?

This should fix the issue. I'll make sure I have the study up and not the watchlist tomorrow.

Code:
``````# Dan Dude's Averager'r
# Get average last 5 periods

input length = 5; # last 5 periods

input Agg = AggregationPeriod.DAY;

def price = close(period = agg);
# The fold
plot sum = fold n = 0 to length
with p
do p + GetValue(price[1], n, length - 1) / length;
# end the fold

AddLabel(yes, "The Average for the Last " + length + " Days is " + sum);``````

Thread starter Similar threads Forum Replies Date
Trailing Stop Conditional order Questions 2
trailing stop strategy? Questions 3
Trailing ATR- show both levels at same time Questions 1
Trailing Stop Loss Percent Plot Questions 2
ATR trailing stop Questions 2

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
318 Online

## 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.