Hi 教授您好, 依照您的建議, 改成以下的方式, 是否更清楚明瞭?
不確定還有沒有什麼可能的Bug, 您能夠一眼看出? 方便指教
// 變數宣告區
var: intrabarpersist trigger_count(0);
var: intrabarpersist trigger_time(0);
var: entry_price(0);
// =================================================================================================
// 1. 進場邏輯處理 (Entry Logic)
// =================================================================================================
if Position = 0 and Filled = 0 and trigger_count = 0 then begin
// 條件:當前K棒最高價 > 前一根K棒最高價
if h[0] > h[1] then begin
entry_price = h[1] + 1; // 設定進場價
SetPosition(1, entry_price);
// 紀錄進場觸發時間
trigger_time = Time;
end;
end;
// =================================================================================================
// 2. 持倉/掛單狀態處理 (Position Management)
// =================================================================================================
if Position > 0 then begin
// ---------------------------------------------------------
// A. 尚未成交 (Pending Order Management)
// ---------------------------------------------------------
if Filled = 0 then begin
// 刪單條件:沒有持續創新高,且價格跌破前低
if h[0] <= h[1] and c[0] < l[1] then begin
CancelAllOrders(); // 取消掛單
// 注意:這裡不設 SetPosition(0),因為還沒成交,不需要平倉指令
end;
end
// ---------------------------------------------------------
// B. 已經成交 (Open Position Management)
// ---------------------------------------------------------
else if Filled > 0 then begin
// 標記已成交狀態 (防止重複進場,直到重置)
if trigger_count = 0 then trigger_count = 1;
// 條件1:停損出場 (Stop Loss)
// 邏輯:價格跌破前一根K棒低點
if c[0] < l[1] then begin
SetPosition(0); // 市價平倉
// 重置狀態,準備下一次交易
trigger_count = 0;
trigger_time = 0;
end
// 條件2:時間出場 (Time Exit)
// 邏輯:持倉超過 5 分鐘
else if TimeDiff(Time, trigger_time, "M") >= 5 and trigger_time > 0 then begin
SetPosition(0); // 市價平倉
// 重置狀態
trigger_count = 0;
trigger_time = 0;
end;
end;
end;
4 評論