Moving average trending over consecutive bars


Category:
0
0

I select my stocks rather manually.  I inspect the charts of stocks that I’m watching and one of the main things I look for are stocks that have moving averages with 20 to 60 day length that are consistently increasing over a long period, at least weeks.  There is probably a scan to do this.  I can do something like “is today greater than yesterday”, but that would take a lot of repetitive scripting if you want to go back for a month or more.  So, I’m looking for a TOS scan that will find stocks that have an increasing moving average (simple or other) and have inputs for the moving average length and the number of bars that I want to analyze.

I looked through the Q & A Forum, but everything I found was about charting the moving averages crossing or color changing; I just want the scan to select stocks that are increasing for the period (length) I’ve chosen.  Maybe I didn’t use the proper search criteria?

Marked as spam
Posted by (Questions: 3, Answers: 0)
Asked on February 5, 2025 2:18 pm
1 views
0
Private answer

I updated the title of the question to better describe what I think you are requesting. You did leave out some very important details. So my solution is based on the minimum number of elements to match the details you provided.

This solution checks if the moving average is higher than the previous bar, then counts the number of consecutive bars in which that condition was true. It also does the reverse for when the moving average is trending lower. There are two scan signals included, one for each direction. You can select whichever of the two you want to run in your scan by moving the "#" symbol in front of the "plot scan" statements.

On the very first line I included a user input allowing you to select the number of consecutive trend bars you want to pick up. Keep in mind that there are limits to the amount of historical data available to the scan engine on Thinkorswim. Each time frame as it's own data limit. If you select a value which exceeds that limit you will get zero scan results.

input numberOfBars = 25;
input maLengthOne = 20;
input maTypeOne = AverageType.SIMPLE;
input maPriceOne = close;
def maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
def ascending = maOne > maOne[1];
def descending = maOne < maOne[1];
rec countAscending = if ascending then countAscending[1] + 1 else 0;
rec countDecsending = if descending then countDecsending[1] + 1 else 0;
# use this to scan for maOne ascending over X number of bars
plot scan = countAscending >= numberOfBars;
# use this to scan for maOne descending over X number of bars
# plot scan = countDecsending >= numberOfBars;

Marked as spam
Posted by (Questions: 37, Answers: 4127)
Answered on February 5, 2025 2:29 pm