想請問為何以下當沖出場的程式碼,如果已進場三支股票,只有一支股票能達到停損或停利條件而平倉,而其他股票則不會有平倉動作?也不會在指定時間強制平倉?原因為何?
// === 參數設定 ===
input:
profit_percent(2.5, "停利(%)"),
loss_percent(2, "停損(%)"),
close_time(114500, "收盤前強制平倉時間");
// === 自訂變數 ===
var: firstEntryDate(0); // 記錄首次建倉日(每支股票獨立)
var: prevPosition(0); // 記錄上根K棒的部位,用來偵測是否剛進場
// === 進場日期記錄 ===
// 若當前有部位且前一根無部位 → 剛進場,記錄 today
if Position > 0 and prevPosition = 0 then
firstEntryDate = Date;
// === 停利停損條件(僅限今日建倉部位) ===
if Position > 0 and firstEntryDate = Date and FilledAvgPrice > 0 then begin
// === 停利條件 ===
if Close >= FilledAvgPrice * (1 + 0.01 * profit_percent) then begin
SetPosition(0);
end
// === 停損條件 ===
else if Close <= FilledAvgPrice * (1 - 0.01 * loss_percent) then begin
SetPosition(0);
end;
end;
// === 收盤前強制平倉(僅限今日建倉部位) ===
if Time >= close_time and Time <= 130000 and Position > 0 and firstEntryDate = Date then begin
SetPosition(0);
end;
// === 記錄上一根K棒的部位狀態,方便下根偵測首次進場 ===
prevPosition = Position;
1 評論