各位先進 想請教 :

A.使用內建範例的程式交易-08-多單移動停利(點)

B.因為程式裡的定義下會反覆一直交易:

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

C.所以想要增設 --> 可設定"到達獲利次數"或是"到達停損次數"時 而停止交易

D.是不是 增加下列的程式  就可以達到目的 ? 增加在範例程式哪個位置? 或是需要修改那些 ?

input: order_qty(1, "每次下單數量");

input: order_bs(1, "買賣方向", inputKind:=Dict(["買進", 1]));

input: order_count(10, "下單筆數");

 

if not exec_order_started and GetInfo("TradeMode") = 1 then begin

exec_order_started = true;

exec_order_count = 0;

exec_order_lasttime = 0;

end;

 

 

   

 

 

 

{

多單移動停利(點)

 

設定停損點(如果不設定的話, 請把loss_point設定成0), 以及停利點, 跟回跌點數

價格下跌到停損時出場

價格上漲到停利點後啟動移動停利, 如果價格繼續上漲, 則繼續持有, 如果價格回檔超過回跌點數時, 則停利出場

}

 

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

input: profit_drawback_point(5, "停利回跌(點)");

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

 

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

 

範例:

 

均線穿越時買進1張

以成交價為基礎, 設定固定停損以及移動停利

}

 

if profit_point = 0 then raiseruntimeerror("請設定停利(點)");

if profit_drawback_point = 0 then raiseruntimeerror("請設定停利回跌(點)");

if profit_drawback_point > profit_point then raiseruntimeerror("停利(點)需大於停利回跌(點)");

 

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

 

if Position = 0 and long_condition then begin

SetPosition(1); { 買進1張 }

end;

 

if Position = 1 and Filled = 1 then begin

var: intrabarpersist max_profit_point(0); { 啟動停利後最大獲利點 }

 

if loss_point > 0 and Close <= FilledAvgPrice - loss_point then begin

{ 停損 }

SetPosition(0);

max_profit_point = 0;

 

end else begin

 

{ 判斷是否要啟動停利 }

if max_profit_point = 0 and Close >= FilledAvgPrice + profit_point then begin

max_profit_point = Close;

end;

 

if max_profit_point <> 0 then begin

if Close <= max_profit_point - profit_drawback_point then begin

{ 停利 }

SetPosition(0);

max_profit_point = 0;

end else if Close > max_profit_point then begin

{ 移動最大獲利點 }

max_profit_point = Close;

end;

end;

end;

 

end;