2023年1月10日 星期二

台指期全時段交易回測篇 [1]

EasyTrader TestNo 001
許久沒在部落格寫文章了,近期在增加策略產生器的進出場邏輯元素的過程中,將早期部落格內的策略內容也再做了複習,主要也是台指期自2017/5/17開始全時段交易至今也超過5年了對於策略回測所需的歷史數據也走過了一個大的多空循環的週期
~盤整緩漲(2018/01~2020/01)
~空頭急跌(2020/01~2020/03)
~多頭趨勢(2020/03~2021/05)
~區間盤整(2021/05~2022/01)
~空頭趨勢(2022/01~2022/09)
~大區間盤整(2022/09~2023/01)
這對於策略的歷史回測也比較能夠反映對於盤勢的適應性,後續的測試將會以2018/01/01~2021/12/31的資料做為樣本內測試資料,2022/01/01~2022/12/31的資料做為樣本外的驗證,測試的方式將以部落格內舊文章的策略進場元素搭配一些策略產生器的出場或反手單元素做回測,當然按照慣例只會放預設參數,重點還是讓讀者自行做發想測試
PS:因為是搭配策略產生器運作,程式碼內有些沒用到的參數就仍保留著!

參考文章 紅買綠賣的交易策略 - 平均K線

// 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) ;


//****************** 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 *************
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 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 ;


// ************* 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 MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then buy ("WrongEntryS8") next bar at Close stop ;

// ************* no more high or low *************

// 近六根高低點區間大於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;


// ************* Rev H/L PullBack turn *************

//近三週高點拉回 且破近三週低點 反手做空
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;

//結算日出場
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;

台指期 11 分K 留倉 交易週期 2018/1/1 ~ 2022/12/31 交易成本 1200








沒有留言:

張貼留言