Moving Average Auto-Adjusted By Timeframe For ThinkOrSwim

magnetar513

New member
Someone has probably already done this, but here is my attempt: It seems to work pretty well, but if anyone finds ways to improve it, please let me know.

# Multi Day Moving Average that remains accurate for different chart
# aggregation periods. i.e. Study length is automatically adjusted
# according to aggregation period of the chart.
# I tried adding multiple MAs to a single study, but plotting was
# unreliable, particularly on aggregation periods less than two hours.
# Selecting several single studies with different day lengths on the same chart seems to sidestep these problems.
# Of course, it won't work with Ticks.
# by: Magnetar513

declare upper;

input price1= close;
input avgType1= AverageType.WILDERS;
input numDays= 10;
input priceA= FundamentalType.CLOSE;
def aggPeriod= GetAggregationPeriod();

def length= if aggPeriod== aggregationPeriod.MIN then (1440* numDays)
else if aggPeriod== aggregationPeriod.TWO_MIN then (720* numDays)
else if aggPeriod== aggregationPeriod.THREE_MIN then (480* numDays)
else if aggPeriod== aggregationPeriod.FOUR_MIN then (360* numDays)
else if aggPeriod== aggregationPeriod.FIVE_MIN then (288* numDays)
else if aggPeriod== aggregationPeriod.TEN_MIN then (144* numDays)
else if aggPeriod== aggregationPeriod.FIFTEEN_MIN then (96* numDays)
else if aggPeriod== aggregationPeriod.TWENTY_MIN then (72* numDays)
else if aggPeriod== aggregationPeriod.THIRTY_MIN then (48* numDays)
else if aggPeriod== aggregationPeriod.HOUR then (24* numDays)
else if aggPeriod== aggregationPeriod.TWO_HOURS then (12* numDays)
else if aggPeriod== aggregationPeriod.FOUR_HOURS then (3* numDays)
else if aggPeriod== aggregationPeriod.DAY then (numDays)
else if aggPeriod== aggregationPeriod.TWO_DAYS then (numDays/2)
else if aggPeriod== aggregationPeriod.THREE_DAYS then (numDays/3)
else if aggPeriod== aggregationPeriod.FOUR_DAYS then (numDays/4)
else if aggPeriod== aggregationPeriod.WEEK then (numDays/5)
else if aggPeriod== aggregationPeriod.MONTH then (numDays/20)
else if aggPeriod== aggregationPeriod.QUARTER then (numDays/60)
else if aggPeriod== aggregationPeriod.YEAR then (numDays/180)
else double.nan;

plot mvgAvg= MovingAverage(avgType1, price1, length);
mvgAvg.setDefaultColor(color.green);
 
Last edited:

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

What does this do? Doesn't ToS already auto adjust the time frame when you switch between them? I'm not sure what you're script is designed to do.
 
What does this do? Doesn't ToS already auto adjust the time frame when you switch between them? I'm not sure what you're script is designed to do.
It allows the moving day averages to remain consistent while using different aggregation periods. Moving average lengths are assigned, by default, as periods, or candles, not days, so a "10 period moving average" doesn't necessarily mean "10 day moving average".
 
Someone has probably already done this, but here is my attempt: It seems to work pretty well, but if anyone finds ways to improve it, please let me know.

# Multi Day Moving Average that remains accurate for different chart
# aggregation periods. i.e. Study length is automatically adjusted
# according to aggregation period of the chart.
# I tried adding multiple MAs to a single study, but plotting was
# unreliable, particularly on aggregation periods less than two hours.
# Selecting several single studies with different day lengths on the same chart seems to sidestep these problems.
# Of course, it won't work with Ticks.
# by: Magnetar513

declare upper;

input price1= close;
input avgType1= AverageType.WILDERS;
input numDays= 10;
input priceA= FundamentalType.CLOSE;
def aggPeriod= GetAggregationPeriod();

