{@type:autotrade}
// SD3_81acd1d8bde3b7f3 | SD3 C | READ-ONLY DAILY DATA DIAGNOSIS.
// No shared table and no order/cancel API. This is not the trading strategy.
// CSV is required: platform preparation can fail before the first Print.
// Each script contains only its own kind of market-data dependencies.
Input: CandidatePool(Group, "被讀取的完整股池；執行商品只選2330.TW"),
       RunId(37, "診斷編號；每輪新建策略並換編號");

Array: intrabarpersist _targets[500]("");
Var: intrabarpersist _started(false), intrabarpersist _done(false),
     intrabarpersist _startDate(0), intrabarpersist _startRun(0),
     intrabarpersist _startClock(0), intrabarpersist _lastSampleClock(0),
     intrabarpersist _startCount(0), intrabarpersist _cycle(0),
     intrabarpersist _allOK(true);
Var: _version("SD3_81acd1d8bde3b7f3"), _profile("C"),
     _clock(0), _targetCount(0), _i(0), _j(0), _validCount(0),
     _dayDate(0), _previousDate(0), _amount(0), _readClock(0),
     _rowOK(false), _cycleOK(false), _changed(false);

// Keep the original history settings; do not change two variables at once.
SetTotalBar(0);
SetBackBar(300, "1");
SetBackBar(2, "D");

if BarFreq <> "Min" or BarInterval <> 1 then
    RaiseRunTimeError("SD3請使用1分鐘、原始值");
if GetInfo("Instance") <> 5 then
    RaiseRunTimeError("SD3只供自動交易即時診斷，不能回測");
if GetInfo("AT_EnableTrade") <> 0 then
    RaiseRunTimeError("SD3禁止任何交易帳號，內建模擬帳號也不可以");
if Position <> 0 or Filled <> 0 then
    RaiseRunTimeError("SD3請使用新策略，部位與成交部位必須為0");
if RunId <= 0 or RunId > 999999 or RunId <> IntPortion(RunId) then
    RaiseRunTimeError("SD3診斷編號須為1至999999整數");
if StrCompare(Symbol, "2330.TW") <> 0 then
    RaiseRunTimeError("SD3本支診斷只能由2330.TW執行，不要放整個股池");
_targetCount = GroupSize(CandidatePool);
if _targetCount < 1 or _targetCount > 500 then
    RaiseRunTimeError("被讀取股池須為1至500檔");

