i'm going to take a guess and say, something to scan all stocks, follow your rules, and use several fold loops, probably will stall out and stop working.
i would suggest make a chart study first. that way you can see if it's working.
then make a lower study that plots 0's or 1's.
then convert that into a scan.
scans work from data read from the last bar, so keep that in mind when reading data.
make an outline, and break out each phase of the code.
then go back and write code for each part
---
if the last bar, then do stuff
determine if some previous x bars are within a narrow horizontal channel. one example, is the b3 code.
...come up with min and max quantity of bars to make up the channel.
determine if x quantity of bars before the horizontal channel were rising.
...come up with min and max quantity of bars to make up the rising bars.
if a horizontal channel exists and the bars before it were rising, then,
determine if some bars are moving above the horizontal channel top level.
...come up with max quantity of bars to make up a range of bars to look for a breakout.
...after awhile you will want to cancel signals and start over
---
B3 Consolidation Box:
https://usethinkscript.com/threads/...kout-breakdown-indicator-for-thinkorswim.103/