當沖,時間到出清所有部位,有問題

  •   80 
  • 最後發表   阿頂  2025 七月 22
阿頂 發文於   2025/07/15

 

 

if time >= _exit_time and position <> 0 then

begin

    SetPosition(0);

    RaiseRunTimeError("超過時間全部出清");

end;

 if filled > 0 then

begin

    _stop_loss = FilledAvgPrice * (1 - _long_sl_percent / 100);    

print("8多單停損價格:",_stop_loss);

    _take_profit = FilledAvgPrice * (1 + _long_tp_percent / 100);   

print("9多單停利價格:",_take_profit);

if close <= _stop_loss then                              

    begin

        SetPosition(0);                       

        print("10current close=",close);

        print("11停損值:",_stop_loss);

        RaiseRunTimeError("多單停損結束執行");        

    end;

 

    if close >= _take_profit then

    begin

        SetPosition(0);  

print("12current close=",close);

print("13停損值:",_take_profit);

        RaiseRunTimeError("多單停利結束執行");   

    end;

end;

 

if filled < 0 then

begin

    _stop_loss = FilledAvgPrice * (1 + _short_sl_percent / 100);  

print("14空單停損價格:",_stop_loss);

    _take_profit = FilledAvgPrice * (1 - _short_tp_percent / 100); 

print("15空單停利價格:",_take_profit);

if close >= _stop_loss then   

    begin

        SetPosition(0);   

print("16current close=",close);

print("17停損值:",_stop_loss);

RaiseRunTimeError("空單停損結束執行"); 

     end;

 

    if close <= _take_profit then

    begin

        SetPosition(0);    

print("18current close=",close);

print("19停損值:",_take_profit);

        RaiseRunTimeError("空單停利結束執行");  

    end;

end;

結果 

 只有停止執行,而部位都沒有出清,請看附檔

請問為何?

附加文件

排序方式: 標準 | 最新
XS小編 發文於   2025/07/17

Hello 阿頂,

 

交易指令要在當次洗價腳本運算完後才會執行,而 RaiseRunTimeError 是在腳本運算到的時候就馬上執行。

因此委託送出前策略就已經中斷。

建議可以用intrabarpersist的變數來紀錄是否觸發時間到出清的邏輯,確認出場委託送出後 (position = 0) 再使用 RaiseRunTimeError 中斷。

阿頂 發文於   2025/07/22

 看不懂您說的意思,光以下這段就不知如何改了

if time >= _exit_time and position <> 0 then

begin

    SetPosition(0);

    RaiseRunTimeError("超過時間全部出清");

end;

虎科大許教授 發文於   2025/07/22

所有的RaiseRunTimeError指令都用變數取代。

var: intrabarpersist finishSession(false);

if isFirstCall("Date") then finishSession=false;

if finishSession=true then raiseRunTimeError("交易結束");

if time >= _exit_time and position <> 0 then

   begin

      SetPosition(0);

      finishSession=true;

   end;

發表回覆
Close