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];
 

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

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;
vtEv3qV.png
 
@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;
vtEv3qV.png
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
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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