請問許教授,程式如下,指標都有箭頭買進跟賣出,但回測都沒任何成交,都是0,不知為何,再麻煩教授幫看一下
{
腳本名稱:Supertrend 跨頻 KD 交易策略 (限定加碼 3 次)
腳本類別:交易腳本
建議頻率:60 分鐘 (60M)
}
input:
_ATR_Len(10, "ATR期數"),
_Mult(3.0, "ATR乘數"),
_ATR_Mode(2, "ATR模式", InputKind:=Dict(["Wilder(RMA)", 1], ["Simple(SMA)", 2])),
_K_Len(9, "KD-K週期"),
_D_Len(3, "KD-D週期"),
_OrderQty(1, "基本買進張數/口數"),
_MaxAddCount(3, "最高加碼次數"); // 這次新增的限制參數
var:
_ATR_Val(0), _Src(0), _Up_Basic(0), _Dn_Basic(0),
_Up_Line(0), _Dn_Line(0), _Trend(1), _ST_Line(0),
_K(0), _D(0), _RSV(0);
// ------------------------------------------------------------
// 1. KD 與 Supertrend 核心邏輯 (維持不變)
// ------------------------------------------------------------
stochastic(_K_Len, _D_Len, _D_Len, _K, _D, _RSV);
_Src = (High + Low) / 2;
_ATR_Val = iff(_ATR_Mode = 1, ATR(_ATR_Len), Average(TrueRange, _ATR_Len));
_Up_Basic = _Src - (_Mult * _ATR_Val);
_Dn_Basic = _Src + (_Mult * _ATR_Val);
if CurrentBar = 1 then begin
_Up_Line = _Up_Basic; _Dn_Line = _Dn_Basic; _Trend = 1;
end else begin
if Close[1] > _Up_Line[1] then _Up_Line = MaxList(_Up_Basic, _Up_Line[1]) else _Up_Line = _Up_Basic;
if Close[1] < _Dn_Line[1] then _Dn_Line = MinList(_Dn_Basic, _Dn_Line[1]) else _Dn_Line = _Dn_Basic;
if _Trend[1] = -1 and Close > _Dn_Line[1] then _Trend = 1
else if _Trend[1] = 1 and Close < _Up_Line[1] then _Trend = -1
else _Trend = _Trend[1];
end;
_ST_Line = iff(_Trend = 1, _Up_Line, _Dn_Line);
// ------------------------------------------------------------
// 2. 交易訊號定義
// ------------------------------------------------------------
// 條件 A:首次翻多
condition1 = (_Trend = 1 and _Trend[1] = -1);
// 條件 B:回踩支撐加碼
condition2 = (_Trend = 1 and _Trend[1] = 1 and Low <= _ST_Line and Close > _ST_Line);
// 條件 C:跌破趨勢出場
condition3 = (Close < _ST_Line);
// ------------------------------------------------------------
// 3. 執行下單 (含次數限制邏輯)
// ------------------------------------------------------------
// A. 首次進場 (Position 為 0 時)
if Position = 0 and condition1 then begin
SetPosition(_OrderQty * 1000, Label:="趨勢翻多買進");
end;
// B. 限定次數加碼
// 計算邏輯:首買 1 單位 + 加碼 N 單位。所以上限是 (1 + _MaxAddCount) 個單位
// 只有在【已有部位】且【部位尚未達到上限】時才執行加碼
if Position >= (_OrderQty * 1000)
and Position < (_OrderQty * (1 + _MaxAddCount) * 1000)
and condition2 then begin
SetPosition(Position + _OrderQty * 1000, Label:="回踩加碼");
end;
// C. 全部出場
if Position > 0 and condition3 then begin
SetPosition(0, Label:="跌破趨勢全平倉");
end;
4 評論