已經先成交了,但是有時候currentTime比成交時間還小

  •   135 
  • 最後發表   xqray001  2026 十月 15
xqray001 發文於   2026/10/13

不好意思,又來請教
我試著寫一個策略,單純就是執行後買進一口,過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

 

);

排序方式: 標準 | 最新
虎科大許教授 發文於   2026/10/14

我在猜,你的問題應該是出在交易夜盤期貨,且進場時間是在午夜之前,而出場時間是在午夜之後。這會造成用TimeDiff計算的數值變成負值。若欲修改這個問題,必須自訂函數。若有綁定我的優惠碼@8596,可用我提供的DateTimeDiff取代TimeDiff解決這個問題。

xqray001 發文於   2026/10/14

謝謝許教授回覆,但我是在晚上8點左右測試的
我把print出來的結果貼上來給您參考一下

有的時候,CurrentTime會比FilledEntryTime晚,這時候程式就可以執行

CurrentTime= 203739.000000  FilledEntryTime= 203738.000000  Count= 1.000000  Position= 1.000000  Filled= 1.000000 

CurrentTime= 203740.000000  FilledEntryTime= 203738.000000  Count= 2.000000  Position= 1.000000  Filled= 1.000000 

CurrentTime= 203740.000000  FilledEntryTime= 203738.000000  Count= 2.000000  Position= 1.000000  Filled= 1.000000 

CurrentTime= 203740.000000  FilledEntryTime= 203738.000000  Count= 2.000000  Position= 1.000000  Filled= 1.000000 

CurrentTime= 203740.000000  FilledEntryTime= 203738.000000  Count= 2.000000  Position= 1.000000  Filled= 1.000000 

CurrentTime= 203740.000000  FilledEntryTime= 203738.000000  Count= 2.000000  Position= 1.000000  Filled= 1.000000 

CurrentTime= 203740.000000  FilledEntryTime= 203738.000000  Count= 2.000000  Position= 1.000000  Filled= 1.000000 
.

.

.

 

但大部分時候CurrentTime數字居然是比FilledEntryTime還要小
就會觸發處理跨午夜的情況->count就會加86400
結果就觸發賣出,造成一買入就馬上賣出

CurrentTime= 203816.000000  FilledEntryTime= 203819.000000  Count= 86397.000000  Position= 1.000000  Filled= 1.000000 

CurrentTime= 203817.000000  FilledEntryTime= 0.000000  Count= 0.000000  Position= 0.000000  Filled= 0.000000 

CurrentTime= 203817.000000  FilledEntryTime= 0.000000  Count= 0.000000  Position= 0.000000  Filled= 0.000000 

CurrentTime= 203818.000000  FilledEntryTime= 0.000000  Count= 0.000000  Position= 0.000000  Filled= 0.000000 

CurrentTime= 203818.000000  FilledEntryTime= 0.000000  Count= 0.000000  Position= 0.000000  Filled= 0.000000 

CurrentTime= 203818.000000  FilledEntryTime= 0.000000  Count= 0.000000  Position= 0.000000  Filled= 0.000000 

CurrentTime= 203820.000000  FilledEntryTime= 0.000000  Count= 0.000000  Position= 0.000000  Filled= 0.000000 

.

.

.

不懂為什麼會有這種不一致的問題
有時候CurrentTime數字大於FilledEntryTime,有時候CurrentTime數字小於FilledEntryTime

還是說這是XQ的系統問題

虎科大許教授 發文於   2026/10/14

CurrentTime是windows系統的時間,可能會有比中原標準時間慢的問題。建議你改用GetField("收盤價","Tick")。

xqray001 發文於   2026/10/15

原來如此,謝謝教授

發表回覆
Close