2023年2月17日 星期五

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

EasyTrader TestNo 006

// Public Variable
vars:MP(0),PF(0),PL(0),DayLast(1345),NightLast(0500),BuyPrice(0),ShortPrice(0),BasePF(150),BasePL(100),FilterA(0),FilterB(0) ;
vars:BarPass(5),HLRange(0),WinPoint(0),HBarPos(0),LBarPos(0),ExitH(0),ExitL(0),TimeOK(false),EntryExitCond(false),SelectNo(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) ;

// 計算近期高低點位置
// ************ High/Low data *****************
BarPass = MaxList(HBar,LBar) ;
HLRange = Highest(High,BarPass)-Lowest(Low,BarPass) ;
if MP <> 0 then WinPoint = MaxPositionProfit/BigPointValue else WinPoint = 0 ;
HBarPos = HighestBar(High,HBar) ;
LBarPos = LowestBar(Low,LBar) ;

// ************ CDP *****************
vars:CDP(0),AH(0),NH(0),NL(0),AL(0),CDPrange(0),CrossA(false),CrossB(false),TH1(0),TL1(0),TH2(0),TL2(0) ;
if Date <> Date[1] then begin
CDP = (HighD(1)+LowD(1)+2*CloseD(1))/4;
AH = CDP + (HighD(1) - LowD(1));
NH = CDP*2 - LowD(1);
NL = 2*CDP - HighD(1);
AL = CDP - (HighD(1) - LowD(1));
end ;
CDPrange = HighD(1) - LowD(1) ;

if BarNumber = 1 then begin
Buy this bar on Close ;
Sell this bar on Close ;
end ;
d;

// ************* Time Set Up *************
TimeOK = ((time >= 0900 and time <= 1200) or (time >= 2130) or time <= 0300) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = (Highest(High,Hbar)+Lowest(Low,Hbar))*0.5 ;
ShortPrice = HighD(0)+MaxList(TrueRange*RatioS/10,6) ;

// ********** Main Strategy *********
{ R-Break System }

vars:f1(0.4),f2(0.1),f3(0.1),xdiv(4);
var:SellshortCheck(0),BuyCheck(0),RevSellshort(0),RevBuy(0),TrendBuy(0),TrendSellshort(0),
DayLow(0),DayHigh(99999),startnow(0),div(0),Rangefilter(false);

f1 = FracA/6 ;
f2 = FracB/6 ;
f3 = RatioL/6 ;
xdiv = RatioS ;

div=maxlist(xdiv,1) ;

//計算 R-Break 系統的四個價位
{ Calculate Ref Price box }
SellshortCheck=HighD(1)+f1*(CloseD(1)-LowD(1));
RevSellshort=((1+f2)/2)*(HighD(1)+CloseD(1))-(f2)*LowD(1);
RevBuy=((1+f2)/2)*(LowD(1)+CloseD(1))-(f2)*HighD(1);
BuyCheck=LowD(1)-f1*(HighD(1)-CloseD(1));
TrendBuy=SellshortCheck+f3*(SellshortCheck-BuyCheck);
TrendSellshort=BuyCheck-f3*(SellshortCheck-BuyCheck);
DayHigh = High;
DayLow = Low;
Rangefilter=HighD(1)-LowD(1)>=MaxList((Highband+LowBand)*div,80);
end;

if TimeOK then begin
if MP = 0 then Begin
if Close > TrendBuy then Buy ("Break LE1T") next bar at High stop;
if Close < TrendSellshort then Sellshort ("Break SE1T") next bar at Low stop;
end;
end ;

//基本停利停損出場
// ************* Base Exit *************
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 ;

// ************* Rev Lower Low & Higher High Exit *************
vars:BaseBar(5),var01(0),var02(0),Var03(0),Var04(0) ;
if ExtNum01 = 12 or ExtNum02 = 12 then begin
if EntriesToday(Date) = 0 then Begin
if MP > 0 then Begin
var01 = (LowD(1)+LowD(0))*0.5-AvgTrueRange(BaseBar)*1.618 ;
var02 = LowSinceEntry ;

//持有多單,當收盤價小於均線,跌破最近兩日最低價均值一段距離後則反手做空
if Close < xAverage(Close,LenA2) and Close > var01 then
SellShort ("rLX_HL1") next bar at var01 stop ;

{ if HBarPos<=BaseBar and Countif(Low<=Low[1],BaseBar)> BaseBar*0.5 and Close > var02 then
SellShort ("rLX_HL2") next bar at var02 stop ; }
end ;

if MP < 0 then Begin
var03 = (HighD(1)+HighD(0))*0.5+AvgTrueRange(BaseBar)*1.618 ;
var04 = HighSinceEntry ;

//持有空單,當收盤價大於均線,突破最近兩日最高價均值一段距離後則反手做多
if Close > xAverage(Close,LenB2) and Close < Var03 then
Buy ("rSX_HL1") next bar at Var03 stop ;

//持有空單,近低點反彈連續過高,反手做多  
if LBarPos<=BaseBar and Countif(High>=High[1],BaseBar)> BaseBar*0.5 and Close < Var04 then
Buy ("rSX_HL2") next bar at Var04 stop ;
end ;
end ;
end ;

//CDP 高點回落/低點反彈出場
// ************* CDP exit *************
//當CDP範圍大於一定範圍且收盤價大於CDP上限值 則再跌落上限後平倉    
if MP > 0 and CDPrange > MaxList(89,PF*0.382) and Close > AH then
Sell ("LX_CDP") next bar at AH-Minlist(Range,HBarPos) stop ;

//當CDP範圍大於一定範圍且收盤價小於CDP下限值 則再反彈過下限後平倉 
if MP < 0 and CDPrange > MaxList(89,PF*0.382) and Close < AL then
BuytoCover ("SX_CDP") next bar at AL+Minlist(Range,LBarPos) 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;
台指期  留倉 交易週期 2018/1/1 ~ 2022/12/31 交易成本 1200





沒有留言:

張貼留言