自動交易停損

  •   625 
  • 最後發表   Alex嘉  2023 四月 18
Alex嘉 發文於   2023/04/08

想請問一下  能否再自動交易策略中 

同時寫入 做多後  再重新判斷是否 做多還是做空等等,

出場條件要寫 停損50點或是 KDJ 的J 死亡交叉/黃金交叉後就出場呢?

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

Hello Alex嘉,

 

您可以用 position 和 filled 來取得目前策略的部位和庫存,並以此來判斷是要加碼還是減碼,甚至是直接反方向建立部位。

舉例來說:

condition1 = 建立多方部位條件;

condiiton2 = 多方加碼條件;

condition3 = 反手做空條件;

if condition1 and position <= 0 then setposition(1, market) 

    else if condition2 and position > 0 then setposition(position + 1, market) 

    else if condition3 and position >= 0 then setposition(-1, market);

上面這樣寫就會在 condition1 成立時且部位為0或空頭時進場,condition2 成立時加碼,condition3成立時出清多方部位並建立空頭部位。

關於用 position 和 filled 控制交易的說明可以參考 自動交易語法介紹 裡的說明

 

同理出場條件也可以有很多個,只要達成其中一個就出場。

舉例來說:

condition1 = KDJ 死亡交叉;

condition2 = 停損50點;

if condition1 or condition2 then setposition(0, market);

 

停損停利的寫法可以參考內建的交易腳本,KDJ的寫法可以參考內建的指標腳本。

 

網站上有教學區,裡面有XS語法的基礎和應用可以閱覽。

Alex嘉 發文於   2023/04/14

謝謝小幫手,有寫出來了,

但目前的問題有遇到一個是
若我有多空條件進場,但我也想要 多空出場使用移動停損,但寫出後 發現結果好像不太對。

if Position = 0 and filled = 0 and long_condition then begin
    SetPosition(1);             { 買進1張 }
end else 

if Position = 0 and filled = 0 and over_condition then begin
    SetPosition(-1);                { 賣進1張 }
end;

{做多 做空的判斷 ↑ }
{--------------------------------------------------------------------}
if Position = 1 and Filled = 1 then begin
    { 依照成本價格設定停損/停利 ___多}
    var: intrabarpersist stoploss_point(0);
end else 
if Position = -1 and Filled = -1 then begin
    { 依照成本價格設定停損/停利 __空}
   var: intrabarpersist stoploss_points(0);
end;
{設定 停利/停損 的宣告 }

    if Position=1 and stoploss_point = 0 then begin
        stoploss_point = FilledAvgPrice - loss_point;
    { 計算停損價格 }

    if Close > FilledAvgPrice then begin
        if Close - loss_point > stoploss_point then begin
            stoploss_point = Close - loss_point;
        end;    
    end;    

    if profit_point > 0 and Close >= FilledAvgPrice + profit_point then begin
        { 停利 }
        SetPosition(0);
        stoploss_point = 0;
    end else if Close <= stoploss_point then begin
        { 停損 }
        SetPosition(0);
        stoploss_point = 0;
    end;

{ 如果價格上漲的話, 則往上挪動停損價格. 停損價格只會越來越高 }

    end else    { 計算停損價格 }

    if Position=-1 and stoploss_points = 0 then begin
        stoploss_points = FilledAvgPrice + loss_point;
    end;
        if Close < FilledAvgPrice then begin
        if Close + loss_point < stoploss_points then begin
            stoploss_points = Close + loss_point;
        end;    
    end;    

    if profit_point > 0 and Close <= FilledAvgPrice - profit_point then begin
        { 停利 }
        SetPosition(0);
        stoploss_points = 0;
    end else if Close >= stoploss_points then begin
        { 停損 }
        SetPosition(0);
        stoploss_points = 0;
    end;

    { 如果價格下跌的話, 則往下挪動停損價格. 停損價格只會越來越低 }

想問一下,有辦法print  出,進出場的時間點和日期和因為哪個策略達成所以出場的嗎?

XQ小幫手 發文於   2023/04/18

Hello Alex嘉,

 

您可以參考交易函數

FilledRecordDate 和 FilledRecordTime 可以印出進場的日期和時間點。

至於策略名稱雖然在腳本中無法取得,不過印出時可以指定檔案的路徑名稱包含策略名稱。

可參考 file 函數。

發表回覆
Close