Need help with solving issue of a very complicated EA

 

Hello everyone.

I'm a beginner MQL4 programmer and I've been stuck for months solving this issue. 

The code isn't really the cleanest as it is a chimera of all MQL4 knowledge that I took here in the forums but please bear with me.

If you try to run the code it should have one running order and multiple pending orders overlapping. I coded this reset feature that when the running order hits TP, it erases all pending orders. After testing it, when the running order hit its TP, the reset feature worked and erased all orders. BUT! The EA messes up the supposed next pending trades after its RESET!

Please try and run the code in MT4 to see what I'm talking about. 

I am very much in need of help. Please help me solve the RESET issue of my code.

void OnTick()
  {//START
  
//-- DEFINING TERMS
   int tpPips = 60;
   int slPips = 30; 
   
   int modifier1 = 30; // for DM Range
   int modifier2 = 20; // for WJ
   int modifier3 = 10; // for WJ
   int modifier4 = 50; // for WJ
   int modifier5 = 60; // for WJ
   
   double dmLimitLocation = modifier1 * perPipDigit();
   double wjStopLocation1 = modifier2 * perPipDigit();
   double wjStopLocation2 = modifier3 * perPipDigit();
   double wjStopLocation3 = modifier4 * perPipDigit();
   double wjStopLocation4 = modifier5 * perPipDigit();

   double tpBuy = Bid + tpPips * perPipDigit();
   double slBuy = Bid - slPips * perPipDigit();
   double tpSell = Ask - tpPips * perPipDigit();
   double slSell = Ask + slPips * perPipDigit();
   
   double smoothedMA = iMA(NULL,PERIOD_M15,12,0,MODE_SMMA,PRICE_CLOSE,0);

   double execBuy = (Bid > smoothedMA && OrdersTotal() < 1);
   double execSell = (Ask < smoothedMA && OrdersTotal() < 1);
   
//-- DM TRADE EXEC --

// !! DM BASE EXECUTION | MAGNUM Code: 10XX !!
//--- execSell Route ---------- MAGNUM Code: 101X
for (int sell = OrdersTotal()-1; sell >=0; sell--)
{ 
   if(OrderSelect(sell, SELECT_BY_POS, MODE_TRADES) && OrderType()==OP_SELL && OrderMagicNumber()==1010)
   {
      int sellPrice = OrderOpenPrice();
      int sellTP = OrderTakeProfit();
      int sellSL = OrderStopLoss();
   }
}

if (execSell)
{
   //DM RANGE | MAGNUM Code: 10XX
   OrderSend(NULL,OP_SELL,DMls1,Bid,10,slSell,tpSell,NULL,1010);
   OrderSend(NULL,OP_BUYLIMIT,DMls1,OrderOpenPrice()-dmLimitLocation,10,OrderTakeProfit(),OrderStopLoss(),NULL,1011);
   
   //WJ RANGE | MAGNUM Code:15XX
   OrderSend(NULL,OP_BUYSTOP,WJls1,OrderOpenPrice()+wjStopLocation1,10,OrderOpenPrice(),OrderOpenPrice()+dmLimitLocation,NULL,1500);
   OrderSend(NULL,OP_SELLSTOP,WJls1,OrderOpenPrice()-wjStopLocation3,10,OrderOpenPrice()-dmLimitLocation,OrderOpenPrice()-wjStopLocation4,NULL,1550);
}  

//--- execBuy Route ---------- MAGNUM Code: 102X
for (int buy = OrdersTotal()-1; buy >=0; buy--)
{ 
   if(OrderSelect(buy, SELECT_BY_POS, MODE_TRADES) && OrderType()==OP_BUY && OrderMagicNumber()==1020)
   {
      int buyPrice = OrderOpenPrice();
      int buyTP = OrderTakeProfit();
      int buySL = OrderStopLoss();
   }
}

if (execBuy)
{
   OrderSend(NULL,OP_BUY,DMls1,Bid,10,slBuy,tpBuy,NULL,1020);
   OrderSend(NULL,OP_SELLLIMIT,DMls1,OrderOpenPrice()+dmLimitLocation,10,OrderTakeProfit(),OrderStopLoss(),NULL,1021);
   
   OrderSend(NULL,OP_SELLSTOP,WJls1,OrderOpenPrice()-wjStopLocation1,10,OrderOpenPrice(),OrderOpenPrice()-dmLimitLocation,NULL,1600);
   OrderSend(NULL,OP_BUYSTOP,WJls1,OrderOpenPrice()+wjStopLocation3,10,OrderOpenPrice()+dmLimitLocation,OrderOpenPrice()+wjStopLocation4,NULL,1650);
}  

//------------------------ CHECK IF MAGNUM OPEN + INPUT LIMIT ORDER AFTER CHECK !!!!!!!!! ------------------------------
// -- DM BASE RANGE ROUTES (Experimental) --

// execSell Route - MAGNUM Code: 101XX
for(int sellL1=OrdersTotal()-1; sellL1>=0; sellL1-- ) 
   if(OrderSelect(sellL1,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1011) //ORDERSELECT CAN BE USED FOR SL/TP 
   {
      if(OrderType() <= OP_SELL && countOpenOrders()==2)
      { 
         OrderSend(NULL,OP_BUYLIMIT,DMls1,OrderOpenPrice()+dmLimitLocation,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1012);
      }
      else
      {//nothing
      }    
   }

for(int sellL2=OrdersTotal()-1; sellL2>=0; sellL2-- ) 
   if(OrderSelect(sellL2,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1012)
   {
      if(OrderType() <= OP_BUY && countOpenOrders()==3)
      { 
         OrderSend(NULL,OP_SELLLIMIT,DMls1,OrderOpenPrice()-dmLimitLocation,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1013);
      }
      else
      {//nothing
      }    
   }

// execBuy Route - MAGNUM Code: 102X
for(int buyL1=OrdersTotal()-1; buyL1>=0; buyL1-- ) 
   if(OrderSelect(buyL1,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1021) 
   {
      if(OrderType() <= OP_SELL && countOpenOrders()==2)
      { 
         OrderSend(NULL,OP_BUYLIMIT,DMls1,OrderOpenPrice()+dmLimitLocation,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1022);
      }
      else
      {//nothing
      }    
   }

for(int buyL2=OrdersTotal()-1; buyL2>=0; buyL2-- ) 
   if(OrderSelect(buyL2,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1022) 
   {
      if(OrderType() <= OP_BUY && countOpenOrders()==3)
      { 
         OrderSend(NULL,OP_SELLLIMIT,DMls1,OrderOpenPrice()-dmLimitLocation,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1023 );
      }
      else
      {//nothing
      }    
   }
   
// ------- WHITE JET EXECUTION ROUTES -------
//execSellRoute | MAGNUM Code: 15XX
//Top
for(int exSTop1=OrdersTotal()-1; exSTop1>=0; exSTop1-- ) 
   if(OrderSelect(exSTop1,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1500)
   {
      if(OrderType() <= OP_BUY && countStopOrders()==2)
      { 
         OrderSend(NULL,OP_SELLSTOP,WJls2,OrderOpenPrice()-wjStopLocation2,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1501);
      }
      else
      {//nothing
      }    
   }

for(int exSTop2=OrdersTotal()-1; exSTop2>=0; exSTop2-- ) 
   if(OrderSelect(exSTop2,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1501)
   {
      if(OrderType() <= OP_SELL && countStopOrders()==3)
      { 
         OrderSend(NULL,OP_BUYSTOP,WJls3,OrderOpenPrice()+wjStopLocation2,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1502);
      }
      else
      {//nothing
      }    
   }
   
//Bot   
for(int exSBot1=OrdersTotal()-1; exSBot1>=0; exSBot1-- ) 
   if(OrderSelect(exSBot1,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1550)
   {
      if(OrderType() <= OP_SELL && countStopOrders()==2)
      { 
         OrderSend(NULL,OP_BUYSTOP,WJls2,OrderOpenPrice()+wjStopLocation2,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1551);
      }
      else
      {//nothing
      }    
   }
   
for(int exSBot2=OrdersTotal()-1; exSBot2>=0; exSBot2-- ) 
   if(OrderSelect(exSBot2,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1551)
   {
      if(OrderType() <= OP_BUY && countStopOrders()==3)
      { 
         OrderSend(NULL,OP_SELLSTOP,WJls3,OrderOpenPrice()-wjStopLocation2,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1552);
      }
      else
      {//nothing
      }    
   }

//execBuyRoute | MAGNUM Code: 16XX
//Top
for(int exBTop1=OrdersTotal()-1; exBTop1>=0; exBTop1-- ) 
   if(OrderSelect(exBTop1,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1650)
   {
      if(OrderType() <= OP_BUY && countStopOrders()==2)
      { 
         OrderSend(NULL,OP_SELLSTOP,WJls2,OrderOpenPrice()-wjStopLocation2,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1651);
      }
      else
      {//nothing
      }    
   }
   
for(int exBTop2=OrdersTotal()-1; exBTop2>=0; exBTop2-- ) 
   if(OrderSelect(exBTop2,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1651)
   {
      if(OrderType() <= OP_SELL && countStopOrders()==3)
      { 
         OrderSend(NULL,OP_BUYSTOP,WJls3,OrderOpenPrice()+wjStopLocation2,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1652);
      }
      else
      {//nothing
      }    
   }

//Bot
for(int exBBot1=OrdersTotal()-1; exBBot1>=0; exBBot1-- ) 
   if(OrderSelect(exBBot1,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1600)
   {
      if(OrderType() <= OP_SELL && countStopOrders()==2)
      { 
         OrderSend(NULL,OP_BUYSTOP,WJls2,OrderOpenPrice()+wjStopLocation2,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1601);
      }
      else
      {//nothing
      }    
   }
   
for(int exBBot2=OrdersTotal()-1; exBBot2>=0; exBBot2-- ) 
   if(OrderSelect(exBBot2,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==1601)
   {
      if(OrderType() <= OP_BUY && countStopOrders()==3)
      { 
         OrderSend(NULL,OP_SELLSTOP,WJls3,OrderOpenPrice()-wjStopLocation2,10,OrderTakeProfit(),OrderStopLoss(),"LIMITTEST",1601);
      }
      else
      {//nothing
      }    
   }

// --- DELETE WJ PENDING RESET ---
if (checkOpenOrders()==false)
{
for (int wjReset = OrdersTotal()-1; wjReset >=0; wjReset--)
{ 
   if(OrderSelect(wjReset, SELECT_BY_POS, MODE_TRADES) && OrderType()==OP_SELLSTOP || OrderType()==OP_BUYSTOP)
   {
      int ticket1 = OrderTicket();
      OrderDelete(ticket1,clrNONE);
   }
}

for (int dmReset = OrdersTotal()-1; dmReset >=0; dmReset--)
{ 
   if(OrderSelect(dmReset, SELECT_BY_POS, MODE_TRADES) && OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT)
   {
      int ticket2 = OrderTicket();
      OrderDelete(ticket2,clrNONE);
   }
}
}


}
//---
//+------------------------------------------------------------------+

//FUNCTIONS

double perPipDigit()
{
   if (_Digits >=4)//-- FX and Stocks
   {
      return 0.0001;
   }
   else if (_Digits <=2) //-- XAUUSD
   {
      return 0.1;
   }
   else //-- xxxJPY
   {
     return 0.01;
   }
}

int countOpenOrders()
{
   int countOP = 0;
   for (int x = OrdersTotal()-1; x >=0; x--)
   {
      if( OrderSelect(x, SELECT_BY_POS, MODE_TRADES) && OrderType() == OP_SELL || OrderType()==OP_BUY)
      {
         countOP++;
      }
   }
   return( countOP );
}

bool checkOpenOrders()
{
   for (int z = OrdersTotal()-1; z >=0; z--)
   {  
      if( OrderSelect(z, SELECT_BY_POS, MODE_TRADES) && OrderType() == OP_SELL || OrderType()==OP_BUY)
      return(true); 
   } 
      return(false); //no open orders for mag#
}

bool checkStopOrders()
{
   for (int y = OrdersTotal()-1; y >=0; y--)
   {  
      if( OrderSelect(y, SELECT_BY_POS, MODE_TRADES) && OrderType() == OP_SELLSTOP || OrderType()==OP_BUYSTOP)
      return(true); 
   } 
      return(false); //no open orders for mag#
}

int countStopOrders()
{
   int countOP = 0;
   for (int x = OrdersTotal()-1; x >=0; x--)
   {
      if( OrderSelect(x, SELECT_BY_POS, MODE_TRADES) && OrderType() == OP_SELLSTOP || OrderType()==OP_BUYSTOP)
      {
         countOP++;
      }
   }
   return( countOP );
}
 
  1.    double execBuy = (Bid > smoothedMA && OrdersTotal() < 1);
       double execSell = (Ask < smoothedMA && OrdersTotal() < 1);
    

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles 2011

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

  2. OrderSend(NULL,OP_SELLSTOP,WJls3

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020)