請問交易程式中,能否在使用SetPosition時,未達到需求張數即停止繼續交易,而執行後續程序??

  •   103 
  • 最後發表   新手測試員  2023 十一月 29
新手測試員 發文於   2023/11/26

在預想程式中,想於購買條件成立後開始執行購買動作,但若於幾根K棒後,未完成購買張數動作,即停止,並以目前以購買數量再作後續賣出動作,大概程式為:

////交易

//買進 

variable : order_qty(0);

order_qty = order_cost / AddSpread(close[barsLast(condition10)] , 0) * 10 - 1;

 

if Position = 0 and Filled = 0

and condition10 {買進條件成立}

then SetPosition(order_qty , market , label:="條件成立=買進"); {以市價買入指定數量}

{

if Position <> 0 and Filled <> 0 

and TrueAll(Position <> Filled , 3) 

then SetPosition(Filled);

}

//賣出

if Filled <> 0

and condition15

then SetPosition(Filled / 2 , market , label:="條件成立=賣出一半"); {以市價賣出一半持股}

if Filled <> 0

and condition20

then SetPosition(0 , market , label:="條件成立=賣出全部"); {以市價賣出全部持股}

 

目前測試有問題的地方為

if Position <> 0 and Filled <> 0 

and TrueAll(Position <> Filled , 3) 

then SetPosition(Filled);

目前測試問題是未達到買進目標張數時,不會停止購買動作,造成成交量較少的個股,可能拖至幾天才能達到買進目標張數,此時股價已下跌很多,希望能在開始購買一段時間未買足張數即停止動作.

是否有能給點建議,能解決這個問題,謝謝!!

XQ小幫手 發文於   2023/11/29

 Hello 新手測試員,

 

小幫手會建議您用變數紀錄進場的Bar來判斷,搭配使用 cancelallorder

 

舉例來說:

var: intrabarpersist _barNum(0);

condition1 = 進場條件;

 

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

    setposition(10);

    _barNum = currentbar;

    end;

 

if _barNum - currentbar > 3 and position <> filled then begin 

    cancelallorder();

    _barNum = 0;

    end;

 

這樣進場超過3根Bar後若部位庫存還是不相符的話,就會取消尚未成交的委託。

  • 按讚來自於
  • johnlintw
發表回覆
Close