回測有進場,但出場條件無法觸發

  •   459 
  • 最後發表   Eric369  2024 七月 01
Eric369 發文於   2024/06/26

各位大神好,

請問以下腳本編譯正常,回測時有觸法進場但一直無法觸發出場,

我需要怎樣修正才能正常觸發出場?

 

input: largeSellOrderThreshold(10, "拋售單閾值"); 

input: largeBuyOrderThreshold(10, "買進單數量");

input: jumpOpenPercent(3, "跳空百分比"); 

input: priceDropPercent(2, "股價下跌"); 

input: priceRallyPercent(2, "股價急拉"); 

input: lowPointRallyPercent(2, "低點急拉");

input: forcedCoverTime(100000, "強制平倉時間");

 

variable: shortEntryPrice(0), shortEntryDate(0), stopLossPrice(0), coverPrice(0);

variable: isShortPosition(false);

variable: lowPoint(0), marketTime(0), currentTradeDate(0), isLargeSellOrder(false), isLargeBuyOrder(false);

 

marketTime = CurrentTime; 

currentTradeDate = CurrentDate; 

print("Current Market Time: ", NumToStr(marketTime, 6));

 

if marketTime >= 90000 and marketTime <= 100000 then begin

    if ((open / close[1]) - 1) * 100 >= jumpOpenPercent then begin

        if getfield("volume", "tick") >= largeSellOrderThreshold then begin

            isLargeSellOrder = true;

        end else begin

            isLargeSellOrder = false;

        end;

 

        if isLargeSellOrder and ((close / open) - 1) * 100 <= (-priceDropPercent) then begin

            isShortPosition = true;

            shortEntryPrice = close;

            shortEntryDate = currentTradeDate;

            lowPoint = low;

            setposition(-1);

            print("Short at ", close, " on ", NumToStr(shortEntryDate, 8));

        end;

    end;

end;

 

if isShortPosition then begin

    if getfield("volume", "tick") >= largeBuyOrderThreshold then begin

        isLargeBuyOrder = true;

    end else begin

        isLargeBuyOrder = false;

    end;

    if isLargeBuyOrder or ((close / shortEntryPrice) - 1) * 100 >= priceRallyPercent or ((close / lowPoint) - 1) * 100 >= lowPointRallyPercent or marketTime >= forcedCoverTime then begin

        setposition(0);

        print("Cover at ", close, " on ", NumToStr(currentTradeDate, 8));

        isShortPosition = false;

    end;

end;

print("Date: ", NumToStr(currentTradeDate, 8), " - Is Short Position: ", isShortPosition);

 

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

variable: intrabarpersist isShortPosition(false);

XS小編 發文於   2024/07/01

Hello Eric369,

 

如果腳本是以逐筆洗價的方式在運算,那麼在當根Bar中的洗價資訊並不會保存至下次洗價。

要以 intrabarpersist 宣告,K棒中洗價的資訊才會被保存下來。

細節可以參考 intrabarpersist 的說明。

 

另外,建議您可以在腳本中加上print,讓其印出相關數值與條件,會比較容易了解腳本的運算。

 

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

發表回覆
Close