寫了一個簡單策略 ,邏輯為 (突破20天高進場,虧損兩個atr停損, 跌破10天低點停利)
並且控制15萬一開 代碼如下,
但回測很明顯失真,我找不出問題點,上來請教達人(檔案附上條件設定)
// 設定參數
input: capital(15, "開倉部位(萬)"),
atr_length(14, "ATR計算天數"),
high_length(20, "突破高點參考天數"),
low_length(10, "停利低點參考天數"),
atr_multiplier(2, "ATR停損倍數");
// 宣告變數
var: atr_value(0), // ATR 值
highest_close(0), // 最近 20 天的最高收盤價
stop_loss_price(0), // 停損價格
trailing_low(0), // 最近 10 天的最低價(作為停利參考)
position_size(0); // 買入股數
// 計算 ATR
atr_value = Average(TrueRange, atr_length);
// 計算最近 high_length 天的最高收盤價
highest_close = Highest(Close[1], high_length);
// 開倉邏輯
if Position = 0 and Close > highest_close then begin
// 計算開倉股數(取整數),假設每張股數為 1000 股
position_size = IntPortion((capital * 10000) / (Close * 1000));
if position_size > 0 then begin
SetPosition(position_size); // 設定部位
stop_loss_price = Close - (atr_value * atr_multiplier); // 設定停損價格
end;
end;
// 停損邏輯
if Position > 0 and Close <= stop_loss_price then begin
SetPosition(0); // 平倉
end;
// 計算最近 low_length 天的最低價
trailing_low = Lowest(Low, low_length);
// 停利邏輯
if Position > 0 and Close <= trailing_low then begin
SetPosition(0); // 平倉
end;
6 評論