Trailing 5 Day Closing Average

mbrandau

mbrandau

New member
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
 
J

JabronLames

Member
VIP
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:
mbrandau

mbrandau

New member
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
 
J

JabronLames

Member
VIP
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);
 
mbrandau

mbrandau

New member
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.
 
J

JabronLames

Member
VIP
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);
 
mbrandau

mbrandau

New member
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.
 
J

JabronLames

Member
VIP
@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);
 
mbrandau

mbrandau

New member
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.
 
J

JabronLames

Member
VIP
@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);
 
mbrandau

mbrandau

New member
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.
 
J

JabronLames

Member
VIP
@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.
 
mbrandau

mbrandau

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

mbrandau

New member
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
 
J

JabronLames

Member
VIP
Odd.

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

mbrandau

New member
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.
 
J

JabronLames

Member
VIP
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.
 
mbrandau

mbrandau

New member
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?
 
J

JabronLames

Member
VIP
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);
 

Similar threads

Top