持有天數出場

  •   666 
  • 最後發表   Golly  2024 十月 22
Golly 發文於   2023/09/03

請問在交易語法中
有沒有辦法寫出指定天數出場
例如依照進場條件買進台積電之後
無論如何20個交易日之後就是強制出場呢

排序方式: 標準 | 最新
XQ小幫手 發文於   2023/09/12

Hello Golly,

 

如果是回測的話,您可以在進場的時候將變數歸0,並在每過一個交易日時讓其增加1。

舉例來說,在非逐筆洗價的狀況下:

if date <> date[1] then value1 += 1;

condition1 = 進場條件;

if condition1 and position = 0 and filled = 0 then begin

    value1 = 0;

    setposition(1, market);

    end;

 

if position = 1 and filled = 1 and value1 >= 20 then setposition(0, market);

這樣 value1 就會記錄經過幾日,並在持有部位且滿20日的狀況下出場。

 

即時的話因為每次關閉策略時相關計算就會重置,所以除非您維持20個工作日不關XQ,或是將 策略部位 設定為依腳本計算並將 策略部位計算起點 設為當初進場的日期,這樣才有辦法模擬這段區間的變數計算。

石頭 發文於   2024/10/16

請教如果是日K回測,逐筆洗價,上方程式碼要怎麼修改?感謝

XS小編 發文於   2024/10/22

Hello 石頭,

 

您可以改使用 IntrabarPersist 所宣告的變數來紀錄進場日期並比較。

舉例來說:

 

var: intrabarpersist _rec(0);

 

condition1 = 進場條件;

 

if position = 0 and filled = 0 and condition1 then begin

    setposition(1, market);

    _rec = date;

    end;

 

if position = 1 and filled = 1 and date[20] >= _rec then setposition(0, market);

 

這樣 _rec 就會是進場的日期,date[20] >= _rec 則會比較20根Bar前的日期是否大於等於進場日期 (相當於過了20日)。

石頭 發文於   2024/10/22

感謝小編。我再試試

發表回覆
Close