這樣寫有點風險
除非 conditionB 有包含 filled <> 1 條件,第13、16、19行每次其實都會跑到,而且第13行 Position < 1 的條件會先跑到,有可能導致第16行和第19行 Position = 1 and filled = 1 沒機會被觸發。做空條件也有同樣問題。
建議用大範圍條件包起來處理:
var : conditionB(false), //做多條件
conditionS(false), //做空條件
conditionT(false), //是否已經進入收盤階段
addconditionB(false), //多單加碼
addconditionS(false); //空單加碼
//是否已經進入收盤階段
conditionT = EnterMarketCloseTime(Period3);
// 停損區先寫,避免邏輯上先跑去進場區,導致觸發不到停損區
// 多寫一個 FilledAvgPrice <> 0 做個防呆
// 原本條件上少了 > 和 <,如果真的觸發加碼,反而你會無法觸發停損
if FilledAvgPrice <> 0 then begin
if Position >= 1 and Filled >= 1 and Close <= (FilledAvgPrice-100)
then SetPosition(0,label:="做多停損");
if Position <= -1 and Filled <= -1 and Close >= (FilledAvgPrice+100)
then SetPosition(0,label:="做空停損");
end;
// 不在收盤階段才去判斷這些進場參數
if not conditionT
then begin
conditionB = xxx; //做多條件
conditionS = xxx; //做空條件
addconditionB = xxx; //多單加碼條件
addconditionS = xxx; //空單加碼條件
// 進場區加碼先寫,理由與停損區先寫一樣,雖然這次 Position 和 Filled 有寫清楚比較沒差
if Position = 1 and Filled = 1 and addconditionB then SetPosition( 2,label:="做多加碼");
if Position = -1 and Filled = -1 and addconditionS then SetPosition( -2,label:="做空加碼");
if Position = 0 and Filled = 0 and conditionB then SetPosition( 1,label:="做多");
if Position = 0 and Filled = 0 and conditionS then SetPosition( -1,label:="做空");
end;
try try see
6 評論