自動交易:執行時發生錯誤(1101)取成交紀錄資訊時,索引值超出範圍

  •   324 
  • 最後發表   帥哥元  2025 二月 21
帥哥元 發文於   2025/02/20

如題

 

 

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

應該是你的程式碼有問題。延續前次執行只有1檔股票8101華冠,錯誤訊息顯示你要抓成交紀錄,很顯然除了這檔以外,其他股票是沒有成交紀錄的。要看看你程式怎麼寫,才知道問題在哪裡。

帥哥元 發文於   2025/02/21

1. 上週都還沒問題,這周才出現問題

2. 好幾個策略都有相同問題

3. 下面是其中一隻有相同問題的策略程式碼,麻煩許教授指導一下,謝謝

虎科大許教授 發文於   2025/02/21

圖看不清楚。能否貼上程式碼,這樣也方便驗證。

帥哥元 發文於   2025/02/21

if BarFreq <> "D" then RaiseRunTimeError("限用日K(逐筆洗價)"); //防呆機制

if SymbolType <> 2 then Return; //只有股票才執行程式

 

//=======================================參數(變數)宣告區=============================================

input: Profit(150, "股票獲利%"), StopLoss(15, "股票停損%"), HoldDay(20, "持有天數(含假日)");

input: budget(100000, "每檔股票預算"); //預算控制下單數量,例如預算=0,是要下單一張 or 不下單

 

Var: lots(0); //預算控制可以購買幾張股票

 

 

//======================================進出場區====================================================

Once Alert("持有天數=",NumToStr(DateDiff(CurrentDate, FilledRecordDate(FilledRecordCount)), 0),"進場日=",NumToStr(FilledRecordDate(FilledRecordCount),0));

 

{進場}

if Position = 0 and Filled = 0 and EnterMarketCloseTime(5) then begin //13:20之後才會進場

lots = GetOrderQuantity("S", budget, "N"); //lots = 可下單張數

if lots <> 0 then SetPosition(1*lots, Market, label:="突破進場");

end;

 

Once (CurrentDate = FilledRecordDate(FilledRecordCount))

begin

//Telegram 通知

Print(File("C:\SysJust\XQLite\XS\Print\"),"======突破進場======");

Print(File("C:\SysJust\XQLite\XS\Print\"),"股票代碼:",Symbol);

Print(File("C:\SysJust\XQLite\XS\Print\"),"股票名稱:",SymbolName);

Print(File("C:\SysJust\XQLite\XS\Print\"),"進場日期:",NumToStr(CurrentDate, 0));

Print(File("C:\SysJust\XQLite\XS\Print\"),"進場時間:",Text(NumToStr(Hour(CurrentTime),0),":", NumToStr(Minute(CurrentTime),0),":", NumToStr(Second(CurrentTime),0)));

Print(File("C:\SysJust\XQLite\XS\Print\"),"進場價格:",NumToStr(Close, 2));

end;

 

if Position > 0 and Filled > 0 then begin

if SetTargetProfitByPercent(Profit) = True then SetPosition(0, Market); //獲利 n% 後出場

if SetStopLossByPercent(StopLoss) = True then SetPosition(0, Market); //虧損 n% 後出場

if DateDiff(CurrentDate, FilledRecordDate(FilledRecordCount)) >= HoldDay and EnterMarketCloseTime(1) then SetPosition(0, Market, label:="持有N天以上強制出場");

end;

 

虎科大許教授 發文於   2025/02/21

(1)程式會跑歷史K棒,在歷史的第一根K棒,你的

Once Alert("持有天數=",NumToStr(DateDiff(CurrentDate, FilledRecordDate(FilledRecordCount)), 0),"進場日=",NumToStr(FilledRecordDate(FilledRecordCount),0));

就已經執行一次,後面不會再執行。加上要抓庫存日期等數據,前提是有庫存,因此上述程式碼的前面應加上if position<>0 then ...

(2)若今天沒建立庫存,則FilledRecordCount=0,

Once (CurrentDate = FilledRecordDate(FilledRecordCount))

會出現你描述的錯誤。這裡一樣在前面要加上if position<>0 then ...

帥哥元 發文於   2025/02/21

可以了,謝謝許教授

發表回覆
Close