實際交易與回測仍差異很大,為何?

  •   109 
  • 最後發表   XQYi  2024 五月 24
XQYi 發文於   2024/05/24

 程式碼在圖下方,何處有誤造成回測與實際差很大

 //%B指標 

input: Lengthb(20, "布林通道天數"); 

input: BandRange(2, "上下寬度"); 

input: bM(80,"擴展"),bs(60,"緊縮"); 

variable: up(0), down(0), mid(0); 

 up = bollingerband(Close, Lengthb, BandRange); 

down = bollingerband(Close, Lengthb, -1 * BandRange); 

if up - down = 0 then value31 = 0 else value31 = (close - down) * 100 / (up - down); 

//ATR  

input: Length(6,"ATR天數"),ATRs(2.25,"ATR除數");  

value1 = Average(TrueRange, Length);  

value2=open-low;  

value3=high-open;  

value4=high-low;  

value5=value2/(value4+0.0001);  

value6=1-value5; 

value7=round(open-open*0.00015-(value1*value5)/ATRs,2);  

condition1=GetField("收盤價","Tick") <= value7;//close  

value8=round(open+open*0.00015+(value1*value6)/ATRs,2);  

condition2=GetField("收盤價","Tick")>= value8;//close  

input:TM(090200,"進場時間"),P1(10,"操作價位L"),P2(500,"超作價位H"),X1(1,">買進有漲%停利"); 

//,X2(25,"<買進有跌%停損"),X3(1,"<賣出有跌%停利"),X4(25,">賣出有漲%停損"); 

condition7=GetSymbolInfo("處置股")=false;// or q_NetValuePerShare >=6; //回測時不可使用 

condition90 = close > p1 and close <P2  and  close < close[1]*1.05 and condition7; //進場點  

var: intrabarpersist SentTime(0); 

var: intrabarpersist hasOrdered(false); 

if  position=0 and filled=0  and condition90 and currentTime > TM and  currentTime <=110000   

then begin  

if  condition1  and (trueAll(value31 > value31[1],2){ or value7 > value7[1] })  

and value7*1.0045 < getField("High","D")  

then 

setposition(minList(position+1,1),GetField("收盤價","Tick"),label:="買1-31");//有庫存時,不進場加買,維持一張  

if  condition1  and ({trueAll(value31 > value31[1],2) or} value7 > value7[1] )  

and value7*1.0045 < getField("High","D")  

then 

setposition(minList(position+1,1),GetField("收盤價","Tick"),label:="買1-7");//有庫存時,不進場加買,維持一張  

//print("時間",Getfield("時間","Tick"),"價格",GetField("收盤價","Tick"),"value31",Value31,"value7",value7); 

end;  

if position<>0 and filled<>0 then 

begin  

value95=100 * (Close - FilledAvgPrice) / FilledAvgPrice; //停利停損%  

if filled=1 then setposition(0,filledAvgPrice*(1+(x1/100)),label:="買進續漲%停利"); 

 SentTime=Getfield("時間","Tick"); 

end; 

input:CT1(10,"排隊時間秒"),CT2(60,"改價時間分");     

if position=0 and filled=1  

and GetField("收盤價","Tick") >=filledAvgPrice*(1+(x1/100)) 

and Getfield("時間","Tick")>=timeadd(SentTime,"S",CT1) 

and addSpread(filledAvgPrice*(1+(x1/100)),0) >=filledAvgPrice*(1+(0.435/100)) 

then setposition(0,addSpread(filledAvgPrice*(1+(x1/100)),0),label:="排隊則調利"); 

if position=0 and filled=1 and Getfield("時間","Tick")>=timeadd(SentTime,"M",CT2)  

then setposition(0,filledAvgPrice*(1+(0.45/100)),label:="未達委託價調利"); 

 

 

XQYi 發文於   2024/05/24

晚上再回測,賣出價的資料又不一樣了,離實際越來越遠,苦惱啊!

發表回覆
Close