1st bear or bull candle after crossing MA script


Category:
0
0

Hello,

I’m look for a way to run a scan for the first bear candle closing above a moving average, and a separate scan for the first bull candle closing below the moving average.  We can use 10EMA for example.  This would be used for various time frames.  I can get close with some studies, but I can’t nail down the first bear or bull candle above or below the moving average.  I added a pic for reference.  Appreciate the help

 

Attachments:
Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on March 25, 2022 3:57 pm
188 views
0
Private answer

There are probably a couple of details you left out so I had to make some guesstimates. This solution should get you 99% of the way toward your stated goal.

input maLengthOne = 21;
input maTypeOne = AverageType.EXPONENTIAL;
input maPriceOne = close;
def maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
rec trackCrossAbove = if close < maOne then 0 else if close > maOne then 1 else trackCrossAbove[1];
rec trackCrossBelow = if close > maOne then 0 else if close < maOne then 1 else trackCrossBelow[1]; def bullishCandle = close > open;
def bearishCandle = close < open;
rec trackFirstBearish = if trackCrossAbove and !trackCrossAbove[1] then 0 else if bearishCandle then 1 else trackFirstBearish[1];
rec trackFirstBullish = if trackCrossBelow and !trackCrossBelow[1] then 0 else if bullishCandle then 1 else trackFirstBullish[1];
# use this to scan for first bearish candle after price crosses above ma
plot scan = trackFirstBearish and !trackFirstBearish[1];
# use this to scan for first bullish candle after price crosses below ma
#plot scan = trackFirstBullish and !trackFirstBullish[1];

As with all of my scans which include multiple scan signals, simply read the comment lines above each plot scan statement and move the "#" symbol to select which signal you want to run.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on March 25, 2022 4:43 pm