請問各位大神!
小弟想要利用XQ自動交易管理庫存用於"當沖"紀律進出~
但是回測時常常發生幾筆無法順利平倉!在實在不知道問題在哪裡~~
條件設定了停利停損也加上萬一盤整 一律在指定時間平倉 但結果一樣摸不著頭緒
以下是我的出場程式碼 (為了回測 新增簡單的入場示範用)
// === 穩定版:突破首根5分K高點 → 進場;移動停利 + 固定停損 ===
Inputs:
stopLossPct(1.5), // 停損%
trailStartPct(1.5), // 啟動移動停利%
trailGapPct(0.8), // 回落%
forceExitTime(132000), // 強制平倉時間
firstBarEndTime(091500); // 第一根5分K收盤時間
Vars:
entryPx(0), // 進場價
peakPx(0), // 持倉中最高價
trailActive(false), // 是否啟動移動停利
inTrade(false), // 是否已進場
holdBars(0), // 持倉後累積K棒數
firstHigh(0); // 第一根K棒高點
// === 每日 Reset ===
If Date <> Date[1] Then
Begin
entryPx = 0;
peakPx = 0;
trailActive = false;
inTrade = false;
holdBars = 0;
firstHigh = 0;
End;
// === 記錄第一根 5分K 高點 ===
If Time = firstBarEndTime Then
firstHigh = High;
// === 突破第一根高點進場(只進場一次)===
If not inTrade and firstHigh > 0 and Close > firstHigh Then
Begin
Buy(1);
entryPx = Close;
inTrade = true;
holdBars = 0;
End;
// === 出場邏輯 ===
If inTrade Then
Begin
holdBars = holdBars + 1;
// 停損
If holdBars >= 1 and Close <= entryPx * (1 - stopLossPct / 100) Then
Begin
SetPosition(0);
inTrade = false;
trailActive = false;
End;
// 啟動移動停利
If not trailActive and Close >= entryPx * (1 + trailStartPct / 100) Then
Begin
trailActive = true;
peakPx = Close;
End;
// 更新最高價
If trailActive and Close > peakPx Then
peakPx = Close;
// 移動停利觸發
If trailActive and Close <= peakPx * (1 - trailGapPct / 100) Then
Begin
SetPosition(0);
inTrade = false;
trailActive = false;
End;
// 時間強制出場
If Time >= forceExitTime Then
Begin
SetPosition(0);
inTrade = false;
trailActive = false;
End;
End;
希望有大神可以幫我看一下 問題是在XQ回測系統還是這支程式出場的邏輯有疏漏?!謝謝
1 評論