// Do not gate startup on a rank time or require a current-minute trade.
if GetInfo("IsRealTime") = 1 and GetInfo("TradeMode") = 1 then begin
    _clock = CurrentTime;
    if not _started then begin
        _started = true;
        _startDate = CurrentDate;
        _startRun = RunId;
        _startClock = _clock;
        _startCount = _targetCount;
        _allOK = true;
        Print(file("D:\XQ策略\隔日沖V5下單\日線成交額診斷_SD3_20260908\logs\SD3C_[StrategyName]_[Symbol]_[StartTime].log"), "SD3_START|", _startDate, "|", RunId, "|", Symbol,
              "|", _version, "|", _profile, "|", _clock, "|", _startCount,
              "|", Date, "|", Time, "|", Position, "|", Filled,
              "|", GetInfo("AT_EnableTrade"), "|", 2, "|", 300, "|", 0);
        for _i = 1 to _startCount begin
            _targets[_i] = CandidatePool[_i];
            if _i > 1 then begin
                for _j = 1 to _i - 1 begin
                    if StrCompare(_targets[_i], _targets[_j]) = 0 then begin
                        Print(file("D:\XQ策略\隔日沖V5下單\日線成交額診斷_SD3_20260908\logs\SD3C_[StrategyName]_[Symbol]_[StartTime].log"), "SD3_STOP|", _startDate, "|", RunId, "|", Symbol,
                              "|", CurrentTime, "|DUPLICATE_TARGET");
                        RaiseRunTimeError("SD3被讀取股池有重複代碼，請停止檢查");
                    end;
                end;
            end;
            Print(file("D:\XQ策略\隔日沖V5下單\日線成交額診斷_SD3_20260908\logs\SD3C_[StrategyName]_[Symbol]_[StartTime].log"), "SD3_POOL|", _startDate, "|", RunId, "|", Symbol,
                  "|", _i, "|", _targets[_i]);
        end;
    end;

    _changed = RunId <> _startRun or CurrentDate <> _startDate;
    if GroupSize(CandidatePool) <> _startCount then _changed = true;
    if not _changed then begin
        for _i = 1 to _startCount begin
            if StrCompare(CandidatePool[_i], _targets[_i]) <> 0 then _changed = true;
        end;
    end;
    if _changed then begin
        Print(file("D:\XQ策略\隔日沖V5下單\日線成交額診斷_SD3_20260908\logs\SD3C_[StrategyName]_[Symbol]_[StartTime].log"), "SD3_STOP|", _startDate, "|", _startRun, "|", Symbol,
              "|", CurrentTime, "|PARAMETER_OR_DATE_CHANGED");
        RaiseRunTimeError("SD3不可跨日或執行中改參數；請建立新的獨立策略");
    end;

    if not _done then begin
        if TimeDiff(_clock, _startClock, "S") >= 120 then begin
            Print(file("D:\XQ策略\隔日沖V5下單\日線成交額診斷_SD3_20260908\logs\SD3C_[StrategyName]_[Symbol]_[StartTime].log"), "SD3_STOP|", _startDate, "|", RunId, "|", Symbol,
                  "|", _clock, "|CALLBACK_TIMEOUT");
            RaiseRunTimeError("SD3開始即時診斷後超過120秒尚未完成，請保留CSV與LOG");
        end;
        if _cycle = 0 or TimeDiff(_clock, _lastSampleClock, "S") >= 10 then begin
            _cycle = _cycle + 1;
            _validCount = 0;
            for _i = 1 to _startCount begin
                // Only these three data dependencies belong to this variant.
                _dayDate = GetSymbolField(CandidatePool[_i], "Date", "D", default:=0);
                _previousDate = GetSymbolField(CandidatePool[_i], "Date", "D", default:=0)[1];
                _amount = GetSymbolField(CandidatePool[_i], "成交金額(元)", "D", default:=0)[1];
                _readClock = CurrentTime;
                _rowOK = CurrentDate = _startDate and Date = _startDate
                    and Time >= 90000 and Time <= _readClock
                    and Time - IntPortion(Time / 100) * 100 = 0
                    and _dayDate = _startDate
                    and _previousDate > 0 and _previousDate < _startDate
                    and _amount > 0;
                if _rowOK then _validCount = _validCount + 1;
                if not _rowOK then _allOK = false;
                Print(file("D:\XQ策略\隔日沖V5下單\日線成交額診斷_SD3_20260908\logs\SD3C_[StrategyName]_[Symbol]_[StartTime].log"), "SD3_READ|", _startDate, "|", RunId, "|", Symbol,
                      "|", _cycle, "|", _i, "|", _targets[_i], "|", _readClock,
                      "|", Date, "|", Time, "|", _dayDate, "|", _previousDate,
                      "|", NumToStr(_amount, 4), "|", _rowOK);
            end;
            _lastSampleClock = CurrentTime;
            if CurrentDate <> _startDate
               or TimeDiff(_lastSampleClock, _startClock, "S") >= 120 then begin
                Print(file("D:\XQ策略\隔日沖V5下單\日線成交額診斷_SD3_20260908\logs\SD3C_[StrategyName]_[Symbol]_[StartTime].log"), "SD3_STOP|", _startDate, "|", RunId, "|", Symbol,
                      "|", _lastSampleClock, "|SCAN_DATE_OR_TIMEOUT");
                RaiseRunTimeError("SD3讀取期間跨日或逾時，不能視為完成");
            end;
            _cycleOK = _validCount = _startCount;
            Print(file("D:\XQ策略\隔日沖V5下單\日線成交額診斷_SD3_20260908\logs\SD3C_[StrategyName]_[Symbol]_[StartTime].log"), "SD3_CYCLE_END|", _startDate, "|", RunId, "|", Symbol,
                  "|", _cycle, "|", _lastSampleClock, "|", _startCount,
                  "|", _validCount, "|", _cycleOK);
            if _cycle = 3 then begin
                _done = true;
                Print(file("D:\XQ策略\隔日沖V5下單\日線成交額診斷_SD3_20260908\logs\SD3C_[StrategyName]_[Symbol]_[StartTime].log"), "SD3_DONE|", _startDate, "|", RunId, "|", Symbol,
                      "|", CurrentTime, "|", _cycle, "|", _startCount, "|", _allOK);
                // No automatic strategy stop. User exports CSV after stopping this run.
            end;
        end;
    end;
end;
