{
多單停損(%)
}
input: profit_percent(20, "停利(%)");
input: loss_percent(10, "停損(%)");
input: max_holding_days(20, "持倉天數上限"); // ?? 新增由使用者填入持倉天數
var: long_condition(false); { 進場買進作多 }
Vars: holding_days(0);
// 計算持倉天數
if Filled > 0 then
holding_days = DateDiff(FilledRecordDate(1), CurrentDate) + 1;
//holding_days = BarsSinceEntry();
//holding_days = DateDiff(FilledRecordDate(FilledRecordCount), CurrentDate) + 1;
{
範例:
均線穿越時以市價買進1張
以成交價為基礎, 設定固定的停損/停利價格, 觸及時出場
}
if Position = 0 then begin
SetPosition(1, MARKET); { 以市價買進 }
end;
if Position >= 1 and Filled >= 1 then begin
// 依照成本價格設定停損/停利 或 持倉超過天數出場
if holding_days >= max_holding_days then begin
SetPosition(0, label:="持倉滿使用者指定日數出場");
end else
if profit_percent > 0 and Close >= FilledAvgPrice * (1 + 0.01 * profit_percent) then begin
SetPosition(0); // 停利
end else if loss_percent > 0 and Close <= FilledAvgPrice * (1 - 0.01 * loss_percent) then begin
SetPosition(0); // 停損
end;
end;
我這麼寫回測也不會賣.可否幫改一下謝謝!!
5 評論