Inconsistent EA

 

Hi,

I'm a beginner in mql4 and currently practicing in creating a forex bot. I created this simple bot with the strategy of convergence of the 10/20 day EMA, I have no problem in compiling it but when it comes to using it on a demo account, I had the ff issues:

1. sometimes my EA takes a trade, and sometimes it doesn't, even though requirements to take a trade were met.

Scenario A. EA doesn't take the trade even though the requirements in the chart were met
Scenario B. if the EA does take the trade, and got stopped out, once another trade requirement was met, it does not take it.


2. stop loss and TP sometimes won't update.

Same scenario occurs with my first issue, sometimes it updates the SL and sometimes it don't.


3. During back testing, I immediately got an error " zero divide in 'FUNCTIONS COMPILATION 10_19_22.mqh' (154,79)". I don't understand this error, because I was using the same exact include file on a different strategy, and it works just fine, but in this strategy that I created gives out the said error.

Please see EA code below and include file attached.


#property version   "1.00"
#property strict
#property show_inputs
#include  <FUNCTIONS COMPILATION 10_19_22.mqh>

int magicNB = 55555;
input int bbPeriod = 50;
input int bandStdProfitExit = 6;
input double riskPerTrade = 0.02;
int openOrderID = OrderTicket();

input int MA_Period_1 = 10;
input int MA_Period_2 = 20;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   Alert("");
   Alert("Starting Strategy MA");

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Alert("Stopping Strategy MA");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  

   double bbLowerProfitExit = iBands(NULL,0,bbPeriod,bandStdProfitExit,0,PRICE_CLOSE,MODE_LOWER,0);
   double bbUpperProfitExit = iBands(NULL,0,bbPeriod,bandStdProfitExit,0,PRICE_CLOSE,MODE_UPPER,0);
   double MA_1 = iMA (NULL,0,MA_Period_1,0, MODE_EMA, PRICE_CLOSE,0);       // 10 day MA
   double MA_2 = iMA (NULL,0,MA_Period_2,0, MODE_EMA, PRICE_CLOSE,0);          // 20 day MA
   double PDL = MathAbs(Ask - MA_1);         // price difference between ask/bid and 10 day MA as stop loss
   double PDS = MathAbs(Bid - MA_1);            // price difference between ask/bid and 10 day MA as stop loss  
   
   if(!CheckIfOpenOrdersByMagicNB(magicNB))//if no open orders try to enter new position
   {
      if(Ask > MA_1 > MA_2 && PDL < 0.0004)  //long

      {
               Print("Price is above the two MA's, Sending buy order");
               double stopLossPrice = NormalizeDouble(MA_1,Digits);
               double takeProfitPrice = NormalizeDouble(bbUpperProfitExit,Digits); 
               Print("Entry Price = " + Ask);
               Print("Stop Loss Price = " + stopLossPrice);
               Print("Take Profit Price = " + takeProfitPrice);
               
               double lotSize = OptimalLotSize(riskPerTrade,Ask,stopLossPrice);
               
               openOrderID = OrderSend(NULL,OP_BUY,lotSize,Ask,10,stopLossPrice,takeProfitPrice,NULL,magicNB);
               
               if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());
      }
            else if(Bid < MA_1 < MA_2 && PDS < 0.0004)                //shorting
               {
                  Print("Price is below the two MA's, Sending short order");
                  double stopLossPrice = NormalizeDouble(MA_1,Digits);
                  double takeProfitPrice = NormalizeDouble(bbLowerProfitExit,Digits);
                  Print("Entry Price = " + Bid);
                  Print("Stop Loss Price = " + stopLossPrice);
                  Print("Take Profit Price = " + takeProfitPrice);
                  
                  double lotSize = OptimalLotSize(riskPerTrade,Bid,stopLossPrice);
         
                  openOrderID = OrderSend(NULL,OP_SELL,lotSize,Bid,10,stopLossPrice,takeProfitPrice,NULL,magicNB);
                  if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());
      }
  }
  else
      {
      Alert("order already open");
      if(OrderSelect(openOrderID,SELECT_BY_TICKET)==true); // GET THE ORDER TO ADJUST
         {
            int ordertype = OrderType(); // 0 = long / short = 1
            
            double CurrentExitpoint;  // SL
            
            if(ordertype == 0) // long
            {
               CurrentExitpoint = NormalizeDouble(bbUpperProfitExit, Digits);
            }
            else // short
            {
               CurrentExitpoint = NormalizeDouble(bbLowerProfitExit, Digits); // tp
            }
            double CurrentTPLong = NormalizeDouble(bbUpperProfitExit, Digits); // take profit level
            double CurrentTPShort = NormalizeDouble(bbLowerProfitExit, Digits); // take profit level
     
            double SL = OrderStopLoss();
            double TP = OrderTakeProfit();
            
            if(TP != CurrentTPLong ||TP != CurrentTPShort || SL != CurrentExitpoint);  // to adjust SL and TP
            {
               bool Ans = OrderModify(openOrderID, OrderOpenPrice(), CurrentExitpoint, TP ,0); // orderopenprice default JUST THE default
               if(Ans == true);
               {
                  Alert("Order Modified " + openOrderID);
               }
            }
         }
      
      }
  
  }

