input:
JumpPoint(20, "跳空點數"),
BodyPoint(10, "實體點數"),
ProfitPoint(50, "啟動移動停利點數"),
PullbackPoint(10, "回檔點數");
Vars:
float myEntryPrice(0),
float maxProfit(0),
bool isLong(false),
bool isShort(false),
bool canTrade(false),
int barCountToday(0),
int todayDate(0);
// 僅允許 5 分 K
if BarFreq <> "Min" or BarInterval <> 5 then
RaiseRunTimeError("請使用5分K");
// 每日重新計算 barCountToday
if Date <> todayDate then begin
todayDate = Date;
barCountToday = 1;
end else
barCountToday += 1;
// 交易時間判斷(0845~1345、1500~0500)
canTrade = (Time >= 084500 and Time <= 134500) or (Time >= 150000 or Time <= 050000);
// 第 1 根 K 棒:檢查跳空 + 實體
if barCountToday = 1 and canTrade then begin
if AbsValue(Open - Close) > BodyPoint and AbsValue(Open - Close[1]) > JumpPoint then begin
if Open > Close[1] then begin
isLong = true;
myEntryPrice = Open;
end else if Open < Close[1] then begin
isShort = true;
myEntryPrice = Open;
end;
end;
end;
// 第 2 根 K 棒:開盤進場
if position = 0 and canTrade and barCountToday = 2 then begin
if isLong then
SetPosition(1, Market)
else if isShort then
SetPosition(-1, Market);
end;
// 移動停利與基準價反向出場邏輯
if position <> 0 then begin
if position = 1 then
maxProfit = MaxList(maxProfit, Close - myEntryPrice);
if position = -1 then
maxProfit = MaxList(maxProfit, myEntryPrice - Close);
// 啟動移動停利
if maxProfit >= ProfitPoint then begin
if position = 1 and Close <= myEntryPrice + maxProfit - PullbackPoint then
SetPosition(0, Market);
if position = -1 and Close >= myEntryPrice - maxProfit + PullbackPoint then
SetPosition(0, Market);
end;
// 基準價反向突破出場
if position = 1 and Close < myEntryPrice then
SetPosition(0, Market);
if position = -1 and Close > myEntryPrice then
SetPosition(0, Market);
end;
// 出場後重置
if position = 0 then begin
isLong = false;
isShort = false;
myEntryPrice = 0;
maxProfit = 0;
end;
2 評論