自動交易N日後出場寫法

  •   310 
  • 最後發表   chaoyueh  2021 四月 27
chaoyueh 發文於   2021/04/26

小幫手好

自動交易策略,進場後想要幾天後的幾點出場,要如何寫呢? 嘗試了幾種寫法,回測大多無法出場,或只會出場第一筆,請協助,謝謝

寫法1: 這個方式會出場第一筆,但是第二筆進了之後,就不再出場

if time=093000 and Symbol = "2330.TW" and position=0 then begin

   setposition(1)

end;

if position=1 and filled=1 then begin

    if datediff(date, filledRecordDate(1))=1 and time=130000 then setposition(0);

end; 

寫法2:沒有出場

if time=093000 and Symbol = "2330.TW" and position=0 then begin

   setposition(1);

   value1=date;

end;

if position=1 and filled=1 then begin

   if datediff(date, value1)=1 and time=130000 then setposition(0);

end;  

 

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

Hello chaoyueh,

 

您第一種寫法 FilledRecordDate 裡應該要放 FilledRecordCount,不然他會去抓第一筆成交的日期,而不是最近成交的日期。

另外如果您條件為等於的話,禮拜五買的商品到禮拜一就超過1天了,會導致出不了場。

小幫手將您的腳本修改後給您作參考。

 

if time=093000 and Symbol = "2330.TW" and position=0 then setposition(1);

if position=1 and filled=1 then begin

    if datediff(date, filledRecordDate(FilledRecordCount))>=1 and time=130000 then setposition(0);

end; 

 

第二個的話也是要把條件改為 >= 1

if time=093000 and position=0 then begin

   setposition(1);

   value1=date;

end;

if position=1 and filled=1 then begin

   if datediff(date, value1)>=1 and time=130000 then setposition(0);

end; 

chaoyueh 發文於   2021/04/27

感謝小幫手的回應

發現一個奇怪的狀況

第二個寫法,出場都會是當天,問題在哪呢?  第一種寫法是正確的,都在隔天出場

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

Hello chaoyueh,

 

小幫手認為您應該是選擇1分鐘頻率且勾選了逐筆洗價。

由於洗價時是用tick來模擬,所以進而導致value1 沒有儲存進場日期。

細節請參考 IntrabarPersist 裡的解釋。

所以如果您要使用1分鐘頻率且逐筆洗價的話,建議您將腳本改為

 

var: IntraBarPersist enter_date(0);

if time=093000 and position=0 then begin

   setposition(1);

   enter_date = date;

end;

 

if position=1 and filled=1 then begin

   if datediff(date, enter_date)>=1 and time=130000 then setposition(0);

end;  

 

您可以將原本腳本中的 value1 和新腳本的 enter_date print出來比較差異。

chaoyueh 發文於   2021/04/27

 

謝謝小幫手

 

我再測試看看

發表回覆
Close