移動固定停損移動停利

  •   1.1K 
  • 最後發表   endy  2022 四月 27
endy 發文於   2022/04/20

請問 空單 設定固定停損2% 有獲利2%時啟動移動停損1%

相關編輯如下是否有誤 經常遇到空單沒有回補的狀況

如何在底下追加 最晚收盤前要回補

以及開啟逐筆下單時間經常提早下單

追加下單時的訊息顯示停損停利價格

 

input: profit_percent(2, "停利(%)"); 

input: profit_drawback_percent(1, "停利回跌(%)"); 

input: loss_percent(2, "停損(%)"); 

 

if profit_percent = 0 then raiseruntimeerror("請設定停利(%)"); 

if profit_drawback_percent = 0 then raiseruntimeerror("請設定停利回跌(%)"); 

if profit_drawback_percent > profit_percent then raiseruntimeerror("停利(點)需大於停利回跌(%)"); 

 

 

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

var: intrabarpersist max_profit_percent(0);{ 啟動停利後最大獲利% } 

 

if loss_percent < 0 and Close >= FilledAvgPrice + loss_percent then begin 

{ 停損 } 

SetPosition(0); 

max_profit_percent = 0; 

 

end else begin 

 

{ 判斷是否要啟動停利 } 

if max_profit_percent = 0 and Close <= FilledAvgPrice - profit_percent then begin 

max_profit_percent = Close; 

end; 

 

if max_profit_percent <> 0 then begin 

if Close >= max_profit_percent + profit_drawback_percent then begin 

{ 停利 } 

SetPosition(0); 

max_profit_percent = 0; 

end else if Close < max_profit_percent then begin 

{ 移動最大獲利點 } 

max_profit_percent = Close; 

end; 

end; 

end; 

 

end;

XQ小幫手 發文於   2022/04/27

Hello endy,

 

您可以參考內建的固定停損停利腳本來修改。

另外您在計算停損停利時input設定為百分比,那麼 FilledAvgPrice + loss_percent 這種計算方式就會是錯誤的。

舉例來說,您 loss_percent 設為2,假設進場價格位20好了,那麼 FilledAvgPrice + loss_percent 就會是 20 + 2 = 22 (10%) 而不是 20* 1.02 = 20.4 (2%)。

正確的百分比計算方法應為 FilledAvgPrice * (1 + (loss_percent / 100))。

建議您可以實際將相關數值print出來檢查。

 

收盤前要回補,您可以用currenttime來限制。

舉例來說,13:20以後要強制出場可以這樣寫:

if currenttime >= 132000 and filled <> 0 then setposition(0, market);

需注意股票交易時間最後5分鐘為集合競價,XQ自動交易在這段時間不會下單。所以要提早出場。

 

提早下單這點無法用設定來辦到,您需要自行撰寫腳本,讓條件在快達成時就預先下單。(換句話說,降低條件標準,然後使用限價單)

但需注意若下出的單優於現在市場價格的話會直接成交。

 

下單時由於還未成交,所以filledavgprice會是0,自然也算不處實際的停損停利價格。

或許您可以用當下的觸發價 (ex. 收盤價) 作為替代計算停損停利點。

要將訊息顯示出來的話,您可以使用print,或是在setposition裡面使用label。

發表回覆
Close