2025年7月13日 星期日

MT5 外匯商品 交易策略開發 [10]

這支 EA 運用了價格排列的方式進出,比較特別的是 勝率很高 77% 用多次的小盈利 來獲取長期的正報酬

多單進場方式 (Low[2] >= High[4] && Close[1] >= High[4])   掛單突破進場做多
空單進場方式 ~ (High[2] <= Low[4] && Close[1] <= Low[4])   掛單突破進場做空

#include <Trade\Trade.mqh>
#include <MagicMT5_函數庫V1.mqh>
ENUM_TIMEFRAMES  時間週期 = PERIOD_M10 ;
int OnInit()
  {
   LoadEA = TimeCurrent();
   return(INIT_SUCCEEDED);
  }
void OnTick()
  {
   if(AccountInfoDouble(ACCOUNT_BALANCE) <= 資金風控)
     {
      Alert("**********  資金不足 *************");
      return;
     }
   BarNumber = iBarShift(Symbol(),時間週期,LoadEA);
   BarSinceExit = BarNumber-CloseOrderNo ;
   if((BarNumber == 1 && BarNumber != JudgeNo))
     {
      多單進場單號 = Buy_at_MARKET(Symbol(),Lots,0,0,"1st_K",MagicNumber) ;
      LX_CloseByTicket(多單進場單號,Lots) ;
      CloseOrderNo =  iBarShift(Symbol(),時間週期,LoadEA);
     }
   if(BarNumber != JudgeNo)
     {
      換K棒();
//時間濾網
      允許交易時段 =  true ;

     }
   Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
   AccountBalance=AccountInfoDouble(ACCOUNT_BALANCE);
   Tickvalue=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);
   SP = NormalizeDouble(MathAbs(Ask-Bid),Digits()) ;
//+------------------------------------------------------------------+
//|計算交易手數
//+------------------------------------------------------------------+
   if(Lots_AutoCal == true)
     {
      Lots = get_dynamic_lot_size(Lots_Even,Symbol(),RiskPercent,AccountBalance,SL) ;
      Lots = MathMin(0.3,MathMax(0.01,Lots)) ;
     }
   if(允許交易時段 == true)
     {
      //+------------------------------------------------------------------+
      //|多單進場 近3根K最高的最高價突破
      //+------------------------------------------------------------------+
      LE_Cond =(Low[2] >= High[4] && Close[1] >= High[4]) ;

      if(LE_Cond == true)
        {
         if(多單部位() == 0 && 空單部位() == 0 && BarNumber != OrderBarNo)
           {
            買入價格 = NormalizeDouble(MathMax(High[1],MathMax(High[2],High[3])),Digits()) ;

//距離上筆出場經過一根K棒 且當日未進場    
            if(BarSinceExit > 1 && EntriesToday(MagicNumber,Symbol()) < 1)
              {
               // 空手+掛單突破買入
                   if(Close[1] < 買入價格)
                    {
                     多單進場單號 = Buy_at_STOP(Symbol(),MagicNumber,買入價格,Lots,TP,SL,"BUY STOP",3600) ;
                     OrderBarNo = iBarShift(Symbol(),時間週期,LoadEA);
                    }
              } // end of BarSinceExit > 1
           } // end of 空手且不同根K棒() == true
        } // end of LE_Cond == true
      //+------------------------------------------------------------------+
      //|空單進場  近3根K最低的最高價跌破
      //+------------------------------------------------------------------+
      SE_Cond = (High[2] <= Low[4] && Close[1] <= Low[4]) ;
      if(SE_Cond == true)
        {
         if(多單部位() == 0 && 空單部位() == 0 && BarNumber != OrderBarNo)
           {
            賣出價格 = NormalizeDouble(MathMin(High[1],MathMin(High[2],High[3])),Digits());
//距離上筆出場經過一根K棒 且當日未進場 
            if(BarSinceExit > 1 && EntriesToday(MagicNumber,Symbol()) < 1)
              {
                  // 空手+掛單突破賣出
                  if(Close[1] > 賣出價格)
                    {
                     空單進場單號 = Short_at_STOP(Symbol(),MagicNumber,賣出價格,Lots,TP,SL,"Short STOP",3600) ;
                     OrderBarNo = iBarShift(Symbol(),時間週期,LoadEA);
                    }             
              } //end of BarSinceExit > 1
           } // end of 空手且不同根K棒() == true
        } // end of SE_Cond == true
     } //end of 允許交易時段 == true

   if(多單部位() > 0)
     {
      //+------------------------------------------------------------------+
      //|    Buy Exit Method  多單  做空條件出現則平倉
      //+------------------------------------------------------------------+
      double 多單最小停利 = 0.0;
      bool LX_MinPF = false ;
      多單進場價格 = LE_EntryPrice(MagicNumber,多單進場單號);
      多單最小停利 = NormalizeDouble(多單進場價格 + SP*3,Digits()) ;
      LX_MinPF = Bid > 多單最小停利 ;

      LX_Cond = (LX_MinPF == true && SE_Cond) ;

      //---------------------------------------------------------多單出場
      if(LX_Cond == true && BarNumber != CloseOrderNo && BarNumber != OrderBarNo && LE_BarsSinceEntry(MagicNumber,多單進場單號,時間週期) > 0)
        {
         LX_CloseByTicket(多單進場單號,Lots) ;
         if(多單部位() == 0)
           {
            CloseOrderNo = iBarShift(Symbol(),時間週期,LoadEA) ;
           }
        }
     } // end of 多單部位() > 0
   if(空單部位() > 0)
     {
      //+------------------------------------------------------------------+
      //|    Short Exit Method  空單 做多條件出現則平倉
      //+------------------------------------------------------------------+
      double 空單最小停利 = 0.0;
      bool SX_MinPF = false ;
      空單進場價格 = SE_EntryPrice(MagicNumber,空單進場單號);
      空單最小停利 = NormalizeDouble(空單進場價格 - SP*3,Digits()) ;
      SX_MinPF = Ask < 空單最小停利 ;

       SX_Cond = (SX_MinPF == true && LE_Cond) ;

      //---------------------------------------------------------空單出場
      if(SX_Cond == true && BarNumber != CloseOrderNo && SE_BarsSinceEntry(MagicNumber,空單進場單號,時間週期) > 0)
        {
         SX_CloseByTicket(空單進場單號,Lots) ;
         if(空單部位() == 0)
           {
            CloseOrderNo = iBarShift(Symbol(),時間週期,LoadEA) ;
           }
        }
     } // end of 空單部位() > 0

   JudgeNo = iBarShift(Symbol(),時間週期,LoadEA);
  } //end of onTick

