2024年10月4日 星期五

小S&P500 (ES)全時段交易回測篇 [02]

EasyTrader ES_Test  02
與 小S&P500 (ES) 測試 01 同樣的交易邏輯 只是沒有多空交易時間濾網 進場價格不同 也有不錯的表現

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

//****************** Basic Setup ****************************************
PF = 6000/BigPointValue ;
PL = 1500/BigPointValue ;

//計算多空盈利點數
// ******** Long/Short win/loss point *********
WinPoint_LE = HighSinceEntry_N - EntryPrice ;
WinPoint_SE = EntryPrice - LowSinceEntry_N ;

//計算 Macd 值
// ************ Macd *****************
vars:uDif(0),uMacd(0),uOsc(0),MacdLen(0) ;
MacdLen = MaxList(LenA1,LenB1) ;
if MacdLen > 15 then MacdLen = 15 ;
uDif = Macd((High+Low+Close*2)/4,IntPortion(MacdLen*1.3),IntPortion(MacdLen*2.5)) ;
uMacd = Xaverage(uDif,MacdLen) ;
uOsc = uDif-uMacd ;

//計算 近三週高低區間價
// ************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) ;

//計算 動量值
// MTM
Vars:MomValue(0),MomAvg(0);
MomValue = Momentum(AvgPrice,MaxList(LenA1,LenB1));
MomAvg = XAverage(MomValue,MinList(LenA2,LenB2));

//計算 ADX值
//*********** DMI ADX setup
vars:DMIupA(0),DMIdnA(0),ADX_A(0),DMIupB(0),DMIdnB(0),ADX_B(0) ;
DMIupA = DMIPlus(LenA1) ;
DMIdnA = DMIMinus(LenA1) ;
ADX_A = ADX(LenA1) ;

//計算 彈性指數動態平均線 Vidya
//* Vidya Setup
vars:Vidya1(1),Vidya2(1) ;
Vidya1 = _SS_Vidya(LenA1,FracA) ;
Vidya2 = _SS_Vidya(LenB1,FracB) ;

vars:Day5Range(0) ;
Day5Range = 五日平均振幅 ;

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

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

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Maxlist(OpenD(0),OpenD(1),OpenD(2))+Range*0.5 ;
ShortPrice = OpenW(0)-MaxList(TrueRange*RatioL/10,6*jumpPoint) ;

// ********** Main Strategy *********
// ********** Entry Method
   if TimeOK then begin
// 近三根Vidya值上升 且收盤價 自近週高點拉回一段時間 且在週區間價2/3之上
則突破設定價位 做多
      if MP <> 1 and Countif(Vidya1 > Vidya1[1],3) = 3 and Close > Week67 and WeekHPB > LenB1 and Close < BuyPrice then   Buy ("LE_Vidya7T") next bar at BuyPrice stop;

// 近三根Vidya值下降 且收盤價 自近週低點反彈一段時間 且在週區間價1/3之下
則跌破設定價位 做空
      if MP <> -1 and Countif(Vidya2 < Vidya2[1],3) = 3 and Close < Week33 and WeekLPB > LenB1 and Close > ShortPrice then  SellShort ("SE_Vidya7T") next bar at ShortPrice stop;
   end ;

// ******** Mometum Entry ***********
   if EntryExitCond then begin

// ADX值小於設定值 且 Macd 上穿零軸 且 動量均值大於 0 則市價做多
    if MP <> 1 and ADX_A < NBarL and uMacd Cross over 0 and MomAvg > 0  then
       Buy ("LE_Mom01") next bar at Market ;

// ADX值小於設定值 且 Macd 下穿零軸 且 動量均值小於 0 則市價做空
    if MP <> -1 and ADX_A < NBarS and uMacd Cross under 0 and MomAvg < 0 then
       sellshort ("SE_Mom01") next bar at Market ;
   end;  

//停損及停利
// ************* Base Exit *************
  if MP > 0 then begin
     Sell ("LX_PF5D_"+NumtoStr(PF+Day5RAnge,0)) next bar at EntryPrice+PF+Day5RAnge limit ;
     if WinPoint_LE <= PL*1.5 then Sell ("LX_EPL"+NumtoStr(PL,0)) next bar at EntryPrice-PL stop
        else if WinPoint_LE <= PF then Sell ("LX_H2PL_"+NumtoStr(PL*2,0)) next bar at HighSinceEntry_N-PL*2 stop 
           else if WinPoint_LE > PF then Sell ("LX_PF_"+NumtoStr(WinPoint_LE*0.5,0)) 
           next bar at HighSinceEntry_N-WinPoint_LE*0.5 stop ;
  end ;      

  if MP < 0 then begin
     BuytoCover ("SX_PF5D_"+NumtoStr(PF+Day5RAnge,0)) next bar at EntryPrice-PF-Day5RAnge limit ;
     if WinPoint_SE <= PL*1.5 then BuytoCover ("SX_EPL_"+NumtoStr(PL,0)) next bar at EntryPrice+PL stop 
        else if WinPoint_SE <= PF then BuytoCover ("SX_L2PL_"+NumtoStr(PL*2,0)) next bar at LowSinceEntry_N+PL*2 stop 
           else if WinPoint_SE > PF then BuytoCover ("SX_PF_"+NumtoStr(WinPoint_SE*0.5,0)) 
           next bar at LowSinceEntry_N+WinPoint_SE*0.5 stop ;
  end ;
      
小S&P500 留倉 交易週期 2018/1/1 ~ 2024/6/30 交易成本 USD 70






沒有留言:

張貼留言