自動交易未自動停損停利

  •   198 
  • 最後發表   alexw  2022 八月 10
alexw 發文於   2022/08/05

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

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

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

 中間略過...

if  condition1 and condition2 and condition3 and condition4 

then setposition(-1, market);

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 * (1 + (loss_percent / 100)) 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; 

if (position < 0 and close >= getfield("漲停價", "D") * 0.99) or 
(position < 0 and close <= getfield("跌停價", "D") *1.01) or (position < 0 and currenttime >= 132000)  then setposition(0, market);

end; 

end; 

end; 



end;

 

不知哪裡沒寫好?

 

排序方式: 標準 | 最新
musashi 發文於   2022/08/05

1. 29行if loss_percent < 0就不會停損了。

2. 要確認一下停損停利要用金額或是%,發現你有些計算方式金額跟%沒有分清楚(45、55行),金額才可以直接加減。

3.腳本盡量簡單化,如69、70行position < 0是多餘判斷,因為已經包覆在23行的Position = -1裡面,個人比較建議不要用太多層的巢狀if+else撰寫,如果沒有天天寫腳本,過陣子再來看會很吃力。

alexw 發文於   2022/08/05

再研究,有些也是從別的文章抄來的.. 話說時間到1320時也沒自動平倉

XQ小幫手 發文於   2022/08/10

Hello alexw,

 

您可以參考內建的交易腳本,裡面有各種不同方式的停損停利寫法範例。

另外,您的時間到出場是放在條件裡面。

要先達成 Close < max_profit_percent 的條件才會執行,且隨著您策略的設定不同,腳本運算時可能已經無法交易。

(ex.用30分鐘頻率無逐筆洗價的話,收盤前的最後一次洗價會是在13:00的時候)

小幫手會建議您可以使用print來檢查,或是將

if (position < 0 and close >= getfield("漲停價", "D") * 0.99) or 

(position < 0 and close <= getfield("跌停價", "D") *1.01) or (position < 0 and currenttime >= 132000)  then setposition(0, market);

這段放到腳本最前方確保執行。

因為如果同時有其他的SetPosition符合條件的話,會優先執行最前面的 SetPosition

 

如果還是有問題的話,麻煩提供 自動交易中心匯出檔勾選(包含)交易腳本、問題發生的日期時間商品 以及 XQ Log 來檢驗。

Log資料夾(預設路徑:C:\SysJust\XQLite\LOG)直接壓縮後提供即可。

您可以直接將檔案上傳,如果檔案過大的話也可以Mail至客服信箱 XQservice@XQ.com.tw 且務必附上 討論文章連結網址(小幫手才能盡早處理)。

感謝。

發表回覆
Close