不好意思,又來請教
我試著寫一個策略,單純就是執行後買進一口,過2分鐘後自動平倉

我先用if GetInfo("IsRealTime") = 1 and filled > 0 確定已經成交
接著計算 Count = TimeDiff (currentTime, FilledEntryTime, "s");
等到Count >= 120 就觸發平倉 


結果跑策略,讓系統print資料後發現問題
if 條件式這邊,已經判斷成交了(filled > 0)
但是跑Count = TimeDiff (currentTime, FilledEntryTime, "s"); 時
currentTime數字比FilledEntryTime數字小
造成Count 是負值->觸發if Count < 0 then begin ->又觸發 if Count >= 120 then begin ->平倉

結果就是系統買完馬上平倉
請問我有什麼地方理解錯誤?

還請老手不吝指教,謝謝


--------以下是我的code-------

Variables:

    IntrabarPersist RangeHigh(0),   //IntrabarPersist 是為了記錄逐筆洗價每一筆的更新資料

    IntrabarPersist RangeLow(0),

    IntrabarPersist SavedDate(0),

IntrabarPersist HasOrdered(False),

    Count(0);

 

// 每天重新初始化

if Date <> SavedDate then Begin

SavedDate = Date;

RangeHigh = 0;

RangeLow = 0;

end;

if GetInfo("IsRealTime") = 1 then begin

if HasOrdered = false and Position =0 and filled =0 then begin  

setPosition(1 , MARKET, label:="買進1口");

HasOrdered = True;

end;

end;

 

// 買進成交後,開始計算持倉時間 

if GetInfo("IsRealTime") = 1 and filled > 0 then begin

 

Count = TimeDiff (currentTime, FilledEntryTime, "s");

// 處理跨午夜的情況

if Count < 0 then begin

Count = Count + 86400;

end;

 

if Count >= 120 then begin   //120秒抵達,準備平倉 

setPosition(0 , MARKET, label:="市價平倉");

end;

end

else begin

    Count = 0;

end;

 

Print(

    File("C:\Users\Ray\Desktop\XQ_Debug.log"),

    "CurrentTime=", CurrentTime,

    " FilledEntryTime=", FilledEntryTime,

    " Count=", Count,

    " Position=", Position,

    " Filled=", Filled

 

);