階段式停損停利

  •   394 
  • 最後發表   布萊恩來了  2024 十一月 06
布萊恩來了 發文於   2024/10/31

各位大神好,

我有個階段式停損停利的想法,我把進場張數改成2張,其餘進場條件不變

階段式停利為以下

但不知哪裡出了問題,回測時完全沒有數據,可以幫我看看原因嗎? 謝謝

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

begin

    EntryPrice1 = Close;

    EntryBar = CurrentBar;

    setposition(2, market, label:="進場");  // 進場2張

end;

 

// 出場邏輯

if position > 0 then

begin

    // 停損

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

    begin

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

        return;

    end;

    

    // 階段式停利

    if Close >= EntryPrice1 * (1 + TakeProfit1 / 100) then  // 第一階段停利5%

    begin

        setposition(1, market, label:="第一階段停利");  // 出場1張

        return;

    end;

 

    if Close >= EntryPrice1 * (1 + TakeProfit2 / 100) then  // 第二階段停利7.5%

    begin

        setposition(0, market, label:="第二階段停利");  // 出場1張

        return;

    end;

 

    // 持有時間到

    if CurrentBar >= EntryBar + HoldDays then

    begin

        setposition(0, market, label:="持有時間到");

        return;

    end;

end;

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

把所以的return拿掉,改用其他變數控制不再進入到if裡面。

布萊恩來了 發文於   2024/11/01

謝謝教授指導 我後來新增了一個變數IsInPsition去控制是否平倉去代替return,可是還是沒有回測數據,能再幫我看一下嗎 謝謝

// 進場邏輯

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

begin

    EntryPrice1 = Close;

    EntryBar = CurrentBar;

    setposition(2, market, label:="進場");  // 進場2張

    IsPositionClosed = false; // 設置為未平倉

    PositionSize = 2; // 設置持有的倉位數量為2

end;

 

// 出場邏輯

if position > 0 and not IsPositionClosed then

begin

    // 停損

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

    begin

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

        IsPositionClosed = true; // 設置為已平倉

    end;

 

    // 第一階段停利

    if Close >= EntryPrice1 * (1 + TakeProfit1 / 100) and PositionSize > 0 then

    begin

        setposition(1, market, label:="第一階段停利"); // 出場1張

        PositionSize = PositionSize - 1; // 更新持有的倉位數量

    end;

 

    // 第二階段停利

    if Close >= EntryPrice1 * (1 + TakeProfit2 / 100) and PositionSize > 0 then

    begin

        setposition(PositionSize, market, label:="第二階段停利"); // 出場剩餘倉位

        IsPositionClosed = true; // 設置為已平倉

        PositionSize = 0; // 更新持有的倉位數量

    end;

 

    // 持有時間到

    if CurrentBar >= EntryBar + HoldDays then

    begin

        setposition(0, market, label:="持有時間到");

        IsPositionClosed = true; // 設置為已平倉

    end;

end;

 

// 更新LastVolume (必須放在最後)

LastVolume = Volume;

虎科大許教授 發文於   2024/11/01

你用not IsPositionClosed在這個情況是不對的,因為進場後,你讓它等於false,not false就會變成true。變成True就無法出場,所以才會沒有回測結果。出場邏輯請修改為

if position > 0 and IsPositionClosed = false then

      begin

            ....

      end;

XS小編 發文於   2024/11/06

Hello 布萊恩來了,

 

就您上述腳本的邏輯,小編覺得不需要在腳本中加上 return。

另外與其加 intrabarpersist 變數來控制,直接使用 position 和 filled 來控制會比較適合。

可參考 自動交易語法介紹 裡的範例。

發表回覆
Close