如題 SMA是用每根K棒的 "收盤" 那當日還沒收盤 怎麼計算出均線指標的?
我有一個策略 是希望如果當天漲幅高於(5日均線+1.5個ATR)*0.9 的話 就跌破 (5日均線+1.5個ATR)*0.9 出場
這個條件只限定在當日 所以所有數值都要是當下的條件
那在指標上會不會有失準的問題 因為當日還沒收盤 沒辦法算出準確的(5日均線+1.5個ATR)*0.9
我再交易腳本回測沒有達到我想要的狀況 並沒有觸發跌破 (5日均線+1.5個ATR)*0.9 出場
以下附上程式碼 , 謝謝各位的回答!
// 設定參數
input: money(15, "下單金額(萬)"), // 下單金額(單位萬)
atr_length(5, "ATR計算天數"), // ATR計算天數
high_length(5, "突破高點參考天數"), // 突破高點天數
ma_length(5, "均線天數"), // 均線天數
atr_multiplier(1.5, "ATR倍數"), // ATR倍數
stop_loss_pct(3, "停損百分比"), // 停損百分比
adjustment_factor(0.9, "價格調整倍數"), // 調整倍數
low_length(3, "低點參考天數"); // 低點參考天數
// 宣告變數
var: atr_value(0), // ATR 值
highest_close(0), // 最近 high_length 天的最高收盤價
position_size(0), // 開倉股數
stop_loss_price(0), // 停損價格
ma_plus_atr(0), // 5日均線 + 1.5倍ATR
adjusted_price(0), // (5日均線 + 1.5倍ATR) * 調整倍數
three_day_low(0); // 最近 3 日低點
// 計算 ATR
atr_value = Average(TrueRange, atr_length);
// 計算最近 high_length 天的最高收盤價
highest_close = Highest(Close[1], high_length);
// 計算 5 日均線 + 1.5倍ATR
ma_plus_atr = Average(Close, ma_length) + (atr_value * atr_multiplier);
// 計算調整後的價格
adjusted_price = ma_plus_atr * adjustment_factor;
// 計算最近 low_length 天的最低價
three_day_low = Lowest(Low, low_length);
// 開倉邏輯
if Position = 0 and Close > highest_close then begin
// 計算開倉股數(取整數)
position_size = IntPortion((money * 10000) / (Close * 1000));
if position_size > 0 then begin
SetPosition(position_size); // 進場
stop_loss_price = FilledAvgPrice * (1 - stop_loss_pct / 100); // 設定初始停損價格
end;
end;
// 停損邏輯:跌破庫存成本 - 3% 出場
if Position > 0 and Close < FilledAvgPrice * (1 - stop_loss_pct / 100) then begin
SetPosition(0); // 平倉
end;
// 停利邏輯:跌破條件出場
if Position > 0 then begin
// 如果當天股價大於調整價格
if High >= adjusted_price then begin
if Close < adjusted_price then begin
SetPosition(0); // 平倉
end;
end;
// 跌破最近3日低點出場
if Close < three_day_low then begin
SetPosition(0); // 平倉
end;
end;

7 評論