input: profit_point(10, "停利(點)");
input: profit_drawback_point(10, "停利回跌(點)");
input: loss_point(20, "停損(點)");
var: long_condition(false); { 進場買進作多 }
var: intrabarpersist _currentbar(0); //紀錄當下的K棒編號
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, 10);
condition1 = currentbar <> _currentbar; //紀錄的K棒編號不等於當下的K棒編號
if Position = 0 and long_condition and condition1 then begin
SetPosition(1);{ 買進1張 }
end;
if Position > 0 and Filled > 0 then begin
var: intrabarpersist max_profit_point(0);{ 啟動停利後最大獲利點 }
if loss_point > 0 and Close <= FilledAvgPrice - loss_point then begin
{ 停損 }
SetPosition(0);
_currentbar = currentbar; //更新紀錄的K棒編號
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);
_currentbar = currentbar; //更新紀錄的K棒編號
max_profit_point = 0;
end else if Close > max_profit_point then begin
{ 移動最大獲利點 }
max_profit_point = Close;
end;
end;
end;
end;
1 評論