web-dev-qa-db-ja.com

MQL4 EAをMQL5に変換する

MQL4コードをMQL5に変更する方法を理解しようとしています。これまでのところ、RSIとMACDの条件とSendOrder()を変更することができましたが、ModifyOrder()とCloseOrder()のように、それを完了するために実行できなかった他のものの中にたくさんあります。私はこれが一口であることを知っていますが、これを完了するためのいくつかの助けに本当に感謝します。

これは元のMQL4コードです:

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);
}

これが私がこれまでに行ったことです(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);
     }


         }
   }

   }

}

前もって感謝します。

5
BBNN

はい、時間がかかる場合があります。幸運なことに、誰かがあなたが 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`

このlibはヘッジモードでのみ機能するため、MT5にはヘッジモードが必要であることに注意してください。

以下の完全な例:

#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;
}
0
Daniel Kniaz