回測時發生錯誤

  •   113 
  • 最後發表   老王837  2022 二月 08
老王837 發文於   2022/01/30

自動交易回測時出現很奇怪的錯誤[1101], 不管用不用逐筆洗價都會出現這個錯誤, 找了很久發現跟下列含式用法有關

SetPosition(0, MARKET);{ 以市價賣出 }

if Position = 0 

then begin 

if _buy_keep > close then _print_str = "賣空停利";

if _buy_keep <= close then _print_str = "賣空停損";

if filledRecordPrice(FilledRecordCount) > _buy_keep   then _print_str = "買多停利";

if filledRecordPrice(FilledRecordCount) <= _buy_keep  then _print_str = "買多停損";

Print(file("c:\temp\"), currentDate , currentTime, "買多",_mode,filledRecordPrice(FilledRecordCount),_print_str);

 

問題出在這個 filledRecordPrice(FilledRecordCount) , 這樣子寫不是應該會取得最後一筆交易的單價嗎?

感覺很像系統回測的Bug 

請協助解決

 

 

XQ小幫手 發文於   2022/02/08

Hello 老王837,

 

FilledRecordCount 回傳的是到目前為止的成交紀錄筆數。

換句話說,當您腳本未交易的時侯,此函數會回傳0。

FilledRecordPrice 需傳入的數值是從1開始。

因次會導致超出陣列的錯誤。

您可以先檢查 FilledRecordCount 是否為0,再使用 FilledRecordPrice 。

舉例來說:

if Position = 0 then begin 

    if _buy_keep > close then _print_str = "賣空停利";

    if _buy_keep <= close then _print_str = "賣空停損";

 

    if FilledRecordCount <> 0 then begin

        if filledRecordPrice(FilledRecordCount) > _buy_keep   then _print_str = "買多停利";

        if filledRecordPrice(FilledRecordCount) <= _buy_keep  then _print_str = "買多停損";

        end;

 

    Print(file("c:\temp\"), currentDate , currentTime, "買多",_mode,filledRecordPrice(FilledRecordCount),_print_str);

    end;

發表回覆
Close