當天只進場一次程式寫法

  •   308 
  • 最後發表   KevinT  2023 十月 03
KevinT 發文於   2023/09/24

Hi 小幫手您好,

我想在股票期貨當天快收盤前(13:35之後),如果漲幅4%以上及5MA之上進場,並且到收盤前只進場一次

我應該如何修改下面的程式碼?

另外收盤前跌破日線的5MA出場下面的程式碼執行結果不如預期,應該如何修改?

if currenttime>133500 

   and Close>CloseD(1)*1.04 

   and Close>average(CloseD(0),5)

 

then setposition(1,market);

 

if currenttime>133500 

   and Close < average(OpenD(0),5)

 

then setposition(0,market);

XQ小幫手 發文於   2023/10/03

 Hello KevinT,

 

建議您可以使用 print 函數將相關數值印出,就可以看出問題原因。

 

closed(0) 和 opend(0) 的前一期 closed(0)[1] / opend(0)[1] 會是前一根Bar的 closed(0) / opend(0),而非前一日的開盤收盤價。

所以如果執行的不是日頻率的話, average(closed(0), 5) 計算出的不會是5日平均。

要計算5日平均的話應該用 average(getfield("Close", "D"), 5)。

 

如果您只希望進場一次的話,可以用變數來控制。

舉例來說:

var: intrabarpersist _count(0);

 

if issessionfirstbar then _count = 0;    //每日重置

 

f currenttime>133500 and Close>CloseD(1)*1.04 and Close>average(getfield("Close", "D"),5) and position = 0 and filled = 0 and _count = 0 then begin

    setposition(1,market);

    if getinfo("TradeMode") = 1 then _count += 1;

    end;

 

這樣在當日交易過後 _count 就會變成1因此不再進場。

發表回覆
Close