請教小幫手關於 策略停利(損)後的策略中止

  •   186 
  • 最後發表   小雷  2022 十月 29
小雷 發文於   2022/10/26

小幫手 您好:

兩個問題想請教您,謝謝您。

 

Q1.下面為部位達到停損停利後,策略即停止的程式,不知道寫法是否正確?

(主要是為了避免出場後,在同一根K棒又達到進場條件,而再次進場)

-------------------------------------

var: intrabarpersist record(0);

if Position = 1 and Filled = 1 then begin

 

if profit_point > 0 and Close >= FilledAvgPrice + profit_point and record = 0

then begin SetPosition(0); { 停利 }

record = 1;

 

end else if loss_point > 0 and Close <= FilledAvgPrice - loss_point and record = 0

then begin SetPosition(0); { 停損 }

record = 1;

    

end else if condition1 and record = 0

then begin SetPosition(0); { 條件一成立後平倉 }

record = 1;

end;

end;

 

if position = 0 and filled = 0 and record = 1 then raiseruntimeerror("出場");

 

Q2:這樣的策略,在達到出場之後,新一輪部位(position=0-->1)策略會如何再次啟動? 是隔天自動啟動,還是必須要人工去啟動?

排序方式: 標準 | 最新
XQ小幫手 發文於   2022/10/27

 Hello 小雷,

 

1.您可以這樣作沒錯 (出場後中斷執行該商品),但更簡單的作法是讓 record 紀錄當下那根bar的編號。

record = CurrentBar;

這樣進場判斷只要多一個條件: condition1 = record <> currentbar;

這樣出場後最少要到下根Bar才能交易。

 

2.如果您有設定自動交易中心排程的話,那麼就會自動啟動。

不然被 raiseruntimeerror 中斷的商品是不會自動重新執行的,除非手動重啟或有排程。

 

小雷 發文於   2022/10/27

非常謝謝您

小雷 發文於   2022/10/27

小幫手 您好

請問您的意思是否是這樣寫? 這樣是不是在出場後就不會在同一根Bar"再次觸發"進場了?

謝謝您

---------------------------

{進場}

if Position = 0 and Filled = 0 then begin

var: intrabarpersist record(0);

 

condition1 = 進場條件1

condition2 = 進場條件2

fi condition1 and condition2 and record <> currentbar then SetPosition(1)

count = Currentbar;

end;

{出場}

if Position = 1 and Filled = 1 then begin

if profit_point > 0 and Close >= FilledAvgPrice + profit_point

then begin SetPosition(0); { 停利 }

 

end else if loss_point > 0 and Close <= FilledAvgPrice - loss_point

then begin SetPosition(0); { 停損 }

 

end else if condition3

then begin SetPosition(0); { 出場條件3成立後平倉 }

 

end;

end;

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

Hello 小雷,

 

小幫手不太清楚您的 count 是哪裡來的,不過小幫手會這樣寫:

var: intrabarpersist record(0);

 

if Position = 0 and Filled = 0 then begin

condition1 = 進場條件1

condition2 = 進場條件2

 

if condition1 and condition2 and record <> currentbar then SetPosition(1);

 

end;

 

{出場}

 

if Position = 1 and Filled = 1 then begin

 

if profit_point > 0 and Close >= FilledAvgPrice + profit_point then begin 

    SetPosition(0); { 停利 }

    record = currentbar;

    end

else if loss_point > 0 and Close <= FilledAvgPrice - loss_point then begin 

    SetPosition(0); { 停損 }

    record = currentbar;

    end

else if condition3 then begin 

    SetPosition(0); { 出場條件3成立後平倉 }

    record = currentbar;

    end;

end;

 

這樣的話 record 會記錄出場時的 currentbar,同一根Bar不會再度進場。

您如果是在進場的時候設定 record = currentbar; 那就會是同一根Bar不會進場2次。

小雷 發文於   2022/10/29

小幫手 您好,非常謝謝您,幫助很大。

發表回覆
Close