2025年6月11日 星期三

MT5 EA交易策略開發教學 [08]

這支 當沖 EA 運用了不同指標背離作為進出場的條件 在今年表現得相當突出亮眼

多單進場方式 KD 低檔背離   掛單突破進場做多
空單進場方式 ~ KD 高檔背離   掛單突破進場做空


#include <Trade\Trade.mqh>
#include <MagicMT5_函數庫V1.mqh>
ENUM_TIMEFRAMES  時間週期 = PERIOD_M30 ;
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棒();
//時間濾網
      允許交易時段 = (getTM_hour(TimeCurrent()) >= 8 ); 
     }
   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)
     {
      //+------------------------------------------------------------------+
      //|多單進場 KD 低檔背離
      //+------------------------------------------------------------------+
      LE_Cond = (KD_LDiv_01()) ;

      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
      //+------------------------------------------------------------------+
      //|空單進場  KD 高檔背離
      //+------------------------------------------------------------------+
      SE_Cond = (KD_HDiv_01())  ;
      if(SE_Cond == true)
        {
         if(多單部位() == 0 && 空單部位() == 0 && BarNumber != OrderBarNo)
           {
            賣出價格 = NormalizeDouble((OpenD[1]+OpenD[2])*0.5,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(當沖 == true && (getTM_hour(TimeCurrent()) == 當沖出場時間 || getTM_hour(TimeCurrent()) == 1))
     {
      if(多單部位() > 0 || 空單部位() > 0)
        {
         當沖平倉();
        }
     }

   if(多單部位() > 0)
     {
      //+------------------------------------------------------------------+
      //|    Buy Exit Method  多單  以MACD 高檔背離及固定停損平倉
      //+------------------------------------------------------------------+
      多單進場價格 = LE_EntryPrice(MagicNumber,多單進場單號);
      多單停利價格 = NormalizeDouble(多單進場價格 + TP * Point(),Digits()) ;
      多單停損價格 = NormalizeDouble(多單進場價格 - SL * Point(),Digits()) ;
          LX_Cond = ((MACD_HDiv_02() == true) || (Close[1] >= 多單停損價格 && Bid < 多單停損價格)) ;


      //---------------------------------------------------------多單出場
      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  空單 以RSI 低檔背離及固定停損平倉
      //+------------------------------------------------------------------+
      空單進場價格 = SE_EntryPrice(MagicNumber,空單進場單號);
      空單停利價格 = NormalizeDouble(空單進場價格 - TP * Point(),Digits()) ;
      空單停損價格 = NormalizeDouble(空單進場價格 + SL * Point(),Digits()) ;
       SX_Cond = ( (RSI_LDiv_02() == true) || (Close[1] <= 空單停損價格 && Ask > 空單停損價格)) ;

      //---------------------------------------------------------空單出場
      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(); //保留

   set_KD() ;

  }

//+------------------------------------------------------------------+
//|                                                                 
//+------------------------------------------------------------------+
void 當沖平倉()
  {
   if((getTM_hour(TimeCurrent()) == 當沖出場時間 || getTM_hour(TimeCurrent()) == 1))
     {
      if(多單部位() > 0 && BarNumber != CloseOrderNo)
         LX_CloseByTicket(多單進場單號,Lots) ;
      if(空單部位() > 0 && BarNumber != CloseOrderNo)
         SX_CloseByTicket(空單進場單號,Lots) ;
      CloseOrderNo = iBarShift(Symbol(),時間週期,LoadEA) ;
     }
  }


交易商品 XAUUSD 樣本內區間 2019/1/1 ~2022/12/31 交易手數 固定 0.1手
當沖



沒有留言:

張貼留言