This should fix most of the issues with illiquidity. At that low of a volume threshold, which is then greatly exacerbated during extended hours, you're always going to find charts doing all sorts of weird things; skipping periods, skipping entire sessions, no post market trading at all, and so on. I don't think I've caught all of the strange things that can happen, but this should hopefully do for now.
Use the 5 or 15 minute agg. Any lower there aren't necessarily enough bars available to the scanner, anything larger and the bars begin to encapsulate the times being detected.
Code:
input Gain = 10;
def isOpen =
Between(
GetTime(),
RegularTradingStart(GetYYYYMMDD()),
RegularTradingEnd(GetYYYYMMDD())
);
def isLastBar =
GetTime() ==
(RegularTradingEnd(GetYYYYMMDD())
- GetAggregationPeriod() + 1)
or (isOpen and !isOpen[-1])
or (isOpen and GetDay() != GetDay()[-1]);
def NoPost =
isOpen and isLastBar[1];
def AggSec =
GetaggregationPeriod() / 1000;
def Clock =
SecondsFromTime(0000);
def Post =
Between(Clock, 57600, 72000 - AggSec);
def Toggle =
if !Toggle[1] and Post and !Post[1] then yes
else if Toggle[1] and Post and !Post[1] then no
else Toggle[1];
def End =
!Post and Post[-1];
def PCA =
if noPost then double.NaN
else if Toggle and End then Close
else if !Toggle and End then Double.NaN
else PCA[1];
def PCB =
if noPost then double.NaN
else if !Toggle and End then Close
else if Toggle and End then Double.NaN
else PCB[1];
def PCX;
if Toggle then {
PCX = if end then PCB[1]
else if Post then PCX[1]
else double.nan;
} else {
PCX = if end then PCA[1]
else if Post then PCX[1]
else double.nan;
};
def Check =
if noPost then No
else if Post then ((Close - PCX) / PCX) > (Gain * 0.01)
else Check[1];
plot Scan = Check == Yes;