|
台指期全時段交易回測篇 [1]
測試以2018/01/01~2021/12/31的資料做為樣本內測試資料,2022/01/01~2022/12/31的資料做為樣本外的驗證,測試的方式將以部落格內舊文章的策略進場元素搭配一些策略產生器的出場或反手單元素做回測,當然按照慣例只會放預設參數,重點還是讓讀者自行做發想測試
PS:因為是搭配策略產生器運作,程式碼內有些沒用到的參數就仍保留著!
// Public Variable
vars:MP(0),PF(0),PL(0),DayLast(1345),NightLast(0500),BuyPrice(0),ShortPrice(0),BasePF(150),BasePL(100) ;
vars:BarPass(5),HLRange(0),WinPoint(0),HBarPos(0),LBarPos(0),ExitH(0),ExitL(0),TimeOK(false) ;
vars:EntNum01(2.0),EntNum02(0.0),ExtNum01(31.0),ExtNum02(50.0),LenA1(20.0),LenB1(20.0),FracA(2.50),FracB(2.50),HBar(20.0),LBar(20.0),TradeProfit(0.020),TradeStopLoss(0.020),NBarL(20.0),NBarS(20.0),LenA2(20.0),LenB2(20.0),RatioL(2.50),RatioS(2.50),HighBand(20.0),LowBand(20.0),BuyMode(41.0),ShortMode(21.0),LE01(4.0),SE01(12.0) ;
//****************** Basic Setup ****************************************
MP = MarketPosition ;
if MP <> 0 then Begin
PF = EntryPrice*TradeProfit ;
PL = EntryPrice*TradeStopLoss ;
end else begin
PF = AvgPrice*TradeProfit ;
PL = AvgPrice*TradeStopLoss ;
end ;
// 依據目前指數來設定最大停利與停損點數
if Close > 10000 then begin
BasePF = BasePF+IntPortion((Close-10000)/1000)*30 ;
BasePF = MinList(BasePF,450) ;
BasePL = BasePL+IntPortion((Close-10000)/1000)*15 ;
BasePL = MinList(BasePL,225) ;
end ;
PF = MinList(PF,BasePF) ;
PL = MinList(PL,BasePL)*iff(CurrentContracts=1,1,0.67) ;
// ************3 Week High or Low *****************
vars:WeekH(High),WeekL(Low),WeekHL(0),WeekHPB(0),WeekLPB(0),Week33(0),Week67(0) ;
WeekH = MaxList(HighW(0),HighW(1),HighW(2)) ;
WeekL = MinList(LowW(0),LowW(1),LowW(2)) ;
WeekHL = WeekH - WeekL ;
Week33 = WeekL+WeekHL*0.33 ;
Week67 = WeekL+WeekHL*0.67 ;
WeekHPB = _BarsLast(High = WeekH) ;
WeekLPB = _BarsLast(Low = WeekL) ;
if DataCompression > 1 then begin
Condition1 = Close > Open and Open > Close[1] ;
Condition2 = Close < Open and Open < Close[1] ;
end else begin
Condition1 = Close > OpenD(0) and OpenD(0) > CloseD(1) ;
Condition2 = Close < OpenD(0) and OpenD(0) < CloseD(1) ;
end;
// ************* Time Set Up *************
if LE01 = 4 then TimeOK = ((time >= 0900 and time <= 1200) or (time >= 2130) or time <= 0300) ;
// ********** Entry Method
Vars:OpenA(0),CloseA(0),OpenB(0),CloseB(0),NewClose(0),AVGTyp(0),AVGClose(0),LineCorss(-1),AvgMaxClose(0) ;
if CurrentBar > 1 then
OpenA = (( Open[1] + High[1] + Low[1] + Close[1])/4 + OpenA[1])/2
else OpenA = Open ;
CloseA = ((Open + High + Low + Close)/4 + OpenA + MaxList(High,OpenA) + Minlist(Low,OpenA ))/4 ;
CloseB = (Close + Open + High + Low )/4 ;
if CurrentBar > 1 then OpenB = (OpenB[1]+CloseB[1])/2 else OpenB = Open ;
NewClose = (CloseA+CloseB)/2 ;
{ calculate averages of Typical Price and NewClose }
AVGTyp = XAverage( TypicalPrice,LenA1) ;
AVGClose = XAverage( NewClose, LenB1 ) ;
if AVGTyp < AVGClose and Close > Open then LineCorss = 1
else if AVGTyp > AVGClose and Close < Open then LineCorss = 0 ;
// 進場改用前週高低點做濾網
if EntNum01 = 2 or EntNum02 = 2 then begin
if TimeOK then begin
if MP <> 1 and Close > HighW(1) and LineCorss = 1 and LineCorss[1] = 0
then Buy ( "MQS01_L1T" ) next bar at market ;
if MP <> -1 and Close < LowW(1) and LineCorss = 0 and LineCorss[1] = 1
then SellShort ( "MQS01_S1T" ) next bar at market ;
end ;
end ;
// ************* Base Exit *************
if MP <> 0 and BarsSinceEntry >= 1 then begin
if MP > 0 then Sell ("PL1_"+NumtoStr(PL,0)) next bar at EntryPrice-PL stop ;
if MP > 0 then Sell ("PF1_"+NumtoStr(PF,0)) next bar at EntryPrice+PF limit ;
if MP < 0 then BuytoCover ("PL2_"+NumtoStr(PL,0)) next bar at EntryPrice+PL stop ;
if MP < 0 then BuytoCover ("PF2_"+NumtoStr(PF,0)) next bar at EntryPrice-PF limit ;
end ;
if SE01 = 12 then begin
if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then buy ("WrongEntryS8") next bar at Close stop ;
end ;
// ************* no more high or low *************
if ExtNum01 = 31 or ExtNum02 = 31 then begin
// 近六根高低點區間大於100且多單連續破低 則平倉出場
if MP > 0 and countif(Low < Low[1],6) >= 6 and Highest(High,6)-Lowest(Low,6) > 100 then
Sell ("LX_6Low") next bar at Lowest(Low,LBar) stop;
// 近六根高低點區間大於100且空單連續過高 則平倉出場
if MP < 0 and countif(High > High[1],6) >= 6 and Highest(High,6)-Lowest(Low,6) > 100 then
BuytoCover ("SX_6High") next bar at Highest(High,HBar) stop;
end ;
// ************* Rev H/L PullBack turn *************
if ExtNum01 = 50 or ExtNum02 = 50 then begin
//近三週高點拉回 且破近三週低點 反手做空
if MP > 0 and WeekHPB < LenA1 and Close > WeekL then
SellShort ("rLX_WeekHPB") next bar at WeekL-Range stop;
//近三週低點反彈 且過近三週高點 反手做多
if MP < 0 and WeekLPB < LenB1 and Close < WeekH then
Buy ("rSX_WeekLPB") next bar at WeekH+Range stop;
end ;
//結算日出場
if _IsSettlementDay and time >= CalcTime(1300,-1*BarInterVal) and Time <= 1330 then begin
if MP > 0 then Sell ("LX_Bal") next bar at market ;
if MP < 0 then BuyToCover ("SX_Bal") next bar at market ;
end;
|
|