The logic we need for this scan signal is all contained within the last line of the code:
AssignPriceColor( if ADX > ADX[1] and price > SMA then globalColor("Up") else if ADX > ADX[1] and price < SMA then globalColor("Down") else globalColor("Neutral"));
So we have the green candle based on: ADX > ADX[1] and price > SMA
While the red candle is based on: ADX > ADX[1] and price < SMA
In order to convert any chart study to a scan there are really only two steps:
- Convert all plot statements to def
- Remove all the style statements applied to the former plot statements
Having completed those two steps we are left with the following:
input price = close;
input KeltnerLength = 13;
input VolumeFastLength = 1;
input VolumeSlowLength = 20;
input ADXLength = 10;
input SMALength = 8;
def KeltnerSMA = Average(hlc3, KeltnerLength);
def AvgRange = Average(high - low, KeltnerLength);
def VolumeOsc = (Average(volume, VolumeFastLength) - Average(volume, VolumeSlowLength)) / Average(volume, VolumeSlowLength);
def ADX = reference ADX(length = ADXLength);
def SMA = Average(price, SMALength);
def KeltnerHigh = KeltnerSMA + AvgRange;
def KeltnerMid = KeltnerSMA;
def KeltnerLow = KeltnerSMA - AvgRange;
def VolumeSpike = VolumeOsc > 0.5;
Every scan requires one plot statement. So we take that code I just posted and add the following plot statement to scan for the green candles produced by this chart study:
plot scan = ADX > ADX[1] and price > SMA;
and for the red candles we would instead add the following:
plot scan = ADX > ADX[1] and price < SMA;
Remember, only one plot statement can be included in a scan at any one time. So you cannot have both of those plot statements in the same Study Filter of your scan.
That's it. Nothing more to it than that.