Scan to find stocks with NO gaps for ALL N bars back


Category:
0
0

I am looking to create a scan that finds stocks with NO gaps for ALL N bars back. It’s easy enough for me to finds gaps looking N bars back. There are built-in default scans that do this. It seems much harder to create a scan to find NO gaps for ALL N bars back. Any ideas?

Someone posted code for an study that works great on a chart. In theory it should work in a scan too. But it does not for some reason.

On a chart there is a a number (greater than 0) over the last bar if there were gaps in the last N bars. If there were NO gaps in the last N bars there would be a 0 over the last bar. It works great on a chart.

In a scan I have tried “data is greater than 0” to find gaps in last N bars. This is only for testing the scan. As I mentioned above, there are built-in default scans that finds gaps N bars back. This scan finds nothing.

I tried “data is equal to 0” to find NO gaps in the last N bars. This scan finds nothing.

For some reason, “data” never really seems to be an actual number (0 or greater) when used in a scan, even though the code will plot a 0 or greater number over the last bar. It’s probably something simple that I am missing.

-Charles

Code provided:
input n = 5; # bars
input t = 1.01; # tolerance

def gaps = if isnan(close[-1]) && !isnan(close[0]) then
fold i = 0 to n with g = 0 do
if getvalue(low,i) > getvalue(high,i+1)*t or getvalue(high,i) < getvalue(low,i+1)/t then g+1 else g
else double.nan;

plot data = gaps;
data.setpaintingstrategy(paintingstrategy.values_above);
data.assignvaluecolor(color.cyan);

RESOLVED
Marked as spam
Posted by (Questions: 5, Answers: 4)
Asked on March 11, 2018 7:27 am
109 views
0
Private answer

I agree that your approaches seem like they should work. Data > 0 should mean gaps appear within n bars.

I see that your code uses the fold method of looping. For this application, that is like trying to use a scalpel to chop wood. Much better to use an axe when the result is exactly the same.

Here is how I would solve for the same result:

input n = 5;
def isGap = low > high[1] or high < low[1];
def countGaps = Sum(isGap, n);
plot data = countGaps;
data.setpaintingstrategy(paintingstrategy.values_above);
data.assignvaluecolor(color.cyan);

And this is how I would convert that to a scan:

input n = 5;
def isGap = low > high[1] or high < low[1];
def countGaps = Sum(isGap, n);
plot scan = countGaps == 0;

Screenshot attached shows scan results and chart.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on March 11, 2018 9:56 am
0

PERFECT!!! Thank you!

( at March 11, 2018 12:52 pm)