我用XS內建的網格交易跑回測,不太明白它的進場點位是怎麼來的,
以台股指數近月(一般)(FITX*1.TF) 2025/7/3 單日為例,
以1分K回測, 網格中心點為8:45 1分K收盤價 22558,
第1次賣出為8:51, 應該是上1分鐘觸發條件了,但是價格22619是怎麼來的?
另外想請問如果要每日平倉所有部位, 且每日開盤重新開始計算網格中心點, 可以怎麼改? 謝謝
附上貼圖,回測檔,程式碼
程式碼(XS內建):
input: grid_gap(20, "每格點數");
input: grid_maxcount(3, "最多格數");
input: stoploss_point(100, "停損(點)");
{
範例:
策略一啟動就以當時收盤價為基礎啟動網格交易, 一直跑到停損點觸發為止
}
var: intrabarpersist grid_started(false); { 開始網格交易 }
var: intrabarpersist grid_base(0); { 網格中心點: 如果不是0的話表示已經啟動 }
var: intrabarpersist grid_current_base(0); { 目前的網格中心點: 依照價格移動 }
var: intrabarpersist grid_current_ord(0); { 目前的網格的編號, 正中央=0, 往上=1, 2, 3, 往下=-1,-2,-3}
var: intrabarpersist grid_buycount(0); { 進入網格後的買進數量合計 }
var: intrabarpersist grid_sellcount(0); { 進入網格後的賣出數量合計, 用buycount/sellcount可以估算目前損益(每個買賣賺一個grid) }
if not grid_started and GetInfo("TradeMode") = 1 then begin
grid_started = true;
grid_base = Close;
grid_current_base = Close;
grid_current_ord = 0;
grid_buycount = 0;
grid_sellcount = 0;
Print("=>啟動網格中心點:", numtostr(grid_current_base, 0));
end;
{ 網格交易邏輯 }
if grid_base <> 0 then begin
if Close >= grid_base + stoploss_point or Close <= grid_base - stoploss_point then begin
SetPosition(0, label:="網格:停損出場"); { 全部平倉, 停止網格交易(TODO:請填入委託價格) }
grid_base = 0; { 停止網格交易 }
end else begin
{
比對目前價格與current_grid_base, 看價格是否穿越網格跳過幾格
請注意以下的邏輯有處理一次跳過多格的情形
}
if Close >= grid_current_base + grid_gap then begin
value1 = grid_current_ord + IntPortion((Close - grid_current_base) / grid_gap);
if value1 >= grid_maxcount then value1 = grid_maxcount;
value1 = value1 - grid_current_ord; { 往上移動的格數 }
if value1 > 0 then begin
{ 往上移動網格 }
grid_current_base = grid_current_base + value1 * grid_gap;
grid_current_ord = grid_current_ord + value1;
grid_sellcount = grid_sellcount + value1;
SetPosition(Position - value1, label:="網格:上漲賣出"); { 賣出 (TODO:請填入委託價格) }
end;
end else if Close <= grid_current_base - grid_gap then begin
value1 = grid_current_ord - IntPortion((grid_current_base - Close) / grid_gap);
if value1 <= -1 * grid_maxcount then value1 = -1 * grid_maxcount;
value1 = grid_current_ord - value1; { 往下移動的格數 }
if value1 > 0 then begin
{ 往下移動網格 }
grid_current_base = grid_current_base - value1 * grid_gap;
grid_current_ord = grid_current_ord - value1;
grid_buycount = grid_buycount + value1;
SetPosition(Position + value1, label:="網格:下跌買進"); { 買進 (TODO:請填入委託價格) }
end;
end;
end;
end;
1 評論