編譯成功但是回測沒有數據

  •   216 
  • 最後發表   chiaoshih  2024 七月 07
chiaoshih 發文於   2024/07/07

1.定義前一天日線最高點的數字為AA以及最低點的數字為B
2.一天開盤時間為15:00;收盤時間為13:45
3.當天價格大於AA時馬上市價做多,停損設定在B;當天價格小於B時馬上市價做空,停損設定在AA。
4.進場後目標價位計算:AA-B
5.做多的目標價:AA加上第四點計算結果
6.做空的目標價:B減掉第四點計算結果
7.如果在收盤前未達目標價,平倉
8.停損或是目標價一到,平倉
以上是我的構想
程式碼如下 想請問要如何修改呢

// 設定時間變數

Value1 = EncodeTime(15, 0, 0); // 15:00

Value2 = EncodeTime(13, 44, 58) + 1; // 隔天13:44.58

 

// 變數宣告

variable:

    AA(0), // 前一天最高點

    B(0), // 前一天最低點

    targetPrice(0), // 目標價位

    stopLossPrice(0), // 停損價位

    entryPriceValue(0); // 進場價格

Vars: intrabarpersist currentClose(0);

 

currentClose = Close;

// 計算前一天的最高點和最低點

if Date <> Date[1] then

begin

    AA = High[1]; // 前一天最高點

    B = Low[1]; // 前一天最低點

end;

 

// 設定多單進場條件

if Time >= Value1 and Time < Value2 then

begin

    if position = 0 and Close > AA then

    begin

        entryPriceValue = Close; // 記錄進場價格

        targetPrice = AA + (AA - B); // 計算多單目標價位

        stopLossPrice = B; // 設定多單停損價位

        setposition(1, market); // 市價進場做多

    end

    else if position = 0 and Close < B then

    begin

        entryPriceValue = Close; // 記錄進場價格

        targetPrice = B - (AA - B); // 計算空單目標價位

        stopLossPrice = AA; // 設定空單停損價位

        setposition(-1, market); // 市價進場做空

    end;

end;

 

// 設定目標價位和停損

if Position = 1 then

begin

    if Close >= targetPrice then

    begin

        setposition(0, market); // 達到目標價時平倉

        // 結束當天的操作

    end

    else if Close <= stopLossPrice then

    begin

        setposition(0, market); // 達到停損價時平倉

        // 結束當天的操作

    end

    else if Time >= Value2 then

    begin

        setposition(0, market); // 收盤時間平倉多單

    end;

end // 在這裡添加了結束 Position = 1 的區塊

 

// 設定空單平倉條件

else if Position = -1 then

begin

    if Close <= targetPrice then

    begin

        setposition(0, market); // 達到目標價時平倉

    end

    else if Close >= stopLossPrice then

    begin

        setposition(0, market); // 達到停損價時平倉

    end

    else if Time >= Value2 then

    begin

        setposition(0, market); // 收盤時間平倉空單

    end;

 

end;

 

虎科大許教授 發文於   2024/07/07

你這邊有幾個問題:

(1)從AA = High[1]; 得知,你使用的是日頻率。在日頻率之下,Date<>Date[1]永遠成立,這個條件有設等於沒設。

(2)在日頻率之下,Time永遠為零。所以你的進場條件if Time >= Value1 and Time < Value2 then永遠不會成立,所以沒有交易訊號,回測當然沒有數據。

(3)控制期貨全日盤交易時段,可考慮使用:

if currentTime>=084500 and currentTime<134500

or currentTime>=150000 and currentTime<240000

or currentTime>=000000 and currentTime<050000 then

      begin

            。。。 

      end;

發表回覆
Close