2024年12月14日 星期六

小道瓊(YM)全時段交易回測篇 [04]

 EasyTrader YM_Test 04

// Public Variable
vars:MP(0),PF(0),PL(0),BuyPrice(0),ShortPrice(0),BasePF(600),BasePL(300) ;
vars:BarPass(5),HLRange(0),WinPoint(0),HBarPos(0),LBarPos(0),ExitH(0),ExitL(0),TimeOK(false),EntryExitCond(false),SelectNo(0) ;
vars:jumpPoint(0) ;
jumpPoint = MinMove/PriceScale ;

Condition98 = 多方交易濾網 ;
Condition99 = 空方交易濾網 ;

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

PF = MinList(PF,2500/BigPointValue) ;
PL = MinList(PL,1000/BigPointValue) ;

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

   
// ************ for week High/Low box *****************
Vars:WeekLong(5),WeekShort(5),WH(0),WL(0),HLWeek(0),WCount(0),WBox33(0),WBox67(0) ;
WH = 近週高點;
WL = 近週低點 ;

// ************ for Day High/Low box *****************
Vars:DayLong(5),DayShort(5),DH(0),DL(0),HLDay(0),DCount(0),DayBox33(0),DayBox67(0) ;
DH = 近日高點 ;
DL = 近日低點 ;

// ************ 彈性指數動態平均線 Vidya *****************
//* Vidya Setup
vars:Vidya1(1),Vidya2(1) ;

Vidya1 = _SS_Vidya(LenA1,FracA) ;
Vidya2 = _SS_Vidya(LenB1,FracB) ;

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

// ************* Time Set Up *************
TimeOK = True ;

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = (EntriesToday(Date)=0 and ExitsToday(Date)=0) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = OpenD(0)+AvgTrueRange(LenA1)*FracA  ;
hortPrice = DL+Range ;

// ********** Main Strategy *********
// ********** Entry Method

   if TimeOK then begin
//多方交易濾網成立 且收盤價位於 NH與AH 之間 則突破近週高點買進做多
   if Condition98 and MP <> -1 and Close < AH and Close > NH then 
      Buy ("LE_CDP5T") next bar at WH stop ;

//空方交易濾網成立 且收盤價位於 NL與AL 之間 則跌破近週低點賣出做空
   if Condition99 and MP <> -1 and Close > AL and Close < NL then 
      SellShort ("SE_CDP5T") next bar at WL stop ;
   end ;

//計算移動平均匯合分數值 MACS
// ******** MACS Entry ***********
vars: Crossup(0),CrossDn(0),LineScore(0),FastLen(0),SlowLenA(0),SlowLenB(0),UpScore(0),DnScore(0),LengthA(1);
  CrossUp = 0 ;
  CrossDn = 0 ;
  LengthA = 1 ;
for LengthA = 1 to 20 Begin
  FastLen = LengthA  ;
  SlowLenA = IntPortion(FastLen * FracA)+1 ;
  SlowLenB = IntPortion(FastLen * FracB)+1 ;
  if Average(Close,FastLen) > {Cross over} Average(Close ,SlowLenA) then Begin
  Crossup = Crossup + 1 ;
UpScore = CrossUp * 5 ;
  end;
  if Average(Close,FastLen) < {Cross under} Average(Close ,SlowLenB) then Begin
  CrossDn = CrossDn + 1 ;
DnScore = CrossDn * 5 ;
  end;
end;

   if EntryExitCond  then begin
// 前筆交易出場後一段時間 且評分分數大於設定值 則收盤價突破設定價位買進作多
      if MP <> 1 and BarsSinceExit(1) > Hbar*0.5 and UpScore > HighBand*4 and Close < BuyPrice then Buy ("LE_MACS") next bar at BuyPrice stop ;

// 前筆交易出場後一段時間 且評分分數小於設定值 則收盤價跌破設定價位賣出作空
      if MP <> -1 and BarsSinceExit(1) > LBar*0.5 and DnScore < LowBand*4 and Close > ShortPrice then  Sellshort ("SE_MACS") next bar at ShortPrice stop ;
   end ;

//基本停利+移動停損出場
// ************* Base Exit *************
if MP <> 0 then WinPoint = MaxPositionProfit/BigPointValue else WinPoint = 0 ;
if Mod(SE01,3) = 0 then begin
   if MP > 0 then begin
      if WinPoint <= 2500 then Sell ("0Trail_PL2_"+NumtoStr(Absvalue(EntryPrice-(HighSinceEntry-MinList((PL+PF)*0.5,PL))),0)) 
         All Contracts next bar at HighSinceEntry-MinList((PL+PF)*0.5,PL) stop    
      else Sell ("0Trail_PL3_"+NumtoStr(Absvalue(winpoint*0.5),0)) 
         All Contracts next bar at HighSinceEntry-winpoint*0.5 stop ;
   end ;
   if MP < 0 then begin
      if WinPoint <= 2500 then BuytoCover ("0Trail_SL2_"+NumtoStr(Absvalue(EntryPrice-(LowSinceEntry+MinList((PL+PF)*0.5,PL))),0)) 
         All Contracts next bar at LowSinceEntry+MinList((PL+PF)*0.5,PL) stop 
      else BuytoCover ("0Trail_SL3_"+NumtoStr(Absvalue(winpoint*0.5),0)) 
         All Contracts next bar at LowSinceEntry+winpoint*0.5 stop ;
   end ;
end ;

//多單部位 虧損超過預期 平倉出場
   if MP > 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -700)
      then Sell ("WrongEntryL6") next bar at Close stop ;

// ************* Rev Fixed Profit *************
//持有多單 獲利後反手做空
   if MP > 0 and BarsSinceEntry >= 1 then 
      Sellshort ("rLX_FP10") next bar at EntryPrice+PF limit ;
//持有空單 獲利後反手做多
   if MP < 0 and BarsSinceEntry >= 1 then 
      Buy ("rSX_FP10") next bar at EntryPrice-PF limit  ;

// Vidya 反手單 

   if MP < 0 and Countif(Vidya1 > Vidya1[1],3) = 3 and Close > NH and Close < BuyPrice then 
      Buy ("rSX_Vidya2") next bar at BuyPrice stop;
   if MP > 0 and Countif(Vidya2 < Vidya2[1],3) = 3 and Close < NL and Close > ShortPrice then 
      SellShort ("rLX_Vidya2") next bar at ShortPrice stop;


小道瓊 留倉 交易週期 2018/1/1 ~ 2024/5/31 交易成本 USD 30





沒有留言:

張貼留言