程式效率執行的問題

  •   118 
  • 最後發表   石頭  2025 九月 03
石頭 發文於   2025/08/26

目前的語法是寫將 時間判斷式 寫在最後面的condition,

//程式開始

if barfreq <> "Min" or Barinterval <>1 then RaiseRuntimeError("請設定頻率為1分鐘");

if GetSymbolInfo("買賣現沖")=false  and  GetSymbolInfo("先買現沖")=false then return;

宣告變數 參數

condition1

condition2

........

condition99=time>=092900 and time<=115900;

if condition1 and condition2 and condition3 and .......and condition99 then setpostion()

if position<>0 and condition then 出場

如果改成 開頭用用判斷式,時間符合才開始判斷,比較不會占用程式效能?因為怕同時間有太多程式在執行,不想時間不符合的程式浪費太多資源,但也不想要時間到了再開啟程式,怕有問題影響執行中的程式,想要開盤前就全部開好。

 

 

// 程式開始

if  time>=092900 and time<=115900 then begin

    if barfreq <> "Min" or Barinterval <>1 then RaiseRuntimeError("請設定頻率為1分鐘");

    if GetSymbolInfo("買賣現沖")=false  and  GetSymbolInfo("先買現沖")=false then return;

    宣告變數 參數

    condition1

    condition2

    ........

    if condition1 and condition2 and condition3 and .......and condition99 then setpostion()

end 

if position<>0 and condition then 出場  // 寫在 if begin的外層,因為停損條件直到收盤

 

 

感恩回覆~

排序方式: 標準 | 最新
虎科大許教授 發文於   2025/08/26

若控制交易時段之外的其他condition,在交易時段之外不需要做運算,則將它們放在控制交易時段的IF裡面,會比較有效率。另外,判斷是否能當沖,只需要在第一次洗價判斷,不能當沖的商品就中斷策略執行。

if barfreq <> "Min" or Barinterval <>1 then RaiseRuntimeError("請設定頻率為1分鐘");
if GetSymbolInfo("買賣現沖")=false and GetSymbolInfo("先買現沖")=false then raiseRunTimeError("無法買賣現沖");
if time>=092900 and time<=115900 then 
    begin   
        condition1=...;
        condition2=...;
        if condition1 and condition2 and condition3 and .......and condition99 then setpostion()
    end;

 

石頭 發文於   2025/08/27

非常感謝許教授的指導。

 

石頭 發文於   2025/08/27

想再請教一下,如果是判斷昨日條件,只需要判斷一次:

if date<>date[1] then....condition88

這條件式是不是可以放在時間邏輯的上一層

If date<>date[1] then....condition88

if time.... then begin

  If condition88 and....then進場

end

 

 

謝謝教授

虎科大許教授 發文於   2025/08/27

分K的情況下,date<>date[1]會在第一根分K計算條件。若是30分K,則前30分鐘都在計算。比較有效率的方式是用第一個Tick進來時計算。例如

if isFirstCall("Date") then condition88=...;

石頭 發文於   2025/08/27

謝謝許教授回答~


 

 

XS小編 發文於   2025/09/03

Hello 石頭,

 

小編補充,雖然節省效能也很重要,不過需注意某些 函數/腳本邏輯 會需要每一根K棒都運算,例如 Highest。

Highest 需要每一根K棒都運算才會得到正確的數值,或是改用 SimpleHighest 確保正確的運算 (但此函數較耗效能)。

石頭 發文於   2025/09/03

感謝提醒~

發表回覆
Close