Moving Average Scanner is providing inaccurate results

Blazer990

New member
Hello,

I was reviewing my trades for the week and noticed that my scanner missed a REALLY good pick that should theoretically have been caught.

I have a scanner that looks for all moving average breaks on the 50, 100, and 200 moving averages.

However, it seemed to have missed $RIVN specifically on July 6th.

My code:

# Moving average break bullish
open[2] is less than SimpleMovingAvg("length" = 50)."SMA" and close[2] is greater than SimpleMovingAvg("length" = 50)."SMA"
or open[2] is less than SimpleMovingAvg("length" = 100)."SMA" and close[2] is greater than SimpleMovingAvg("length" = 100)."SMA"
or open[2] is less than SimpleMovingAvg("length" = 200)."SMA" and close[2] is greater than SimpleMovingAvg("length" = 200)."SMA"

Scanner results with above code ($RIVN is missing):

2TV6HiS.png


Updated code by removing 200-day and 100-day conditions:

# Moving average break bullish
open[2] is less than SimpleMovingAvg("length" = 50)."SMA" and close[2] is greater than SimpleMovingAvg("length" = 50)."SMA"

Scanner results with above code ($RIVN is present):

Uz04y3N.png


I believe the root cause of the issue is that $RIVN has not traded for 200 days and therefore does not have a 200-day moving average. I think this may be causing the search to fail?

But that begs the question, Is there a way for me to properly account for those kind of scenarios and still show the correct results?

I don't want to have to make 6 different scanners to account for the 3 moving averages for bullish and bearish scenarios...
 
Solution
bvqTB20.png


This type of outlier scenario is kind of a pain.

Even if you do something like If BarNumber() >= 200 then Average(close,200) else Double.NaN it will still check to see if 200 bars worth of data are available when the script is first loaded, and it will throw an ( ! ) assertion regardless of the bar number logic.

You've basically got to compute it manually.

Ruby:
def WithinBars = 5;
def Cross;
Script CrossSMA{
    input Length = 200;
    plot CrossSMA;
    if BarNumber() > Length {
        CrossSMA =
            Crosses(
                close,
                ((
                    Fold Index = 0 to Length
                    with Value do
                    Value + getvalue(close,index)
                ) / Length)...
bvqTB20.png


This type of outlier scenario is kind of a pain.

Even if you do something like If BarNumber() >= 200 then Average(close,200) else Double.NaN it will still check to see if 200 bars worth of data are available when the script is first loaded, and it will throw an ( ! ) assertion regardless of the bar number logic.

You've basically got to compute it manually.

Ruby:
def WithinBars = 5;
def Cross;
Script CrossSMA{
    input Length = 200;
    plot CrossSMA;
    if BarNumber() > Length {
        CrossSMA =
            Crosses(
                close,
                ((
                    Fold Index = 0 to Length
                    with Value do
                    Value + getvalue(close,index)
                ) / Length),
                crossingDirection.ANY
            )
        ;
    } else {
        CrossSMA = No;
    }
}
if isNaN(close[-WithinBars * 2]) {
    Cross =
        CrossSMA(200)
        or CrossSMA(100)
        or CrossSMA(50)
        within WithinBars bars
    ;
} else {
    Cross = No;
}
Plot Scan = Cross;

GSVBpH7.png
 
Last edited:
Solution

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
417 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