想請教各位大神,這套策略可以使用,但是為什麼回測結果都是0?

  •   283 
  • 最後發表   菜鳥韭菜想進步  2025 五月 05
菜鳥韭菜想進步 發文於   2025/05/04

input: 

    JumpPoint(20, "跳空點數"), 

    BodyPoint(10, "實體點數"), 

    ProfitPoint(50, "啟動移動停利點數"), 

    PullbackPoint(10, "回檔點數");

 

Vars: 

    float myEntryPrice(0), 

    float maxProfit(0), 

    bool isLong(false), 

    bool isShort(false), 

    bool canTrade(false),

    int barCountToday(0),

    int todayDate(0);

 

// 僅允許 5 分 K

if BarFreq <> "Min" or BarInterval <> 5 then 

    RaiseRunTimeError("請使用5分K");

 

// 每日重新計算 barCountToday

if Date <> todayDate then begin

    todayDate = Date;

    barCountToday = 1;

end else

    barCountToday += 1;

 

// 交易時間判斷(0845~1345、1500~0500)

canTrade = (Time >= 084500 and Time <= 134500) or (Time >= 150000 or Time <= 050000);

 

// 第 1 根 K 棒:檢查跳空 + 實體

if barCountToday = 1 and canTrade then begin

    if AbsValue(Open - Close) > BodyPoint and AbsValue(Open - Close[1]) > JumpPoint then begin

        if Open > Close[1] then begin

            isLong = true;

            myEntryPrice = Open;

        end else if Open < Close[1] then begin

            isShort = true;

            myEntryPrice = Open;

        end;

    end;

end;

 

// 第 2 根 K 棒:開盤進場

if position = 0 and canTrade and barCountToday = 2 then begin

    if isLong then

        SetPosition(1, Market)

    else if isShort then

        SetPosition(-1, Market);

end;

 

// 移動停利與基準價反向出場邏輯

if position <> 0 then begin

    if position = 1 then

        maxProfit = MaxList(maxProfit, Close - myEntryPrice);

    if position = -1 then

        maxProfit = MaxList(maxProfit, myEntryPrice - Close);

 

    // 啟動移動停利

    if maxProfit >= ProfitPoint then begin

        if position = 1 and Close <= myEntryPrice + maxProfit - PullbackPoint then

            SetPosition(0, Market);

        if position = -1 and Close >= myEntryPrice - maxProfit + PullbackPoint then

            SetPosition(0, Market);

    end;

 

    // 基準價反向突破出場

    if position = 1 and Close < myEntryPrice then

        SetPosition(0, Market);

    if position = -1 and Close > myEntryPrice then

        SetPosition(0, Market);

end;

 

// 出場後重置

if position = 0 then begin

    isLong = false;

    isShort = false;

    myEntryPrice = 0;

    maxProfit = 0;

end;

 

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

問題出在以下的程式碼區塊。第一根K棒雖然isLong為True,或isShort為True,但因為還沒進場,所以程式跑到最下面時,它們又被恢復為False,所以第二根K棒不可能進場。

// 出場後重置

if position = 0 then begin

    isLong = false;

    isShort = false;

    myEntryPrice = 0;

    maxProfit = 0;

end;

菜鳥韭菜想進步 發文於   2025/05/05

感謝許教授回覆,已獲的解決!感恩您

發表回覆
Close