Converting mql4 EA to mql5

对着背影说爱祢 提交于 2020-04-07 03:20:28

问题


I have been trying to figure out how to change my MQL4 code to MQL5. So far I've been able to change the RSI and MACD conditions and SendOrder() but there's a lot, like the ModifyOrder() and CloseOrder() amongst other stuff that I've not been able to do to complete it. I know this is a mouthful but I would really appreciate some help in completing this.

This is the original MQL4 code:

extern int MagicNumber=112223;
extern double Lots =0.005;
extern double StopLoss=0;
extern double TakeProfit=0;
extern int TrailingStop=0;
extern int Slippage=3;

int mode_main = 0;
int mode_signal = 1;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start()
{
  double MyPoint=_Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;

  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()==0 ) 
  {
     int result=0;
     if((iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,mode_signal,0)<iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,mode_main,0))&&(iRSI(NULL,PERIOD_M5,2,PRICE_CLOSE,0)>84)) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"EA",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if((iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,mode_main,0)<iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,mode_signal,0))&&(iRSI(NULL,PERIOD_M5,2,PRICE_CLOSE,0)<16)) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA",MagicNumber,0,Red);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  }

  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
              if((iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,mode_signal,0)>iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,mode_main,0))&&(iRSI(NULL,PERIOD_M5,2,PRICE_OPEN,0)<16)) //here is your close buy rule

              {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
              }
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
                if((iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,mode_main,0)>iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,mode_signal,0))&&(iRSI(NULL,PERIOD_M5,2,PRICE_OPEN,0)>84)) // here is your close sell rule
                {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                }
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}

Here is what I've done so far (MQL5):

#include <indicators/indicators.mqh>

CIndicators g_indicators;
CiRSI       *g_rsi;
CiMACD      *g_macd;


input int MagicNumber=112223;
input double Lots =0.005;
input double StopLoss=0;
input double TakeProfit=0;
input int TrailingStop=0;
input int Slippage=3;



int OnInit() {
   g_rsi = new CiRSI();
   g_indicators.Add(g_rsi);
   g_macd = new CiMACD();
   g_indicators.Add(g_macd);
   bool is_init = g_rsi.Create(_Symbol, PERIOD_M5, 2, PRICE_CLOSE);
   is_init &= g_macd.Create(_Symbol, PERIOD_M5, 12, 26, 9, PRICE_CLOSE);
   return is_init ? INIT_SUCCEEDED : INIT_FAILED;
}

void OnTick() {
   g_indicators.Refresh();

   double MyPoint=_Point;
  if(_Digits==3 || _Digits==5) MyPoint=_Point*10;

  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()==0 ) 
  {
     int result=0;
   if (g_macd.Main(0) > g_macd.Signal(0) && g_rsi.Main(0) > 84) { // Here is your open Buy rule
      Print("Signal!");

       MqlTradeRequest request;
            MqlTradeResult  result;
            MqlTradeCheckResult check;
            ZeroMemory(request);
            ZeroMemory(result);
            ZeroMemory(check);

            double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); // Get the Ask Price

            //--- parameters of request
            request.action   =TRADE_ACTION_PENDING;                     // type of trade operation
            request.symbol   =Symbol();                              // symbol
            request.volume   =Lots;                                   // volume of 0.005 lot
            request.type     =ORDER_TYPE_BUY;                        // order type
            request.price    = Ask; // price for opening
            request.deviation=0;                                     // allowed deviation from the price
            request.magic    =magicnumber;                          // MagicNumber of the order
            request.tp       = 0;
            request.sl       = 0;
            request.type_filling=ORDER_FILLING_RETURN; 
            request.type_time=0;
            request.expiration=0;
            ObjectSetString(0,name,OBJPROP_TEXT,string(result.order));

             if(!OrderSend(request,result))
     {
      Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",result.retcode);
     }


         }

   }
   if (g_macd.Main(0) < g_macd.Signal(0) && g_rsi.Main(0) < 16) { // Here is your open Sell rule
      Print("Signal!");

       MqlTradeRequest request;
            MqlTradeResult  result;
            MqlTradeCheckResult check;
            ZeroMemory(request);
            ZeroMemory(result);
            ZeroMemory(check);


            double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); // Get the Bid Price

            //--- parameters of request
            request.action   =TRADE_ACTION_PENDING;                     // type of trade operation
            request.symbol   =Symbol();                              // symbol
            request.volume   =Lots;                                   // volume of 0.005 lot
            request.type     =ORDER_TYPE_SELL;                        // order type
            request.price    = Bid; // price for opening
            request.deviation=0;                                     // allowed deviation from the price
            request.magic    =magicnumber;                          // MagicNumber of the order
            request.tp       = 0;
            request.sl       = 0;
            request.type_filling=ORDER_FILLING_RETURN; 
            request.type_time=0;
            request.expiration=0;
            ObjectSetString(0,name,OBJPROP_TEXT,string(result.order));

             if(!OrderSend(request,result))
     {
      Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",result.retcode);
     }


         }
   }

   }

}

