移動停損 改成入場兩口以後 不會停損也不會停利

  •   84 
  • 最後發表   swer  2022 一月 13
swer 發文於   2022/01/10

 

{

多單移動停損(點)

 

設定停損點, 跟停利點(如果不設定停利的話請把profit_point設定成0)

價格碰觸到停損/停利點時出場

如果價格上漲時, 停損點會跟著上漲

}

 

input: profit_point(0, "停利(點)");

input: loss_point(10, "停損(點)");

 

var: long_condition(false); { 進場買進作多 }

 

 

}

 

if loss_point = 0 then raiseruntimeerror("請設定停損(點)");

 

long_condition = Average(Close, 5) cross over Average(Close, 10);

 

if Position = 0 and long_condition then begin

SetPosition(2);{ 買進2張 }

end;

 

if Position = 1 and Filled = 1 then begin

{ 依照成本價格設定停損/停利 }

var: intrabarpersist stoploss_point(0);

 

{ 計算停損價格 }

if stoploss_point = 0 then begin

stoploss_point = FilledAvgPrice - loss_point;

end;

 

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

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;

 

XQ小幫手 發文於   2022/01/13

Hello swer,

 

這是因為您的停損停利執行條件為 if Position = 1 and Filled = 1 then begin,也就是說,在您的部位和庫存為1的時候會執行。

但您進場的部位是2,所以不會執行停損停利。

您可以修改為 if Position = 2 and Filled = 2 then begin  //庫存部位為2時執行

或是 if Position > 0 and Filled > 0 then begin  //庫存部位為多方時執行

小幫手建議您先看一下教學區內的文章,裡面有XS語法的基礎和應用。

發表回覆
Close