[MQL4 Coding Help] EA Does not work in Zero Spread account

shanmugapradeep

Active Trader
Dec 18, 2020
118
5
34
38
Hello,

I have an EA that works well in trading accounts with some spread, but it does not function properly in trading accounts where the spread is zero. In a zero spread account, the limit orders automatically close instantly after being opened, and the EA continues to operate indefinitely. Upon reviewing the code, I discovered that there is an "if" condition that is causing the current issue. In a zero spread account, this condition evaluates to true and deletes pending orders. I'm unsure why this "if" condition is important and what its purpose is. If I remove this "if" condition, the EA works fine in both spread and zero spread accounts, but I'm concerned about potential consequences on trading performance if I remove it.

MQL4:
BuyLimitMagic = 9;
SellLimitMagic = 99;
Range = 21;
 
   if(Digits == 3 || Digits == 5)
      Pips = 10.0 * Point;
   else
      Pips = Point;
 
 double value1= Orders1() - PendingOrder1() - MarketInfo(Symbol(), MODE_SPREAD) * Pips;
      if(value1> 0.0 && value1> Range * Pips)
         DeletePendingOrders(BuyLimitMagic);
 
      double value2= PendingOrder2() - Order2() - MarketInfo(Symbol(), MODE_SPREAD) * Pips;
      if(value2> 0.0 && value2> Range * Pips)
         DeletePendingOrders(SellLimitMagic);
 
double Orders1()
  {
   int cmd_0;
   double order_open_price_4;
   for(int pos_12 = 0; pos_12 < OrdersTotal(); pos_12++)
     {
      OrderSelect(pos_12, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != BuyLimitMagic)
         continue;
      cmd_0 = OrderType();
      if(cmd_0 == OP_BUY)
         order_open_price_4 = OrderOpenPrice();
      if(cmd_0 == OP_SELL)
         order_open_price_4 = OrderOpenPrice();
     }
   return (order_open_price_4);
  }
 
double PendingOrder1()
  {
   int cmd_0;
   double order_open_price_4;
   for(int pos_12 = 0; pos_12 < OrdersTotal(); pos_12++)
     {
      OrderSelect(pos_12, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != BuyLimitMagic)
         continue;
      cmd_0 = OrderType();
      if(cmd_0 == OP_BUYLIMIT)
         order_open_price_4 = OrderOpenPrice();
      if(cmd_0 == OP_SELLLIMIT)
         order_open_price_4 = OrderOpenPrice();
     }
   return (order_open_price_4);
  }
 
double Order2()
  {
   int cmd_0;
   double order_open_price_4;
   for(int pos_12 = 0; pos_12 < OrdersTotal(); pos_12++)
     {
      OrderSelect(pos_12, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != SellLimitMagic)
         continue;
      cmd_0 = OrderType();
      if(cmd_0 == OP_BUY)
         order_open_price_4 = OrderOpenPrice();
      if(cmd_0 == OP_SELL)
         order_open_price_4 = OrderOpenPrice();
     }
   return (order_open_price_4);
  }
 
double PendingOrder2()
  {
   int cmd_0;
   double order_open_price_4;
   for(int pos_12 = 0; pos_12 < OrdersTotal(); pos_12++)
     {
      OrderSelect(pos_12, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != SellLimitMagic)
         continue;
      cmd_0 = OrderType();
      if(cmd_0 == OP_BUYLIMIT)
         order_open_price_4 = OrderOpenPrice();
      if(cmd_0 == OP_SELLLIMIT)
         order_open_price_4 = OrderOpenPrice();
     }
   return (order_open_price_4);
  }
 
void DeletePendingOrders(int deletependingorder_magic)
  {
   for(int pos_0 = OrdersTotal() - 1; pos_0 >= 0; pos_0--)
     {
      OrderSelect(pos_0, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != deletependingorder_magic)
         continue;
      if(OrderType() > OP_SELL)
         OrderDelete(OrderTicket());
     }
  }

Please help me understand this code and what could be the reason behind this condition closing pending orders only in zero spread accounts. Additionally, how can I rewrite this code so that it will work in zero spread accounts the same way as in spread accounts?
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,822
1,419
144
Odesa
www.earnforex.com
It's hard to tell without knowing what this code is supposed to do.
Right now the two conditions work like this:
1. Find a pending order's open price.
2. Find a market order's open price.
3. Then it checks if the market order's open price is at least a spread away plus 21 pips from the pending one.

There is no defined behavior for when there is either no pending order or no market order because the returned price is not initialized.