Thanks in advance.


回答1:


Yes, it may take long. luckily, someone wrote a library that you are welcome to download from https://www.mql5.com/ru/code/16006.

#include <MT4Orders.mqh> // if you have #include <Trade/Trade.mqh> in your MQL5 files, include this one AFTER.
int ticket = OrderSend(...);
same for `OrderModify()` and `OrderDelete` \ `OrderModify`

Keep in mind that this lib works for hedging mode only, so your MT5 must have a hedging mode.

full example below:

#include <MT45/MT4Orders.mqh> 
//better to use MQL5 analogues of Ask etc for both MQL4 and MQL5, or redefine them
#define Ask SymbolInfoDouble(_Symbol,SYMBOL_ASK)
#define Bid SymbolInfoDouble(_Symbol,SYMBOL_BID)
#define Point _Point
#define Digits _Digits
// your inputs
extern int MagicNumber=112223;
extern double Lots =0.005;
extern double StopLoss=0;
extern double TakeProfit=0;
extern int TrailingStop=0;
extern int Slippage=3;
//int mode_main = 0;    //probably throw this away
//int mode_signal = 1;  //probably throw this away
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   //initialize your indicators here
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   //deinitialize your indicators here if you need to avoid memory leak
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
 double MyPoint=_Point;
 if(Digits==3 || Digits==5) MyPoint=Point*10;

 double TheStopLoss=0;
 double TheTakeProfit=0;
 if( TotalOrdersCount()==0 ) 
 {
    TICKET_TYPE result=0;
    if(isRule4Buy()) // Here is your open buy rule
    {
       result=OrderSend(Symbol(),OP_BUY,Lots,SymbolInfoDouble(_Symbol,SYMBOL_ASK),Slippage,0,0,"EA",MagicNumber,0,clrBlue);
       if(result>0)
       {
        TheStopLoss=0;
        TheTakeProfit=0;
        if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
        if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
        OrderSelect(result,SELECT_BY_TICKET);
        OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
       }
       return;
    }
    if(isRule4Sell())
    {
     //simply copy the sell block
    }
 }
 for(int cnt=0;cnt<OrdersTotal();cnt++)
 {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
     if(OrderType()<=OP_SELL &&   
        OrderSymbol()==Symbol() &&
        OrderMagicNumber()==MagicNumber 
        )  
     {
        if(OrderType()==OP_BUY)  
          {
           if(isRule4Sell())
             {
              OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
             }
           if(TrailingStop>0)  
             {                 
              if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                {
                 if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                   {
                    OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                    return;
                   }
                }
             }
          }
        else
          {
           //simply copy the sell block
          }
     }
  }
}
//+------------------------------------------------------------------+
int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;
   }
  return (result);
}
//+------------------------------------------------------------------+
bool isRule4Buy()
{
   #ifdef __MQL4__
      return iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0)<iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,0)
           && iRSI(NULL,PERIOD_M5,2,PRICE_CLOSE,0)>84;
   #else 
      return true;
      //replace with MT5
      //g_macd.Main(0) > g_macd.Signal(0) && g_rsi.Main(0) > 84
   #endif 
}
bool isRule4Sell()
{
   //similar for sell
   return false;
}


来源:https://stackoverflow.com/questions/60933737/converting-mql4-ea-to-mql5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!