Trend Strength Script

Hello,

I have been trying unsuccessfully to write a script to do the following in an EOD scan -

-“the close has not been less than the 50sma for more than 10 times” - during the Scanning Manager “Date Range” settings, which I could make from 22/12/2019 to 22/12/2024 as an example.

I have made my own attempts, used 2 examples from Ai Chat and tried the following example from - Scripterbot as follows -

// Define the 50-day Simple Moving Average
SMA50 = MA(BARS=50, STYLE=Simple); // Calculate the 50-day SMA

// Check if the Close is less than the 50-day SMA
CloseBelowSMA50 = CLOSE < SMA50; // Condition for Close below the 50-day SMA

// Count the number of times the Close has been below the 50-day SMA
CountBelowSMA50 = BarsTrue(CloseBelowSMA50); // Count occurrences

// Check if the count is less than or equal to 10
finalCondition = CountBelowSMA50 <= 10; // Final condition

// Output the final condition for scanning
finalCondition; // Output the result

This example runs ok but appears not to be correct as well so I am back to square 1.

My criteria may be a bit strict, but once I have a working base script, I can alter the numbers.

I would really appreciate some help in getting this to work correctly please.

Thanks for your help,

Regards,

Brad

Hi Brad,

The BARSTRUE() function only looks at the last X bars (10 by default) so can’t be used when looking at a date range.

Instead you can use the Accumulation ACC() function to count how many times a true condition has occurred from a specified date:

// Define the 50-day Simple Moving Average
SMA50 = MA(BARS=50, STYLE=Simple); 

// Check if the Close is less than the 50-day SMA
CloseBelowSMA50 = CLOSE() < SMA50; 

// Count the number of times the Close has been below the 50-day SMA
CountBelowSMA50 = ACC(CloseBelowSMA50, RANGE=Look Back Period, BACKTYPE=Date, STARTDATE=2019-12-12); 

// Check if the count is less than or equal to 10
finalCondition = CountBelowSMA50 <= 10; 

// Output the final condition for scanning
finalCondition; 

10 is a too small a number to get any results, but if you add the CountBelowSMA50 output to a watchlist column you will see the total number of results. Of the S&P500 stocks listed before 2019, CTAS has the fewest closes below its 50MA with 339, but is currently trading well below. DECK has the fewest that is trading above, with 364:

Hello Darren,

Thank you for your scripting help.

I ran your script in an EOD scan, it didn’t achieve what I was looking for which is my fault for not describing what I was looking for better.

  • below is a better English version wording - I intend to scan with it in “Scanning Manager” between chosen date range over a selected universe of stocks ie ASX200 -

“for more than 3 times the close has crossed above the 50sma and stayed above the 50sma for more than 90 days”
or
“for more than 3 times the close has crossed above and remained above the 50sma for more than 90 days”

Once a base script is found to be working correctly, the numbers and criteria can easily be changed.

In Scripterbot I have tried many variations of the above English versions, the resulting scripts either don’t run or don’t produce the correct results.

One of the most recent examples given by Scripterbot is as per below -

// Define the 50-day Simple Moving Average
////SMA50 = MA(BARS=50, STYLE=Simple); // Calculate the 50-day SMA

// Check if the Close crosses above the 50-day SMA
////CrossAboveSMA50 = CLOSE CrossesAbove SMA50; // Condition for Close crossing above the 50-day SMA

// Initialize a counter for the number of times the Close has stayed above the 50-day SMA for more than 90 days
////CountAbove90Days = 0; // Counter for occurrences

// Loop through the last 500 bars to count the number of times the Close has stayed above for more than 90 days
////for i = 0 to 499 do
// Check if the Close crossed above the 50-day SMA at this bar
//// if CrossAboveSMA50[i] then
// Check if the Close has stayed above the 50-day SMA for more than 90 days
//// if BarsTrue(CLOSE > SMA50, BARS=90, OFFSET=i) == 90 then
////CountAbove90Days = CountAbove90Days + 1; // Increment the counter // CountAbove90Days is problem-is used 2 times
//// end if
///// end if
////end for

// Final condition to check if the Close has crossed above and remained above the 50SMA for more than 90 days for more than 3 times
////finalCondition = CountAbove90Days > 3; // Final condition

// Output the final condition for scanning
////finalCondition; // Output the result

This script wouldn’t run, and I have been unable to make it run.

Your help will be much appreciated.

Thanks again,

Brad