//+------------------------------------------------------------------+
//|        自訂函數庫                                                              |
//+------------------------------------------------------------------+
int 多單部位()
  {
   int count;
   count = get_TradeCounts(Symbol(), MagicNumber,POSITION_TYPE_BUY) ;
   return count ;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int 空單部位()
  {
   int count;
   count = get_TradeCounts(Symbol(), MagicNumber,POSITION_TYPE_SELL) ;
   return count ;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void 換K棒()
  {
// 新K棒刪除所有掛單
   if(total_pending_order_count(Symbol(), MagicNumber,-1) != 0)
     {
      delete_pending_orders_all(Symbol(), MagicNumber, -1, 0x0000ff);
     }
   Set_OHLC_Bar_Series(); //保留
   Set_OHLC_Day_Series(); //保留
   Get_OHLC_Bar(10) ; //保留
   Get_OHLC_Day(10) ; //保留
   set_BarInfo(); //保留

  }
//+--------------------------------------------------------+
//| 設定 開高低收 陣列序列
//+--------------------------------------------------------+
//********BarPrice Array variables
double Open[], High[], Low[], Close[],Range[],Body[],UPshadow[],DNshadow[] ;
double OpenD[], HighD[], LowD[], CloseD[] ;
//+------------------------------------------------------------------+
//|                                                             
//+------------------------------------------------------------------+
void Set_OHLC_Bar_Series()
  {
   ArraySetAsSeries(Open,true);
   ArraySetAsSeries(High,true);
   ArraySetAsSeries(Low,true);
   ArraySetAsSeries(Close,true);
  }
//+------------------------------------------------------------------+
//|                                                               
//+------------------------------------------------------------------+
void Get_OHLC_Bar(int argCount)
  {
   get_OpenData(Symbol(),時間週期,argCount,Open) ;
   get_HighData(Symbol(),時間週期,argCount,High) ;
   get_LowData(Symbol(),時間週期,argCount,Low) ;
   get_CloseData(Symbol(),時間週期,argCount,Close) ;
  }
//+------------------------------------------------------------------+
//|                                                                 
//+------------------------------------------------------------------+
void set_BarInfo()
  {
   ArrayResize(Range,ArraySize(Open)) ;
   ArraySetAsSeries(Range,true) ;
   ArrayResize(Body,ArraySize(Open)) ;
   ArraySetAsSeries(Body,true) ;
   ArrayResize(UPshadow,ArraySize(Open)) ;
   ArraySetAsSeries(UPshadow,true) ;
   ArrayResize(DNshadow,ArraySize(Open)) ;
   ArraySetAsSeries(DNshadow,true) ;
   for(int i = 0; i < ArraySize(Open)-1; i=i+1)
     {
      Range[i] = High[i]-Low[i];
      Body[i] = MathAbs(Close[i]-Open[i]);
      UPshadow[i] = High[i]-MathMax(Close[i],Open[i]);
      DNshadow[i] = MathMin(Close[i],Open[i])-Low[i];
     }
  }
//+------------------------------------------------------------------+
//|                                                             
//+------------------------------------------------------------------+
void Set_OHLC_Day_Series()
  {
   ArraySetAsSeries(OpenD,true);
   ArraySetAsSeries(HighD,true);
   ArraySetAsSeries(LowD,true);
   ArraySetAsSeries(CloseD,true);
  }
//+------------------------------------------------------------------+
//|                                                           
//+------------------------------------------------------------------+
void Get_OHLC_Day(int argCount)
  {
   get_OpenData(Symbol(),PERIOD_D1,argCount,OpenD) ;
   get_HighData(Symbol(),PERIOD_D1,argCount,HighD) ;
   get_LowData(Symbol(),PERIOD_D1,argCount,LowD) ;
   get_CloseData(Symbol(),PERIOD_D1,argCount,CloseD) ;
  }

交易商品 XAUUSD 樣本內區間 2019/1/1 ~2023/6/30 交易手數 固定 0.1手

波段





沒有留言:

張貼留言