//+------------------------------------------------------------------+
Files:
 

Hope this helps


1. try restarting your the mtq4 editor and meta trader, probably a wifi or a network issue on your part or the broker.

2. just use a fixed stop loss so you dont have to worry of updating it. also I saw that you put " ; " on the end of the if statement, try removing that one out might solve the errors your getting, this also apply to issue number 3 on your errors. Also since it say the error on line (154,79), try moving the lines on (154,79) on a different line of the code.

 
 if(Ask > MA_1 > MA_2 && PDL < 0.0004)  //long

This doesn't do what you think it does.

True = non-zero and false = zero so you get:

if( 3 < 2 < 1 )
if( false < 1 )
if(     0 < 1 )
if(     true  )
if( 3 > 2 > 1 )
iftrue > 1 )
if(     1 > 1 )
if(     false )
 
William Roeder #:

This doesn't do what you think it does.

True = non-zero and false = zero so you get:

Hi thank you for your reply, I'm still a bit new to this programming thing, would you mind explaining further?
 
Invoker #:
Hi thank you for your reply, I'm still a bit new to this programming thing, would you mind explaining further?

You can usually test by speaking your boolean test out loud and if it sounds strange it is probably wrong.

if(Bid < MA_1 < MA_2 && PDS < 0.0004

"If bid is smaller than MA_1 is smaller than MA_2 and PDS is smaller than 0.0004" sounds odd, does it not?

So what does it mean?

Is it

"If bid is smaller than MA_1 and MA_1 is smaller than MA_2 and PDS is smaller than 0.0004" 

or is it

"If bid is smaller than MA_1 and bid is smaller than MA_2 and PDS is smaller than 0.0004" 

These 2 you can read out loud and they don't sound odd.

if(Bid < MA_1 && MA_1 < MA_2 && PDS < 0.0004
if(Bid < MA_1 && Bid < MA_2 && PDS < 0.0004
 
Keith Watford #:

You can usually test by speaking your boolean test out loud and if it sounds strange it is probably wrong.

"If bid is smaller than MA_1 is smaller than MA_2 and PDS is smaller than 0.0004" sounds odd, does it not?

So what does it mean?

Is it

"If bid is smaller than MA_1 and MA_1 is smaller than MA_2 and PDS is smaller than 0.0004" 

or is it

"If bid is smaller than MA_1 and bid is smaller than MA_2 and PDS is smaller than 0.0004" 

These 2 you can read out loud and they don't sound odd.

Hi Keith, thanks, it all makes sense now. The EA now takes orders automatically, but issue 1 and 3 still persist. Once the trade had been closed automatically, and another trade parameter has been met, my EA won't take the trade for some unknown reason. Basically my EA acts more like a script that an EA, working only once then that's it. Any advice would be much appreciated 


   if(!CheckIfOpenOrdersByMagicNB(magicNB))//if no open orders try to enter new position
   {
      if(Open[1] > MA_1 && Close[1] > MA_1 && MA_1 > MA_2)  //long
      {
               Print("Price is above the two MA's, Sending buy order");
               double stopLossPrice = NormalizeDouble(MA_1,Digits);
               double takeProfitPrice = NormalizeDouble(bbUpperProfitExit,Digits); 
               Print("Entry Price = " + Ask);
               Print("Stop Loss Price = " + stopLossPrice);
               Print("Take Profit Price = " + takeProfitPrice);
               
               double lotSize = OptimalLotSize(riskPerTrade,Ask,stopLossPrice);
               
               openOrderID = OrderSend(NULL,OP_BUY,lotSize,Ask,10,stopLossPrice,takeProfitPrice,NULL,magicNB);
               
               if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());
      }
            else if(Open[1] < MA_1 && Close[1] < MA_1 && MA_1 < MA_2)                //shorting
               {
                  Print("Price is below the two MA's, Sending short order");
                  double stopLossPrice = NormalizeDouble(MA_1,Digits);
                  double takeProfitPrice = NormalizeDouble(bbLowerProfitExit,Digits);
                  Print("Entry Price = " + Bid);
                  Print("Stop Loss Price = " + stopLossPrice);
                  Print("Take Profit Price = " + takeProfitPrice);
                  
                  double lotSize = OptimalLotSize(riskPerTrade,Bid,stopLossPrice);
         
                  openOrderID = OrderSend(NULL,OP_SELL,lotSize,Bid,10,stopLossPrice,takeProfitPrice,NULL,magicNB);
                  if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());
      }