Converted a MQL4 code to MQL5, and need opinions

 

I converted this code for an EA I saw online from MQL4 to MQL5, Can someone help me check for errors?

//+------------------------------------------------------------------+

input int    min_gapsize = 1;
input double lotsize_gap = 0.1;
//----
datetime order_time = 0;

int OnInit()
  {
//----
   return(0);
  }

void OnDeinit(const int reason)
  {
   return(0);
  }

int OnStart()
  {

   Print("order time ", order_time);
double current_openprice = iOpen(Symbol(), 15, 0); 
double previous_highprice = iHigh(Symbol(), 15, 1); 
double previous_lowprice = iLow(Symbol(), 15, 1); 
double point_gap = SymbolInfoDouble(Symbol(), SYMBOL_POINT);
int spread_gap = SymbolInfoInteger(Symbol(), SYMBOL_SPREAD);
datetime current_time = iTime(Symbol(), 15, 0); 

   if(current_openprice > previous_highprice + (min_gapsize + spread_gap)*point_gap &&
      current_time != order_time)
     {
       double lotsize_gap = 0.1; 
int ticket = OrderSend(Symbol(), OP_SELL, lotsize_gap, Bid, 0, 0,
                      previous_highprice + spread_gap * point_gap,
                      "", 0, clrNONE);
order_time = iTime(Symbol(), 15, 0); 
Print("I am inside (sell) :-)", order_time);
       
      if (ticket < 0)
{
    int error_code = GetLastError();
    string error_message = ErrorDescription(error_code);
    Print("OrderSend failed with error #", error_code, ": ", error_message);
}
     }

   if (current_openprice < previous_lowprice - (min_gapsize + spread_gap) * point_gap &&
    current_time != order_time)
{
    ticket = OrderSend(Symbol(), OP_BUY, lotsize_gap, Ask, 0, 0,
                      previous_lowprice - spread_gap * point_gap,
                      "", 0, clrNONE);
    order_time = iTime(Symbol(), 15, 0); 
    Print("I am inside (buy) :-)", order_time);
    
    if (ticket < 0)
    {
        int error_code = GetLastError();
        string error_message = ErrorDescription(error_code);
        Print("OrderSend failed with error #", error_code, ": ", error_message);
    }
}
//----
   return(0);
  }
//+------------------------------------------------------------------+

Is there something that is wrong Or things I am supposed to add to it or remove from it?

 

Is this a "Script", "Service" or an "Expert Advisor (EA)"?

Scripts and Services don't use OnInit nor OnDeinit, while EAs don't use the OnStart. You should not mix them.

Assuming that the original MQL4 program was an EA and was using the "init", "deinit" and "start" event handlers, then you should be using the newer OnTick event handler for MQL5 and MQL4+.

Your OrderSend function is also incorrect. Please read the documentation as it works completely differently in MQL5.

PS! I have moved your topic to the section: Expert Advisors and Automated Trading —  https://www.mql5.com/en/forum/172166/page6#comment_49114893

 

I also suggest you read the following, as MT5 trading functionality is completely different to that of MT4.

Articles

Orders, Positions and Deals in MetaTrader 5

MetaQuotes, 2011.02.01 16:13

Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
 
Fernando Carreiro #:

Is this a "Script", "Service" or an "Expert Advisor (EA)"?

Scripts and Services don't use OnInit nor OnDeinit, while EAs don't use the OnStart. You should not mix them.

Assuming that the original MQL4 program was an EA and was using the "init", "deinit" and "start" event handlers, then you should be using the newer OnTick event handler for MQL5 and MQL4+.

Your OrderSend function is also incorrect. Please read the documentation as it works completely differently in MQL5.

It is an EA, I have done the needful based on your suggestions, I will read more on how to use OrderSend and the OnTick. Also on mql5

Thank you

 
peinjo12:

I converted this code for an EA I saw online from MQL4 to MQL5, Can someone help me check for errors?

Is there something that is wrong Or things I am supposed to add to it or remove from it.

I fixed your code. Removed some redundant lines AND it compiles now. Make sure to test that logic has not been tampered with. Goodluck :D

#include <Trade\Trade.mqh> //Instatiate Trades Execution Library

//---
CTrade         trade;
input int    min_gapsize = 1;
input double lotsize_gap = 0.1;
input ENUM_TIMEFRAMES period = PERIOD_M15; //Timeframe
//----
datetime order_time = 0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {

   Print("order time ", order_time);
   double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
   double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   double current_openprice = iOpen(Symbol(), period, 0);
   double previous_highprice = iHigh(Symbol(), period, 1);
   double previous_lowprice = iLow(Symbol(), period, 1);
   double point_gap = SymbolInfoDouble(Symbol(), SYMBOL_POINT);
   long spread_gap = SymbolInfoInteger(Symbol(), SYMBOL_SPREAD);
   datetime current_time = iTime(Symbol(), period, 0);

   if(current_openprice > previous_highprice + (min_gapsize + spread_gap)*point_gap &&
      current_time != order_time)
     {

      trade.Sell(lotsize_gap,_Symbol,Bid,0,previous_highprice + spread_gap * point_gap,NULL);
      order_time = iTime(Symbol(), period, 0);
      Print("I am inside (sell) :-)", order_time);
     }

   if(current_openprice < previous_lowprice - (min_gapsize + spread_gap) * point_gap &&
      current_time != order_time)
     {
      trade.Buy(lotsize_gap,Symbol(),Ask,0,previous_lowprice - spread_gap * point_gap,NULL);
      order_time = iTime(Symbol(), period, 0);
      Print("I am inside (buy) :-)", order_time);
     }
  }

//+------------------------------------------------------------------+