可以編譯回測沒有數據

  •   276 
  • 最後發表   布萊恩來了  2024 十一月 05
布萊恩來了 發文於   2024/10/29

各位大神 小編好

我有一個策略能成功編譯,回測時卻沒有任何數據 (我用逐筆洗價)

麻煩幫我看看哪裡可能出了問題謝謝

 

// 宣告參數

input: BigOrderRatio(0.1, "大單量佔昨日成交比例"),

       LongStopLoss(5, "多方停損百分比"),

       LongTakeProfit(5, "多方停利百分比"),

       ShortStopLoss(1, "空方停損百分比"),

       ShortTakeProfit(1, "空方停利百分比"),

       MaxPrice(100, "最高股價限制");

 

var:intrabarpersist EntryPrice1(0),

    intrabarpersist ShortEntryPrice(0),

    intrabarpersist YesterdayVolume(0),

    intrabarpersist LastVolume(0),

    intrabarpersist LastPosition(0),

    intrabarpersist LastTime(0),

    intrabarpersist DayFirstTick(1),    // 用於標記當天第一筆

    intrabarpersist FirstTickProcessed(0); // 用於確認第一筆處理完成

 

// 設定回測區間

settotalbar(3);

setbarback(3);

 

// 重置日期變更判斷

if Date <> Date[1] then

begin

    DayFirstTick = 1;

    FirstTickProcessed = 0;

end;

 

// 取得昨日成交量 (用時間來判斷新的一根K棒開始)

if Time <> LastTime then

begin

    YesterdayVolume = Volume[1];

    LastTime = Time;

end;

 

// 處理第一筆搓合單

if DayFirstTick = 1 then

begin

    LastVolume = Volume;

    DayFirstTick = 0;

    FirstTickProcessed = 1;

    return;

end;

 

// 定義交易條件

condition1 = Volume - LastVolume > YesterdayVolume * BigOrderRatio; // 單筆成交量超過昨日總量的1/10

condition2 = Close <= MaxPrice; // 股價需小於100元

condition3 = CurrentTime >= 90000 and CurrentTime <= 120000; // 交易時間在9:00-12:00之間

condition4 = FirstTickProcessed = 1; // 確保第一筆已經處理完成

 

// 更新LastVolume

LastVolume = Volume;

 

// 多方進場邏輯

if position = 0 and condition1 and condition2 and condition3 and condition4 then

begin

    EntryPrice1 = Close;

    LastPosition = 1;

    setposition(1, market, label:="多方進場-大單");

end;

 

// 多方出場邏輯

if position = 1 then

begin

    // 停損 (5%)

    if Close <= EntryPrice1 * (1 - LongStopLoss / 100) then

    begin

        LastPosition = 0;

        setposition(0, market, label:="多方停損");

    end;

    

    // 停利 (5%)

    if Close >= EntryPrice1 * (1 + LongTakeProfit / 100) then

    begin

        LastPosition = 0;

        setposition(0, market, label:="多方停利");

    end;

end;

 

// 空方進場邏輯 (融券)

if position = 0 and condition1 and condition2 and condition3 and condition4 then

begin

    ShortEntryPrice = Close;

    LastPosition = -1;

    setposition(-1, market, label:="空方進場-大單");

end;

 

// 空方出場邏輯

if position = -1 then

begin

    // 停損 (1%)

    if Close >= ShortEntryPrice * (1 + ShortStopLoss / 100) then

    begin

        LastPosition = 0;

        setposition(0, market, label:="空方停損");

    end;

    

    // 停利 (1%)

    if Close <= ShortEntryPrice * (1 - ShortTakeProfit / 100) then

    begin

        LastPosition = 0;

        setposition(0, market, label:="空方停利");

    end;

end;

 

// 收盤時間強制平倉

if position <> 0 and CurrentTime > 120000 then

begin

    LastPosition = 0;

    setposition(0, market, label:="收盤平倉");

end;

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

if Date <> Date[1] then

begin

    DayFirstTick = 1;

    FirstTickProcessed = 0;

end;

if DayFirstTick = 1 then

begin

    LastVolume = Volume;

    DayFirstTick = 0;

    FirstTickProcessed = 1;

    return;

end;

DayFirstTick = 1就return,後面的程式碼就不會被執行,所以沒有回測結果。

布萊恩來了 發文於   2024/10/29

謝謝教授 已解決

XS小編 發文於   2024/11/05

Hello 布萊恩來了,

 

小編補充,就您的腳本來看,應該是盤中的逐筆洗價在使用的。

需注意回測時的逐筆洗價運作方式會和即時的不太相同,回測時的運作方式為:

1分鐘逐筆洗價 => 1分鐘K棒洗四次 (OHLC)

其他頻率逐筆洗價 => 1分鐘K棒洗一次

故在編寫腳本的時候需要將此考慮進去 (ex. Volume - LastVolume 就不會是單筆成交量)。

 

感謝 虎科大許教授 的熱心回覆。

發表回覆
Close