MA Slope Scan For ThinkOrSwim

cos251

Well-known member
Day traders are interested in a moving average's slope because it provides a quick and easy way to identify the direction and strength of a short-term trend, allowing them to make informed decisions about entry and exit points based on the immediate momentum of the price action within a single trading day.

Key points about using a moving average slope for day trading:
  • Trend identification:
    A steep upward slope indicates a strong uptrend, while a steep downward slope suggests a strong downtrend.

  • Momentum signal:
    The slope's change can signal shifts in momentum, allowing traders to react to potential trend reversals.

  • Support and resistance:
    Moving averages can act as dynamic support and resistance levels, with price often bouncing off the line, especially when the slope is changing.

  • Timeframe suitability:
    Day traders typically use shorter timeframes for their moving averages, allowing them to capture rapid price movements within a single trading session.
This is a basic start for the scans. We can build from here.

When you save the scan (with whatever name you give it) you can select it from the studies then change the inputs to your preference (length or average type).
BH5z0ly.png

https://tickertape.tdameritrade.com...average-thinkscript-stock-momentum-tool-15190
Ruby:
## Started on 2021.09.23
## MA Slope SCAN
##
##
## CREDITS
## Orignal source https://tickertape.tdameritrade.com/trading/cool-scripts-create-moving-average-thinkscript-stock-momentum-tool-15190
##
##
## Removing the header Credit credits and description is not permitted, any modification needs to be shared.
##
## V 1.0 :    @cos251 - SCAN version of MA Slope
##       :


input length = 20;
input price = close;
input averageType = AverageType.SIMPLE;

def avg = MovingAverage(averageType,price,length);
def height = avg - avg[1];

plot "Angle, deg" = Atan(height/length) * 180 / Double.Pi;

plot crossPositive = "Angle, deg" > 0 and "Angle, deg"[1] < 0;
plot crossNegative = "Angle, deg" < 0 and "Angle, deg"[1] > 0;

  1. UPDATED WITH AN ADDITIONAL EXAMPLE SCAN
Ruby:
## Started on 2021.09.23
## MA Slope SCAN
##
##
## CREDITS
## Orignal source https://tickertape.tdameritrade.com/trading/cool-scripts-create-moving-average-thinkscript-stock-momentum-tool-15190
##
##
## Removing the header Credit credits and description is not permitted, any modification needs to be shared.
##
## V 1.0 :    @cos251 - SCAN version of MA Slope
##       :


input length = 20;
input price = close;
input averageType = AverageType.SIMPLE;

def avg = MovingAverage(averageType,price,length);
def height = avg - avg[1];

plot "Angle, deg" = Atan(height/length) * 180 / Double.Pi;

plot crossPositive = "Angle, deg" > 0 and "Angle, deg"[1] < 0;
plot crossNegative = "Angle, deg" < 0 and "Angle, deg"[1] > 0;

# --- Example Near Zero but not greater
def approachZero = "Angle, deg" > "Angle, deg"[1] and "Angle, deg"[1] > "Angle, deg"[2] and "Angle, deg" > -.1 and "Angle, deg" < 0;
plot NearZeroUp = if approachZero then 1 else Double.NaN;
 
Last edited by a moderator:

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

I am looking for code example of calculating the slope of exponential moving average ...

Thanks in advance
 
See first post.

The input portion for "length" and "AverageType" can be adjusted to what you need.
 
Last edited by a moderator:
How would you turn this into a scanner? Would you be able to define a certain time period or number of bars to say "the slope flipped positive compared to the X previous days/candles?"
Create the indicator as described in the link I provided. Then go to the scan section in TOS and create a new condition group and add new filter. Select "Study" for your new filter and find the indicator (however you named it).

You can scan for greater/less than 0 or any other set of conditions you would like. For example: if the last 3 have increased or decreased consecutively. Let me know if you need more help.
 
Create the indicator as described in the link I provided. Then go to the scan section in TOS and create a new condition group and add new filter. Select "Study" for your new filter and find the indicator (however you named it).

You can scan for greater/less than 0 or any other set of conditions you would like. For example: if the last 3 have increased or decreased consecutively. Let me know if you need more help.
Hi Cos, thanks for your response on this. I have added the study. When trying to make a Scan, one thing I have trouble with is editing this to make it Exponential Average:

#######
declare lower;
input length = 20;
input price = close;
input averageType = AverageType. SIMPLE;
def avg = MovingAverage(averageType, price, length);
def height = avg - avg[1];
plot “Angle, deg” = Atan(height/length) * 180 / Double.Pi;
“Angle, deg”.AssignValueColor (if “Angle, deg” >=0 then color.ORANGE else color.BLUE);
#######

Whenever I go in there to change averageType and try to make it =MovAvgExp it gives an error.

This is where you would also come to change the angle you are searching for, correct?

When using the Condition Wizard you can't adjust the angle, can you?

Any help would be appreciated, thanks!

Andy
 
Hi Cos, thanks for your response on this. I have added the study. When trying to make a Scan, one thing I have trouble with is editing this to make it Exponential Average:

#######
declare lower;
input length = 20;
input price = close;
input averageType = AverageType. SIMPLE;
def avg = MovingAverage(averageType, price, length);
def height = avg - avg[1];
plot “Angle, deg” = Atan(height/length) * 180 / Double.Pi;
“Angle, deg”.AssignValueColor (if “Angle, deg” >=0 then color.ORANGE else color.BLUE);
#######

Whenever I go in there to change averageType and try to make it =MovAvgExp it gives an error.

This is where you would also come to change the angle you are searching for, correct?

When using the Condition Wizard you can't adjust the angle, can you?

Any help would be appreciated, thanks!

Andy
Change this line:
Ruby:
input averageType = AverageType. SIMPLE;

