# Scan for Moving Average Slope Change

#### WillyG

##### New member
To set the stage, this is my first post on usethinkscript.com. I have not written a study with thinkscript but have made some simple modifications to several studies that I use. I use Martin Begley's moving average study that changes color as the slope of the moving average changes (see his code below). I am trying to write the script to create a scan to identify stocks where their 50 day moving average has changed from sloping downward (yesterday) to sloping upward (today). Following are my attempts to create this scan. First, I used the Thinkorswim Condition Wizard and came up with the following code which did not work:

SimpleMovingAvg("length" = 50) is greater than SimpleMovingAvg("length" = 50) from -1 bars ago

So, I tried to hand-write the code below and, unfortunately, it did not work either. I would very much appreciate someone's help! Thank you in advance!!!

input length = 50;
input price = close;
input AverageType = SimpleMovingAvg;
def MA = SimpleMovingAvg;
plot MA > MA[1];

##############################################
# Calculate and Plot Moving Average With Colors
# by Martin Begley
###############################################
input length = 50;
input price = close;
input AverageType = {Default Simple, Exponential, Weighted, Wilders, Hull};
def average;
switch (AverageType) {
case Simple:
average = Average(price, length);
case Exponential:
average = ExpAverage(price, length);
case Weighted:
average = wma(price, length);
case Wilders:
average = WildersAverage(price, length);
case Hull:
average = HullMovingAvg(price, length);
}
plot MA = average;
MA.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
MA.SetLineWeight(2);
MA.DefineColor("Up", Color.DARK_GREEN);
MA.DefineColor("Down", Color.RED);
MA.DefineColor("Flat", Color.GRAY);
MA.AssignValueColor(if MA[0] > MA[1] then MA.Color("Up") else if MA[0] <
MA[1] then MA.Color("Down") else MA.Color("Flat"));

I made a small modification to my effort above by changing the "input" code to "def" code, but the code still does not give me the information I'm looking for.

def average = SimpleMovingAvg(close, 50);
def MA = average;
plot scan = MA[0] > MA[1];

Ruby:
``````# try this.
# need to also look at the change from 2 days ago to 1 days ago

plot scan = (MA[2] > MA[1] and MA[1] < MA[0]);``````

Ruby:
``````# try this.
# need to also look at the change from 2 days ago to 1 days ago

plot scan = (MA[2] > MA[1] and MA[1] < MA[0]);``````
That's it!!! Thank you so much!!!

That's it!!! Thank you so much!!!
So would this work for a scanner:

input length = 50;
input price = close;
def AverageType = movavgExponential;
def MA = movavgExponential;
plot scan = (MA[2] > MA[1] and MA[1] < MA[0]);

It doesn't seem to be working for me. Any help appreciated, thanks!

@Andygray8 There are many ways to write this. Here is one example:
Ruby:
``````def MA = ExpAverage(close, 50);;
plot scan = MA[2] > MA[1] and MA[1] < MA;``````

@Andygray8 There are many ways to write this. Here is one example:
Ruby:
``````def MA = ExpAverage(close, 50);;
plot scan = MA[2] > MA[1] and MA[1] < MA;``````
Thanks MD I appreciate the quick response! Looks like it's providing good results. While trying to keep it simple, I was pondering ways to make this a little more sophisticated. Like somehow putting in a condition like "is it greater than X bars ago." Not sure how this would work. One workaround would be to just create two different scans where MA2 and MA1 are isolated and flip flopped and just reconcile them against each other with multiple scans to figure out when the slope flips to positive.

Forgive my ramblings thanks for your help!

Andy

def MA = ExpAverage(close, 50);; plot scan = MA[2] > MA[1] and MA[1] < MA;
Hey MerryDay,
So I'll just use the built in filters and add this as a custom study?

Thank you.

Hey MerryDay,
So I'll just use the built in filters and add this as a custom study?

Thank you.

87k+ Posts
261 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?