def length= if aggPeriod== aggregationPeriod.MIN then (1440* numDays)
else if aggPeriod== aggregationPeriod.TWO_MIN then (720* numDays)
else if aggPeriod== aggregationPeriod.THREE_MIN then (480* numDays)
else if aggPeriod== aggregationPeriod.FOUR_MIN then (360* numDays)
else if aggPeriod== aggregationPeriod.FIVE_MIN then (288* numDays)
else if aggPeriod== aggregationPeriod.TEN_MIN then (144* numDays)
else if aggPeriod== aggregationPeriod.FIFTEEN_MIN then (96* numDays)
else if aggPeriod== aggregationPeriod.TWENTY_MIN then (72* numDays)
else if aggPeriod== aggregationPeriod.THIRTY_MIN then (48* numDays)
else if aggPeriod== aggregationPeriod.HOUR then (24* numDays)
else if aggPeriod== aggregationPeriod.TWO_HOURS then (12* numDays)
else if aggPeriod== aggregationPeriod.FOUR_HOURS then (3* numDays)
else if aggPeriod== aggregationPeriod.DAY then (numDays)
else if aggPeriod== aggregationPeriod.TWO_DAYS then (numDays/2)
else if aggPeriod== aggregationPeriod.THREE_DAYS then (numDays/3)
else if aggPeriod== aggregationPeriod.FOUR_DAYS then (numDays/4)
else if aggPeriod== aggregationPeriod.WEEK then (numDays/5)
else if aggPeriod== aggregationPeriod.MONTH then (numDays/20)
else if aggPeriod== aggregationPeriod.QUARTER then (numDays/60)
else if aggPeriod== aggregationPeriod.YEAR then (numDays/180)
else double.nan;

plot mvgAvg= MovingAverage(avgType1, price1, length);
mvgAvg.setDefaultColor(color.green);
Hi, and great script. I'm attempting to use this, however it won't plot. I've tried to view it on a few different chart timeframes and it won't show on any. Any ideas?
 
Hi, and great script. I'm attempting to use this, however it won't plot. I've tried to view it on a few different chart timeframes and it won't show on any. Any ideas?

look in the top left corner of chart. is there a white circle with a ! in it? click on it.
there is a max limit of 2000 historical bars.

try a bigger chart time, like hour
(288* numDays) , 288 x 10 = 2880 bars
 
Hi, and great script. I'm attempting to use this, however it won't plot. I've tried to view it on a few different chart timeframes and it won't show on any. Any ideas?
Thanks for calling it a "great script", but it might not be if it's not working for you. I copied the code onto my system and it seems to work, but for smaller timeframes the plot may be outside the price range shown on the screen. You could try switching to a day aggregation, or select "fit studies" under the "price axis" tab under chart settings. Thanks for the feedback.
 
Hi, and great script. I'm attempting to use this, however it won't plot. I've tried to view it on a few different chart timeframes and it won't show on any. Any ideas?
Try this script, it's leaner and should be more reliable:


# Multi Day Moving Average accurate for different chart
# aggregation periods.
# Of course, it won't work with Ticks.

declare upper;

input avgType1 = AverageType.EXPONENTIAL;
input numDays = 50;
def aggPeriod = GetAggregationPeriod();

def price;
def length;

if aggPeriod<= AggregationPeriod.DAY{
price= close(period= aggregationPeriod.DAY);
length= numdays;

}
else if aggPeriod == AggregationPeriod.TWO_DAYS {
price= close;
length = (numDays / 2);
}
else if aggPeriod== aggregationPeriod.THREE_DAYS {
price= close;
length= (numDays/3);
}
else if aggPeriod== aggregationPeriod.FOUR_DAYS {
price= close;
length= (numDays/4);
}
else if aggPeriod== aggregationPeriod.WEEK {
price= close;
length= (numDays/5);
}
else if aggPeriod== aggregationPeriod.MONTH {
price= close;
length= (numDays/20);
}
else if aggPeriod== aggregationPeriod.QUARTER {
price= close;
length= (numDays/60);
}
else if aggPeriod== aggregationPeriod.YEAR {
price= close;
length= (numDays/180);
}
else {
price= close;
length= 50;
}

def mvgAvg =MovingAverage(avgType1, price, length) ;
plot MA = mvgAvg;
MA.SetDefaultColor(Color.CYAN);
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
238 Online
Create Post

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