to:
Ruby:
input averageType = AverageType.EXPONENTIAL;



There is also a space in the code you posted above. The space is between "AverageType." and "SIMPLE;"
 
Change this line:
Ruby:
input averageType = AverageType. SIMPLE;

to:
Ruby:
input averageType = AverageType.EXPONENTIAL;



There is also a space in the code you posted above. The space is between "AverageType." and "SIMPLE;"
Hi cos,

It seems to be working but I am getting results for all the stocks in my watchlist back when I run the scan. What variables can I add to make this scan more specific? For example, I don't need the color coding for the scan but would like to try and say something like "scan for all stocks with MovAvgExp angle >=25 degrees." Is this possible?
 
Hi cos,

It seems to be working but I am getting results for all the stocks in my watchlist back when I run the scan. What variables can I add to make this scan more specific? For example, I don't need the color coding for the scan but would like to try and say something like "scan for all stocks with MovAvgExp angle >=25 degrees." Is this possible?
Yes. But I think it would need to be "Angle, deg" > .25
The plot for Slope is in decimal I believe. The key with the slope is to look for divergence and whether the slope is above or below 0.
 
Yes. But I think it would need to be "Angle, deg" > .25
The plot for Slope is in decimal I believe. The key with the slope is to look for divergence and whether the slope is above or below 0.
Yeah good call on the degree input. But I think the code needs to be changed to get this to work in a scan. Still getting all tickers back as a result.
 
how do i get this to scan within a 5 bar period? instead of scanning for stocks that has already made the move.
 
Create the indicator as described in the link I provided. Then go to the scan section in TOS and create a new condition group and add new filter. Select "Study" for your new filter and find the indicator (however you named it).

You can scan for greater/less than 0 or any other set of conditions you would like. For example: if the last 3 have increased or decreased consecutively. Let me know if you need more help.
I also would like to know how i can use this can to find stocks right where they are about the slope is turning postive, scan a day chart on 5 minute period. or be able to select the number of bars to scan from. please help and thank you.
 
I also would like to know how i can use this can to find stocks right where they are about the slope is turning postive, scan a day chart on 5 minute period. or be able to select the number of bars to scan from. please help and thank you.
This is a basic start for the scans. We can build from here.

When you save the scan (with whatever name you give it) you can select it from the studies then change the inputs to your preference (length or average type).

  1. UPDATED WITH AN ADDITIONAL EXAMPLE SCAN
Ruby:
## Started on 2021.09.23
## MA Slope SCAN
##
##
## CREDITS
## Orignal source https://tickertape.tdameritrade.com/trading/cool-scripts-create-moving-average-thinkscript-stock-momentum-tool-15190
##
##
## Removing the header Credit credits and description is not permitted, any modification needs to be shared.
##
## V 1.0 :    @cos251 - SCAN version of MA Slope
##       : 


input length = 20;
input price = close;
input averageType = AverageType.SIMPLE;

def avg = MovingAverage(averageType,price,length);
def height = avg - avg[1];

plot "Angle, deg" = Atan(height/length) * 180 / Double.Pi;

plot crossPositive = "Angle, deg" > 0 and "Angle, deg"[1] < 0;
plot crossNegative = "Angle, deg" < 0 and "Angle, deg"[1] > 0;

# --- Example Near Zero but not greater
def approachZero = "Angle, deg" > "Angle, deg"[1] and "Angle, deg"[1] > "Angle, deg"[2] and "Angle, deg" > -.1 and "Angle, deg" < 0;
plot NearZeroUp = if approachZero then 1 else Double.NaN;
 
Last edited by a moderator:
This is a basic start for the scans. We can build from here.

When you save the scan (with whatever name you give it) you can select it from the studies then change the inputs to your preference (length or average type).


Ruby:
## Started on 2021.09.23
## MA Slope SCAN
##
##
## CREDITS
## Orignal source https://tickertape.tdameritrade.com/trading/cool-scripts-create-moving-average-thinkscript-stock-momentum-tool-15190
##
##
## Removing the header Credit credits and description is not permitted, any modification needs to be shared.
##
## V 1.0 :    @cos251 - SCAN version of MA Slope
##       : 


input length = 20;
input price = close;
input averageType = AverageType.SIMPLE;

def avg = MovingAverage(averageType,price,length);
def height = avg - avg[1];

plot "Angle, deg" = Atan(height/length) * 180 / Double.Pi;

plot crossPositive = "Angle, deg" > 0 and "Angle, deg"[1] < 0;
plot crossNegative = "Angle, deg" < 0 and "Angle, deg"[1] > 0;
would i able to use this for like intraday 5minute chart. where it will only bring up stocks that are showing a posting slope for like the last 10 5minute bars.
 
would i able to use this for like intraday 5minute chart. where it will only bring up stocks that are showing a posting slope for like the last 10 5minute bars.
Yes. When you build the scan, be sure to increase the bar count "within" so many bars.

Like this:
Ruby:
MA_Slope_SCAN()."crossPositive " is true within 3 bars
 
For the issue on the slope, I'm starting to realized that the below statement where value is equal to [1], if I adjust this value to say 5 or 10, my slope accuracy start to widen out. But on some stocks, it's still just read a very low value.

def height = avg - avg[1];
 
avg - avg[1]
calculates the difference between the moving average of the current candle and the prior candle. Providing a definition of height
 
Yes. When you build the scan, be sure to increase the bar count "within" so many bars.

Like this:
Ruby:
MA_Slope_SCAN()."crossPositive " is true within 3 bars
can you please post the complete code. I have tried it a few times and no success. sorry it took me a while to reply but i was down with covid. fully recovered now.
 
Anything that calculates the angle from the low of a candle to the high the next 2 or 3 candles could definitely help here.
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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