MA Slope Scan For ThinkOrSwim

cos251

Well-known member
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).

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:
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
 
I'm been lurking for some time, first time posting. I am trying to create a slope indicator of the 34 EMA in which I have color coded (green, yellow, red) the return angle, similar to Raghee Horner double green indicator. I got /NQ and /YM to work perfectly. If they return angle is greater than 15 degree, then green, if it is less than -15 degree then red, and yellow in between. For some reason, all other stocks and /ES doesn't work correctly. The angle return is in the 0.001 range (possibly radian) and not degrees. I've been racking my head around this for a few days and thought it was an issue with ToS. Does anyone else have this issue with the atan function, or am I missing something? I posted by code below. Thanks in advance for the help.

declare lower;

input length = 34;
input price = close;
input averageType = AverageType. Exponential;
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” >=.833333333 then color.ORANGE else color.white);


def Candle = if “Angle, deg” > 15 then 2 else if “Angle, deg” < -15 then -2 else 0;
def plotcandle = if candle > 3 then 100 else 10;
plot DiffPlot = plotcandle;
DiffPlot.AssignValueColor(if Candle < -1 then Color.RED else if Candle > 1 then Color.GREEN else Color.YELLOW);
 
@Rockdog10
Ruby:
def Candle =
    if “Angle, deg” > 15 then 2 else
    if “Angle, deg” < -15 then -2 else 0;

        def plotcandle = if candle > 3 then 100 else 10;

This logic doesn't work. Candle will always equal the data set you created (2 or -2 or 0)
Therefore this statement does not do anything:
def plotcandle = if candle > 3 then 100 else 10;
 
Yes, I put this statement in because I have an extended area that plots white features if there is no value. It's just a visually thing for me on my graphs. Not sure if that's the right way to do it, but it seem to work visually.
 
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
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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