期貨自動交易

  •   98 
  • 最後發表   qwer0203  2026 四月 20
qwer0203 發文於   2026/04/19

{@type:autotrade}

// 設定建議:1分鐘K、逐筆洗價、讀取500筆

// 商品選擇:FITX* (全盤)

 

Input: _RangeBar(180, "橫盤K棒數");

Input: _StopLoss(25, "停損點數");

Input: _TP_Offset(55, "第一口加點");

 

Var: IntrabarPersist HasBrokenBelow(false); 

Var: IntrabarPersist TP1_Price(0);    

Var: HH(0), LL(0);

 

// 強制系統讀取足夠的資料,解決回測時間消失的問題

SetTotalBar(_RangeBar + 50);

SetBarBack(_RangeBar);

 

// 1. 定義 180 根 K 棒高低點 (不含當前這根)

HH = Highest(High[1], _RangeBar);

LL = Lowest(Low[1], _RangeBar);

 

// 2. 進場邏輯 (無部位時)

if Position = 0 then begin

    // 條件:收盤跌破 LL

    if Close < LL then HasBrokenBelow = true;

    

    // 條件:曾經跌破 + 站回 LL 之上

    if HasBrokenBelow and Close > LL then begin

        SetPosition(2, Market, label:="多單2口進場");

        HasBrokenBelow = false; 

        // 第一口停利價:(高+低)/2 + 55 點

        TP1_Price = (HH + LL) / 2 + _TP_Offset; 

    end;

end;

 

// 3. 持位管理

if Position > 0 then begin

    

    // 第三 & 第四點:停損 25 點 (防守下單位置)

    if Close <= FilledAvgPrice - _StopLoss then begin

        SetPosition(0, Market, label:="停損防守出場");

        // 停損出場後 Position 變 0,程式會自動回到第 2 段重新監控「跌破再收上」

    end;

 

    // 第五點:兩口出場機制

    // 第一口:達標平倉

    if Position = 2 and Close >= TP1_Price then begin

        SetPosition(1, Market, label:="第一口停利");

    end;

    

    // 第二口:無腦放 (除非觸發停損)

end;

 

// 跨日重置 (夜盤 15:00 開始時清空紀錄)

if Date <> Date[1] then HasBrokenBelow = false;

請問一下不知道是否是語法有錯,跑回測時只有夜盤的訊號

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

在交易全日盤期貨時,if Date <> Date[1] then HasBrokenBelow = false; 會在每天午夜零時跨日。若希望150000重置,請改用:

if getFieldDate("Date") <> getFieldDate("Date")[1] then HasBrokenBelow = false;

if Time = 150000 then HasBrokenBelow = false;

 

qwer0203 發文於   2026/04/20

{@type:autotrade}

// 設定:1分鐘K、逐筆洗價、讀取500筆

// 商品:FITX* (全盤)

 

Input: _RangeBar(180, "橫盤K棒數");

Input: _StopLoss(25, "停損點數");

Input: _TP_Offset(55, "第一口加點");

 

Var: IntrabarPersist HasBrokenBelow(false); 

Var: IntrabarPersist TP1_Price(0);    

Var: HH(0), LL(0);

 

// 強制資料回補,確保 08:45 開盤時有值

SetTotalBar(500);

SetBarBack(_RangeBar);

 

// 1. 定義 180 根 K 棒高低點

HH = Highest(High[1], _RangeBar);

LL = Lowest(Low[1], _RangeBar);

 

// 2. 修正跨日重置邏輯 (改以下午 15:00 為基準)

// 使用 getFieldDate("Date") 確保在 15:00 同步換日,不被午夜 00:00 干擾

if getFieldDate("Date") <> getFieldDate("Date")[1] then begin

    HasBrokenBelow = false;

end;

 

// 3. 進場邏輯

if Position = 0 then begin

    // 條件:跌破 180 根低點

    if Close < LL then HasBrokenBelow = true;

    

    // 條件:曾跌破 + 重新收上 LL

    if HasBrokenBelow and Close > LL then begin

        SetPosition(2, Market, label:="多單進場2口");

        HasBrokenBelow = false; 

        // 第一口停利價:(高+低)/2 + 55 點

        TP1_Price = (HH + LL) / 2 + _TP_Offset; 

    end;

end;

 

// 4. 持位管理

if Position > 0 then begin

    

    // 第三 & 第四點:停損 25 點防守

    // 停損出場後 Position 會變回 0,腳本會自動回到上方偵測「跌破再收上」

    if Close <= FilledAvgPrice - _StopLoss then begin

        SetPosition(0, Market, label:="停損防守出場");

    end;

 

    // 第五點:兩口出場

    if Position = 2 and Close >= TP1_Price then begin

        SetPosition(1, Market, label:="第一口停利");

    end;

end;

 

 

回測商品是台股指數近月(FITXN*1)

一樣沒有日盤時間而且交易的時間怪怪的

附加文件

虎科大許教授 發文於   2026/04/20

若你把回測期間拉長到最近半年,可發現日盤有成交紀錄。

qwer0203 發文於   2026/04/20

還是我回測的內容有誤提供回測參數

附加文件

虎科大許教授 發文於   2026/04/20

回測最近三個月或六個月,雖然大部份是夜盤有交易,但還是有一兩筆日盤交易。若限制每天只進場一次,大部份交易也都會發生在夜盤,但還是有日盤的交易。

發表回覆
Close