Repository: microsoft/qlib
File: qlib/contrib/data/highfreq_handler.py (Python)

Code before:
        template_fillnan = "FFillNan({0})"
        fields += [
            template_fillnan.format(template_paused.format("$close")),
        ]
        names += ["$close0"]

        fields += [
            template_paused.format(
                template_if.format(
                    template_fillnan.format("$close"),
                    "$vwap",
                )
            )
        ]
        names += ["$vwap0"]

        fields += [template_paused.format("If(IsNull({0}), 0, {0})".format("$volume"))]
        names += ["$volume0"]

        fields += [template_paused.format("If(IsNull({0}), 0, {0})".format("$factor"))]
        names += ["$factor0"]

        return fields, names


class HighFreqGeneralBacktestHandler(HighFreqBacktestHandler):
    def __init__(
        self,
        instruments="csi300",
        start_time=None,
        end_time=None,
        day_length=240,
    ):
        self.day_length = day_length
        super().__init__(
            instruments=instruments,
            start_time=start_time,
            end_time=end_time,
        )

    def get_feature_config(self):
        fields = []
        names = []

        template_paused = f"Cut({{0}}, {self.day_length * 2}, None)"
        # template_paused = "{0}"
        template_fillnan = "FFillNan({0})"
        template_if = "If(IsNull({1}), {0}, {1})"
        fields += [
            template_paused.format(template_fillnan.format("$close")),
        ]

Review comment (suggestion):
Should this class inherit from  DataHandlerLP directly?

Both `class HighFreqHandler` and `class HighFreqGeneralHandler` are special cases of the high frequent handlers.
We don't have a base class for high frequent handlers. So it should inherit from DataHandlerLP directly

Code after:
        template_if = "If(IsNull({1}), {0}, {1})"
        template_paused = "Select(Gt($paused_num, 1.001), {0})"
        template_fillnan = "FFillNan({0})"
        fields += [
            template_fillnan.format(template_paused.format("$close")),
        ]
        names += ["$close0"]

        fields += [
            template_paused.format(
                template_if.format(
                    template_fillnan.format("$close"),
                    "$vwap",
                )
            )
        ]
        names += ["$vwap0"]

        fields += [template_paused.format("If(IsNull({0}), 0, {0})".format("$volume"))]
        names += ["$volume0"]

        fields += [template_paused.format("If(IsNull({0}), 0, {0})".format("$factor"))]
        names += ["$factor0"]

        return fields, names


class HighFreqGeneralBacktestHandler(DataHandler):
    def __init__(
        self,
        instruments="csi300",
        start_time=None,
        end_time=None,
        day_length=240,
    ):
        self.day_length = day_length
        data_loader = {
            "class": "QlibDataLoader",
            "kwargs": {
                "config": self.get_feature_config(),
                "swap_level": False,
                "freq": "1min",
            },
        }
        super().__init__(
            instruments=instruments,
            start_time=start_time,
            end_time=end_time,
            data_loader=data_loader,
        )