2023年4月1日 星期六

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

EasyTrader TestNo 010

參考文章 國外知名交易系統 一目均衡表交易模型
// 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) ;

Vars:WeekLong(5),WeekShort(5),WH(0),WL(0),HLWeek(0),WCount(0) ;

//計算近幾週高低點
{ for week High/Low box }
WH = 近幾週高點 ;
WL = 近幾週低點 ;

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

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = True ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Maxlist(HighD(0),HighD(1),HighD(2))-Range*0.5 ;
ShortPrice = WL ;

// ********** Main Strategy *********
//計算一目均衡表各個價位

Vars: ShortTerm(9),MidTerm(26),LongTerm(52);
Vars: ConvertLine(0),BaseLine(0),AheadLine1(0),AheadLine2(0),BehindLine(0),HighCloud(0),LowCloud(0),Cloud(0) ;
Vars: BaseCond(false),ConvertCond(false),C_over_B(0),C_Under_B(0),H_Over_L(0),H_Under_L(0) ;

ShortTerm = LenA1 ;
if ShortTerm <= 8 then ShortTerm = 8
else if ShortTerm >= 20 then ShortTerm = 20 ;
MidTerm = ShortTerm*3 ;
LongTerm = ShortTerm*6 ;

ConvertLine = (highest(high,ShortTerm) + Lowest(Low,ShortTerm))/2 ;
BaseLine = (highest(high,MidTerm) + Lowest(Low,MidTerm))/2 ;
AheadLine1 = (ConvertLine + BaseLine)/2 ;
AheadLine2 = (highest(high,LongTerm) + Lowest(Low,LongTerm))/2 ;
BehindLine = Close ;
C_over_B = _BarsLast(ConvertLine cross over BaseLine) ;
C_under_B = _BarsLast(ConvertLine cross under BaseLine) ;

HighCloud = AheadLine1[MidTerm] ;
LowCloud = AheadLine2[MidTerm] ;
Cloud = AbsValue(HighCloud-LowCloud) ;
BaseCond = BaseLine >= MinList(HighCloud,LowCloud) and BaseLine <= MaxList(HighCloud,LowCloud) ;
ConvertCond = ConvertLine >= MinList(HighCloud,LowCloud) and ConvertLine <= MaxList(HighCloud,LowCloud) ;

if EntryExitCond then begin

//當轉換線大於基準線且收盤價突破設定價格做多
if MP <> 1 and ConvertLine > BaseLine and Close < BuyPrice then
Buy ("MQS15_LE1") next bar at BuyPrice Stop ;

//當轉換線小於基準線且收盤價跌破設定價格做空
if MP <> -1 and ConvertLine < BaseLine and Close > ShortPrice then
SellShort ("MQS15_SE1") next bar at ShortPrice Stop ;
end ;


if EntryExitCond then begin

//當收盤價同時突破雲層上下限 則在這根K棒高點上做多
if MP <> 1 and Close Cross over MinList(HighCloud,LowCloud) and Close Cross over MaxList(HighCloud,LowCloud) then
Buy ("MQS15_LE10") next bar at High Stop ;

//當收盤價同時跌破雲層上下限 則在這根K棒低點下做空
if MP <> -1 and Close Cross under MinList(HighCloud,LowCloud) and Close Cross under MaxList(HighCloud,LowCloud) then
SellShort ("MQS15_SE10") next bar at Low Stop ;
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 ;

//進場後部位獲利小於預期則平倉出場或虧損大於預期則做反手單
if MP > 0 and (BarsSinceEntry >= 300/Barinterval and MaxPositionprofit/currentcontracts < 5000)
then buytocover ("WrongEntryS15") next bar at Close stop ;
if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then buy ("WrongEntryS16") next bar at Close 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





沒有留言:

張貼留言