停利後如何停止腳本執行

  •   212 
  • 最後發表   路人小員工  2023 七月 11
路人小員工 發文於   2023/07/04

if CurrentTime >= 084500 and CurrentTime <= 130000

Then begin

if Position = 0 and condition1 and condition3 and value1 > value10 then begin SetPosition(1, value1-10);

end;

if Position = 0 and condition5 and condition1 and condition3 and value1 < value10 then begin SetPosition(1, value1-10);

end;

if Position = 0 and condition6 and condition1 and condition3 and value1 < value10 then begin SetPosition(-1, value1+5);

end;

if Position = 0 and condition1 and condition4  then begin SetPosition(-1, value1+5);

end;

if Position = 0 and condition2 and condition3  then begin SetPosition(1, value1);

end;

if Position = 0 and condition7 and condition2 and condition4  then begin SetPosition(-1, value1+5);

end;

if Position = 0 and condition8 and condition2 and condition4  then begin SetPosition(1, value1-10);

end;

 

//出場狀況

input: profit_point(60, "停利(點)");

input: loss_point(25, "停損(點)");

if Position = 1 and Filled = 1 then begin

if  Close < FilledAvgPrice - loss_point then begin SetPosition(0,market); { 停損 }

end; 

if  close >= FilledAvgPrice + profit_point then begin SetPosition(0,market); { 停利 }

end; 

if Position = -1 and Filled = -1 then begin

if  Close > FilledAvgPrice + loss_point then begin SetPosition(0,market);{ 停損 }

end;

if close <= FilledAvgPrice - profit_point then begin SetPosition(0,market);{ 停利 }

end;

進場觸發條件與買進後出場條件如上,目前想要設定能進場2次,如果達成停利的話,就只執行一次,如果先停損,就會執行第二次,

但目前不知道RaiseRunTimeError放在哪裡才能得到我要的效果,麻煩小幫手指點一下。

排序方式: 標準 | 最新
XQ小幫手 發文於   2023/07/06

 Hello 路人小員工,

 

既然您要在停利的時候中止,那麼就將 RaiseRunTimeError 放在停利那邊即可,例如:

if  close >= FilledAvgPrice + profit_point then begin 

    SetPosition(0,market); { 停利 }

    RaiseRunTimeError(""停利出場故中斷);

    end; 

 

至於停損要執行兩次的話,可以用變數紀錄執行過的次數,如果超過的話2的話才中止。

舉例來說:

var: intrabarpersist count(0);

 

if  Close < FilledAvgPrice - loss_point then begin 

    SetPosition(0,market); { 停損 }

    count += 1;     //用value1紀錄執行過的次數

    if count >= 2 then RaiseRunTimeError(""停損出場2次故中斷);

    end; 

路人小員工 發文於   2023/07/07

多謝小幫手的回應!另外在請教一個問題,

原本的想法是如果做多成交一口後,

(1)低點碰到支撐點,便在value1-10買回停損,

(2)跌到FilledAvgPrice - 25也是停損,

(3)漲到FilledAvgPrice + 60就停利。

但回測後發現如果觸發到(1),就算到(2)也不會停損,甚至到了尾盤平倉也不會停損。

請問該怎麼寫才能夠達到觸發(1)如果沒回value1-10停損,也會再跌到FilledAvgPrice - 25停損。

如果沒有到觸發(1),直接發生觸發(2),FilledAvgPrice - 25也能停損的寫法,謝謝小幫手!

if CurrentTime >= value14 and CurrentTime <= 134200

Then begin

if Position = 1 and Filled = 1 then begin          

    if  low <= value11 then begin

   { 停損 }

    SetPosition(0,value1-10);

end;

if  Close < FilledAvgPrice - 25 then begin

{ 停損 }

SetPosition(0,market);

end; 

if  close >= FilledAvgPrice + 60 then begin

{ 停利 }

SetPosition(0,market);

end; 

end;

/尾盤平倉

if CurrentTime >= 134310 and CurrentTime <= 134430 and Position <> 0   { 接近收盤,市價平倉 }

Then begin

SetPosition(0, MARKET);                      { 回補 }

end;

 

XQ小幫手 發文於   2023/07/11

 Hello 路人小員工,

 

您可以參考 setposition 裡的說明。

如果同時有複數個setposition成立的話,只會執行第一個。

就小幫手看來,您是因為(1)先執行到,所以(1)和(2)同時觸發時只會執行(1)。

 

最簡單的改法就是讓其依重要度排序,您希望在(1)和(2)同時觸發時要優先執行(2)的話,只要該部分的腳本向上挪到(1) 的腳本上方即可。

發表回覆
Close