Number of consecutive bars after moving average crossover


Category:
0
0

I’m new to TOS and Forums; so please bear with me.  I’d like to not reinvent the “wheel”.  I’m looking to create (or plagiarize) a scan that looks at a Simple Moving Average (SMA) of selected length is above another SMA of a selected length for a selected number of days.  I suspect this can mostly be created in the Condition Wizard, but I’m not sufficiently educated yet.  I also had a difficult time trying to come up with words to be able to search for this issue.  Also, what is a “tag” below.  I watched the Introductory Video a couple of times.

Marked as spam
Posted by (Questions: 2, Answers: 0)
Asked on January 31, 2025 7:02 pm
8 views
0
Private answer

Well you did great for being new to forums. I did update the title of the question, but just to make it easier to show up in searches for the rest of our viewers.

I borrowed the code for this solution from a custom watchlist column I created for another forum post. You can find that here:

https://www.hahn-tech.com/ans/display-number-of-days-since-cross-of-two-moving-averages/

Be sure to check that out, because it will make a great companion to this scan.

Here is the same code from post, after modifying it to work as a scan:

input priceOne = close;
input lengthOne = 2;
input typeOne = AverageType.SIMPLE;
input priceTwo = close;
input lengthTwo = 8;
input typeTwo = AverageType.SIMPLE;
def maOne = MovingAverage(typeOne, priceOne, lengthOne);
def maTwo = MovingAverage(typeTwo, priceTwo, lengthTwo);
def crossAbove = maOne[1] < maTwo[1] and maOne > maTwo;
def crossBelow = maOne[1] > maTwo[1] and maOne < maTwo;
rec countAbove = if crossAbove then 1 else if maOne > maTwo then countAbove[1] + 1 else 0;
rec countBelow = if crossBelow then 1 else if maOne < maTwo then countBelow[1] + 1 else 0;
# adjust input value to however many consecutive bars you want the scan to pick up
input numberOfBars = 10;
# use this signal to scan for X number of bars after cross above
plot scan = countAbove >= numberOfBars;
# use this signal to scan for X number of bars after cross below
#plot scan = countBelow >= numberOfBars;

Note that I included a user input just before the scan signals. You did not mention how many consecutive bars you wanted to use for your scan. But you can adjust that value to suit your preferences.

There are two plot signals included. One for crossing above and another for crossing below. If you have viewed any of my tutorial videos on custom scans you will know how to switch between those two signals.

Marked as spam
Posted by (Questions: 37, Answers: 4125)
Answered on January 31, 2025 7:10 pm