// 策略參數
inputs:
Length(9, "計算期數"),
loss_percent(2, "止損(%)"),
profit_percent(6, "止盈(%)"),
startTime(091500, "開始計算時間");
// 變數
vars: intrabarpersist
highestPriceToday(0),
long_condition(false), // 是否做多
myEntryPrice(0),
takeProfitPrice(0),
stopLossPrice(0);
// 初始化最高價
if date <> date[1] then begin
highestPriceToday = 0;
end;
// 計算當天的最高價從09:30開始
if Time >= startTime then begin
if High > highestPriceToday then
highestPriceToday = High;
end;
// 做多條件:當前價格高於當天前9根K棒高點
long_condition = Time >= startTime and High > Highest(High, Length)[1];
// 當Position=0且符合做多條件時進場
if Position = 0 and long_condition then begin
SetPosition(1, MARKET);
myEntryPrice = Close; // 設置進場價格
takeProfitPrice = myEntryPrice * (1 + profit_percent / 100); // 設置止盈價格
stopLossPrice = myEntryPrice * (1 - loss_percent / 100); // 設置止損價格
long_condition = false; // 重置做多條件
end;
// 當Position=1且多單已經買進1口
if Position = 1 and Filled = 1 then begin
// 檢查止盈條件
if High >= takeProfitPrice then begin
SetPosition(0, takeProfitPrice); // 以止盈價出場
end;
// 計算損益百分比
vars: intrabarpersist plratio(0);
// 不管Filled是大於0還是小於0, FilledAvgPrice的數值都是'正數'(>0)
plratio = 100 * (Close - FilledAvgPrice) / FilledAvgPrice;
// 止損條件:損益百分比小於等於 -loss_percent
if plratio <= -loss_percent then begin
SetPosition(0, MARKET); // 以市價出場
end;
end; 想請問為啥我的出場都沒有按照邏輯走,進場有該怎麼調整,我是在做自動交易中心的回測
2 評論