今天在做自動交易回測時發現一個問題,
SetPosition = 0 多單想出場的時候,
掛低 負1個 tick , 甚至 負5個 tick 都無法順利賣出成交,
(用 market 可順利出場)
結果發現 Close 和 AddSpread(Close, -1) 算出的差距很大!!!
以 5474聰泰為例,2021/04/14 09:00 那根1分K:
Close = 160.35
AddSpread(Close, -1) = 180.00
Close計算負1Tick後,價格竟比 Close 高出不少!!!
不確定是單一案例,還是剛好5474聰泰的資料有錯,
再麻煩小幫手協助查詢了
-------------------------------------
以下是我的回測畫面、結果、程式碼:
使用選股腳本, 裡面放一隻5474聰泰

日K和還原K的結果比較:

交易腳本程式碼:
SetTotalBar(1);
SetBackBar(1);
var:
NowDate(0), NowTime(0), NowDTStr(""),
intrabarpersist MyEntryDate(0), intrabarpersist MyLeaveDate(0);
NowDate = date; //currentDate
NowTime = currentTime; //time
NowDTStr = Text(datetostring(NowDate)," ",timetoString(NowTime));
print(Text(NowDTStr,", Type:Init",", Price:",Close,
", position=", numtoStr(position,0), ", filled=", numtoStr(filled,0)) );
if position = 0 and filled = 0 then
begin
if MyEntryDate = NowDate or MyLeaveDate = NowDate then return;
if NowDate = 20210318 then
begin
MyEntryDate = NowDate;
setposition(2, AddSpread(Close, 1));
print(Text(NowDTStr,", Type:●進場",", Price:",Close,
", position=", numtoStr(position,0), ", filled=", numtoStr(filled,0)) );
end;
end
else if filled > 0 then //有庫存
begin
if NowDate = 20210414 then
begin
MyLeaveDate = NowDate;
setposition(0, AddSpread(Close, -1));
//setposition(0, market); //用market可順利成交
print(Text(NowDTStr,", Type:●出場",", Price:",Close,
", position=", numtoStr(position,0), ", filled=", numtoStr(filled,0)) );
print(close, AddSpread(Close, -1));
end;
end;
3 評論