小老師你好,目前使用交易腳本想寫出固定金額的回測方式但選出的股票容易在同一天賣出



input: ordersize_w(25, "每筆交易金額(萬)");
input: StopLoss(5, "停損百分比");
input: TakeProfit(10, "停利百分比");
input: HoldDays(20, "持有天數");
input:PercentUp(1,"上漲百分比");
var: _entrydate(0);
var: _entryPrice(0);
var: _entryBar(0);
//定義條件
condition1 = Close > Close[1] * (1+PercentUp/100);////上漲1以上
// 進場條件
if condition1 and Position = 0 and (_entrydate = 0 or dateDiff(date, _entrydate) > 20) then begin
Setposition(floor(ordersize_w * 18 / c));
_entrydate = date; // 記錄進場日期
_entryPrice = c; // 記錄進場價格
_entryBar = CurrentBar; // 記錄進場時的 Bar 編號
end;
// 平倉條件
if Position >= 1 and Filled >= 1 then begin
// 停損條件:當目前收盤價 <= 進場價格 * (1 - 停損百分比) 時觸發停損
if Close <= _entryPrice * (1 - StopLoss / 100) then
setposition(0, market, label:="停損");
// 停利條件:當目前收盤價 >= 進場價格 * (1 + 停利百分比) 時觸發停利
if Close >= _entryPrice * (1 + TakeProfit / 100) then
setposition(0, market, label:="停利");
// 持有時間到期條件:當當前的 Bar 超過進場的持有天數時觸發平倉
if CurrentBar >= _entryBar + HoldDays then
setposition(0, market, label:="持有時間到");
end;
1 評論