2024年3月17日 星期日

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

EasyTrader TestNo 032

參考文章 特別的均線評分策略 - 移動平均匯合

// 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) ;
if MP <> MP[1] and MP > 0 then ExitL = EntryPrice-PL ;
if MP <> MP[1] and MP < 0 then ExitH = EntryPrice+PL ;

//計算自訂 MACD
// ************ Macd *****************
vars:vDif(0),vMacd(0),vOsc(0),uDif(0),uMacd(0),uOsc(0),MacdLen(0) ;
MacdLen = MaxList(LenA1,LenB1) ;
if MacdLen > 12 then MacdLen = 12 ;
uDif = Macd((High+Low+Close*2)/4,IntPortion(MacdLen*1.3),MacdLen*3) ;
uMacd = Xaverage(uDif,MacdLen) ;
uOsc = uDif-uMacd ;

//計算CDP區間價格
// ************ 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) ;

//計算一目均衡表價格
// ************ Ichimoku Sanjin Syytem *****************
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),Thick(100) ;
vars: CondL(false),CondS(false) ;

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) ;
H_Over_L = _BarsLast(HighCloud cross over LowCloud) ;
H_under_L = _BarsLast(HighCloud cross under LowCloud) ;
CondL = Close > HighCloud and HighCloud > LowCloud ;
CondS = Close < HighCloud and HighCloud < LowCloud ;

//判斷多空趨勢
//*********** Trend filter setup
vars:UpTrendNo(1),DnTrendNo(1),UpTrend_High(false),UpTrend_Low(false),DnTrend_High(false),DnTrend_Low(false) ;
vars:Trend4Long(false),Trend4Short(false) ;

UpTrendNo = Mod(LenA1,5)+1 ;
DnTrendNo = Mod(LenB1,5)+1 ;

if UpTrendNo = 3 then Trend4Long = 多方趨勢 ;
if DnTrendNo = 5 then Trend4Short = 空方趨勢 ;

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

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/Exit Condition Set Up *************
SelectNo = IntPortion(RatioL+0.6) ;
if SelectNo < 1 or SelectNo > 5 then SelectNo = 1 ;
if SelectNo = 1 then EntryExitCond = (EntriesToday(Date)=0 and ExitsToday(Date)=0) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = OpenD(1)+Range*0.5 ;
ShortPrice = OpenD(1)-Range*0.5 ;

// ********** Main Strategy *********
// ********** Entry Method
//計算MACS評分
vars:AvgRatio(2.5),Num(0) ;
vars:Crossup(0),CrossDn(0),LineScore(0),FastLen(0),SlowLen(0),UpScore(0),DnScore(0),Length(1);

  CrossUp = 0 ;
  CrossDn = 0 ;
  Length = 1 ;
for Num = 1 to 20 Begin
  FastLen = Length  ;
  SlowLen = IntPortion(FastLen * AvgRatio)+1 ;
  if Average(Close,FastLen) > Average(Close,SlowLen) then Begin
  Crossup = Crossup + 1 ;
UpScore = CrossUp * 5 ;
  end;
  if Average(Close,FastLen) < Average(Close,SlowLen) then Begin
  CrossDn = CrossDn + 1 ;
DnScore = CrossDn * 5 ;
  end;
  Length = Length + 1 ;
end;

   if EntryExitCond then begin

//上漲分數大於設定值 且多方趨勢 收盤價在平盤上突破設定價位做多
      if MP <> 1 and UpScore > HighBand*4 and Trend4Long and Condition1 and Close < BuyPrice then   Buy ("LE_MACS9") next bar at BuyPrice stop;

//下跌分數小於設定值 且空方趨勢 收盤價在平盤下跌破設定價位做空
      if MP <> -1 and DnScore < LowBand*4 and Trend4Short and Condition2 and Close > ShortPrice then  SellShort ("SE_MACS9") next bar at ShortPrice stop;
   end ;

   if TimeOK then begin

//上漲分數大於設定值 且 收盤價在藍色雲層上突破設定價位做多
      if MP <> 1 and UpScore > HighBand*4 and CondL and Close < BuyPrice then 
         Buy ("LE_MACS10T") next bar at BuyPrice stop;

//下跌分數小於設定值 且 收盤價在紅色雲層下跌破設定價位做空
      if MP <> -1 and DnScore < LowBand*4 and CondS and Close > ShortPrice then 
         SellShort ("SE_MACS10T") next bar at ShortPrice stop;
   end ;

//基本停利+移動停損出場
// ************* Base Exit *************
if MP > 0 then Sell ("PL1_"+NumtoStr(PL,0)) next bar at HighSinceEntry-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 LowSinceEntry+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 maxpositionloss/currentcontracts < -20000)
      then Sellshort ("WrongEntryL8") next bar at Close stop ;

//依進場時間長短及移動停損未製作反手單進場
   if WinPoint < PL and BarsSinceEntry < BarPass then begin 
if MP > 0 and Close > (EntryPrice-PL) then SellShort ("rLX_GE0") next bar EntryPrice-PL stop
else if MP < 0 and Close < (EntryPrice+PL) then Buy ("rSX_GE0") next bar EntryPrice+PL stop ; 
   end else 
   if WinPoint < PL and BarsSinceEntry >= BarPass then begin 
if MP > 0  and Close > maxlist(EntryPrice-PL ,Lowest(Low,BarPass)) then 
   SellShort ("rLX_GE1") next bar maxlist(EntryPrice-PL ,Lowest(Low,BarPass)) stop
else if MP < 0 and Close < minlist(EntryPrice+PL ,Highest(High,BarPass)) then 
          Buy ("rSX_GE1") next bar minlist(EntryPrice+PL ,Highest(High,BarPass)) stop ; 
   end ; 

//持有空單 收盤價大於 NL值 且 Dif 及 Osc 值上升 則收盤價突破設定價位反手做多
   if MP < 0 and Close > NL and uDif > 0 and uOsc > uOsc[1] and Close < BuyPrice then 
      Buy ("rSX_CDP1") next bar at BuyPrice stop ;

//持有多單 收盤價小於 NH值 且 Dif 及 Osc 值下降 則收盤價跌破設定價位反手做空
   if MP > 0 and Close < NH and uDif < 0 and uOsc < uOsc[1] and Close > ShortPrice then 
      SellShort ("rLX_CDP1") next bar at ShortPrice 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






沒有留言:

張貼留言