做了一個策略 大意是越跌越買 有漲就賣 賣完後以目前價格重新再進
在跑回測時發現 如果有設定委託價 全部賣出時 程式跑到一半沒法繼續執行
但接續在前次被停止執行的當天 當作開始日期 同樣接續前次的最後一天買進價 卻可以多次全部賣出後再買進
這邊可以判斷是SetPosition(0, 指定價格)有BUG
如果SetPosition(0) 沒有指定價格 就可以完整跑完直到回測結束
請問該如何修改SetPosition(0, 指定價格) 使之可以完整回測呢?
測試使用sell(position, 指定價格)也是一樣
使用軟體 XQ全球贏家個人版 版號3.08.05 220425(TW)
使用環境 Windows 10
回測介面設定
執行頻率:日 原始值 每日部位歸零:不勾選
執行商品:3481群創 預先執行筆數:0
第一次回測
開始日期 2021-05-14~2022-05-10
在全部賣出 再買進第一張後停止執行
LOG顯示
Date= 20210514.000000 第一張 委託價= 22.050000
Date= 20210517.000000 買進 委託價= 20.050000
Date= 20210518.000000 全部賣出 委託價= 21.050000
Date= 20210518.000000 第一張 委託價= 20.100000
第二次回測 接續在第一次被停止執行的當天 當作開始日期
開始日期 2021-05-18~2022-05-10
在全部賣出五次後 再買進第一張後停止執行
LOG顯示
Date= 20210518.000000 第一張 委託價= 20.100000
Date= 20210519.000000 全部賣出 委託價= 21.100000
Date= 20210519.000000 第一張 委託價= 21.350000
Date= 20210520.000000 買進 委託價= 21.350000
Date= 20210521.000000 買進 委託價= 21.350000
Date= 20210524.000000 買進 委託價= 20.350000
Date= 20210525.000000 全部賣出 委託價= 22.350000
Date= 20210525.000000 第一張 委託價= 22.350000
Date= 20210526.000000 買進 委託價= 22.350000
Date= 20210527.000000 買進 委託價= 21.350000
Date= 20210528.000000 全部賣出 委託價= 22.350000
Date= 20210528.000000 第一張 委託價= 22.250000
Date= 20210531.000000 買進 委託價= 22.250000
Date= 20210601.000000 全部賣出 委託價= 24.250000
Date= 20210601.000000 第一張 委託價= 24.700000
Date= 20210602.000000 買進 委託價= 24.700000
Date= 20210603.000000 買進 委託價= 23.700000
Date= 20210604.000000 買進 委託價= 23.700000
Date= 20210607.000000 買進 委託價= 23.700000
Date= 20210608.000000 買進 委託價= 23.700000
Date= 20210609.000000 買進 委託價= 23.700000
Date= 20210610.000000 買進 委託價= 22.700000
Date= 20210611.000000 買進 委託價= 21.700000
Date= 20210615.000000 買進 委託價= 21.700000
Date= 20210616.000000 買進 委託價= 20.700000
Date= 20210617.000000 買進 委託價= 19.700000
Date= 20210618.000000 全部賣出 委託價= 21.700000
Date= 20210618.000000 第一張 委託價= 21.100000
完整程式碼如下
var: intrabarpersist lastSellPoint(1);
var: intraBarPersist _time(0);
if Position <= 0
and _time <> DateTime
then
begin
_time = DateTime;
SetPosition(1, close);
Print("Date=", Date, "第一張 委託價=", close);
end;
if Position <> 0
and _time <> DateTime
then
begin
var: idx(0);
value1 = lastSellPoint;
for idx = FilledRecordCount downto lastSellPoint
begin
if FilledRecordBS(idx) < 0 then
begin
value1 = idx + 1;
break;
end;
end;
lastSellPoint = value1;
var: base_deal_price(0);
var: base_deal_grid(0);
base_deal_price = FilledRecordPrice(lastSellPoint);
base_deal_grid = 0;
var: min_deal_price(0);
var: min_deal_grid(0);
min_deal_price = base_deal_price;
for idx = lastSellPoint + 1 to FilledRecordCount
begin
if FilledRecordPrice(idx) <= min_deal_price then
begin
min_deal_price = FilledRecordPrice(idx);
end;
end;
min_deal_grid = base_deal_grid - IntPortion((base_deal_price - min_deal_price) / 1);
var: curr_grid(0);
var: curr_price(0);
curr_grid = base_deal_grid - IntPortion((base_deal_price - Close) / 1);
curr_price = base_deal_price + curr_grid;
value30 = base_deal_price + curr_grid;
value31 = min_deal_grid;
var: grid_diff(0);
grid_diff = curr_grid - value31;
if 1 <= grid_diff
then
begin
SetPosition(0, value30);
//SetPosition(0);
Print("Date=", Date, "全部賣出 委託價=", value30);
end
else
begin
_time = DateTime;
SetPosition(Position + 1, curr_price);
Print("Date=", Date, "買進 委託價=", curr_price);
end;
end;
2 評論