EA opens Limit order and closes it in seconds and repeats

 

Hello,

EA code is to open Buy Limit order if Open Buy Orders > 0.And close any Limit order when Open Buy Orders = 0

It does but it closes it after seconds and repeats this

How could we fix this? Thanks in advance!

Code:

int count_sellLIMITorder()
  {
   int countsellLIMIT=0;
   for(int n=0;n<OrdersTotal();n++)
     {      if(OrderSelect(n,SELECT_BY_POS)==true)
        {         if(OrderSymbol()==Symbol() && OrderType()==OP_SELLLIMIT)
           {            countsellLIMIT=countsellLIMIT+1;
            //count++;
           }
        }
     }
   return(countsellLIMIT);
  }
 
int count_buyorder()
  {
   int countbuy=0;
   for(int o=0;o<OrdersTotal();o++)
     {
      if(OrderSelect(o,SELECT_BY_POS)==true)
        {
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY )
           {
            countbuy=countbuy+1;
            //count++;
           }
        }
     }
   return(countbuy);
  }

int count_sellorder()
  {
   int countsell=0;
   for(int l=0;l<OrdersTotal();l++)
     {
      if(OrderSelect(l,SELECT_BY_POS)==true)
        {
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL  )
           {
            countsell=countsell+1;
            //count++;
           }
        }
     }
   return(countsell);
  }

int countbuyLIMIT;

void start()
{


 // {
   countbuyLIMIT=0;
   for(int m=0;m<OrdersTotal();m++)
     {      if(OrderSelect(m,SELECT_BY_POS)==true)
        {         if(OrderSymbol()==Symbol() && OrderType()==OP_BUYLIMIT)
           {            countbuyLIMIT=countbuyLIMIT+1;
            //count++;
           }
        }
     }
  // return(countbuyLIMIT);
 // }

if (
    count_buyorder()==0 // && OTHER CONDITIONS MET 
   ) 
         {ticket=OrderSend(Symbol(),OP_BUY,InitLots,Ask,10,SL,TP,0,0,0,Lime); Time0=Time[0];
          ticket=OrderSend(Symbol(),OP_BUYLIMIT,InitLots,Ask-NormalizeDouble(atr,4),10,SL,TP,0,0,0,Lime);
         }
        
if ( count_buyorder() > 0 && countbuyLIMIT==0 && count_sellLIMITorder()==0   )
          ticket=OrderSend(Symbol(),OP_BUYLIMIT,InitLots,Ask-NormalizeDouble(atr,4),10,SL,TP,0,0,0,Lime);


if (
    count_sellorder()==0 // &&  OTHER CONDITIONS MET
   )
         {ticket=OrderSend(Symbol(),OP_SELL,InitLots,Bid,10,SL,TP,0,0,0,Red); Time0=Time[0];
          ticket=OrderSend(Symbol(),OP_SELLLIMIT,InitLots,Bid+NormalizeDouble(atr,4),0,SL,TP,0,0,0,White);
         }
        
if ( count_sellorder() > 0 && count_sellLIMITorder()==0 && countbuyLIMIT==0 )
          ticket=OrderSend(Symbol(),OP_SELLLIMIT,InitLots,Bid+NormalizeDouble(atr,4),0,SL,TP,0,0,0,White);


int total=OrdersTotal();
for(int cnt=total;cnt>=0;cnt--)

     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(   // check for opened position
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY  )   // LONG position is opened
           {
            // should it be closed? 
            if(         /*  OTHER CONDITIONS MET */       )
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
              //   OrderDelete(OrderTicket());
                }
            // check for trailing stop
           
           }
         if( OrderType()==OP_BUYLIMIT ) // IF 0 ONGOING ORDERS - CLOSE LIMITS
            {
            if (count_buyorder()==0)
               {OrderDelete(OrderTicket());}
            }
          
         if(OrderType()==OP_SELL  ) // go to SHORT position 
           {
            // should it be closed?
            if(          /*  OTHER CONDITIONS MET */         )
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
            //   OrderDelete(OrderTicket());
                // exit
              }
            // check for trailing stop
           
           }
         if( OrderType()==OP_SELLLIMIT ) // IF 0 ONGOING ORDERS - CLOSE LIMITS
            {
            if (count_sellorder()==0)
               {OrderDelete(OrderTicket());}
            }

        }
     }
   return;
  }
 

Your code could be simplified a lot. Also, there's no need to keep changing the name of the iterator variable in all of your loops. Typically (a good rule of thumb) 'i' is used as the iterator variable unless the loop is nested then inner loop names proceed as  j,k,l, etc.


int CountByOrderType(int type)
{
   int count=0;
   for(int i=OrdersTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type)
         count++;
   return count;
}

bool CloseOrdersByType(int type)
{
   bool res = true;
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type)
      {
         if(type==OP_BUY)
         {   if(!OrderClose(OrderTicket(),OrderLots(),Bid,10))
               res = false;
         }
         else
         if(type==OP_SELL)
         {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,10))
               res = false;
         }
         else
         {
            if(!OrderDelete(OrderTicket()))
               res = false;
         }
      }
   }
   return res;
}

void OnTick()
{
   int buy_orders = CountByOrderType(OP_BUY);
   int buylimit_orders = CountByOrderType(OP_BUYLIMIT);
   if(buy_orders > 0 && buylimit_orders < 1)
      //open limit
   else
   if(buy_orders <=0)
      CloseOrdersByType(OP_BUYLIMIT);
      
}
 
nicholishen:

Your code could be simplified a lot. Also, there's no need to keep changing the name of the iterator variable in all of your loops. Typically (a good rule of thumb) 'i' is used as the iterator variable unless the loop is nested then inner loop names proceed as  j,k,l, etc.


thanks for reply!

i tried to insert OrderSend(OP_BUY) also but now EA opens OP_BUY but don't open any Limit order

is there a mistake somewhere?

--- EDIT:

i rushed to reply, code is opening Buy Limits but not all instantly, some has few to more than 10 bars gap

 

void OnTick()

{

   int buy_orders = CountByOrderType(OP_BUY);
   int buylimit_orders = CountByOrderType(OP_BUYLIMIT);
  
   if(buy_orders <= 0 /* && -OTHER CONDITIONS-  */ )
      {
      ticket=OrderSend(Symbol(),OP_BUY,InitLots,Ask,10,SL,TP,0,MAGIC,0,Lime);
      }
  
   if(buy_orders > 0 && buylimit_orders < 1)
      {
      //open limit
      ticket=OrderSend(Symbol(),OP_BUYLIMIT,InitLots,Ask-NormalizeDouble(atr,4),10,SL,TP,0,MAGIC,0,Lime);
      }
   else
   if(buy_orders <=0)
      CloseOrdersByType(OP_BUYLIMIT);

}
 

Hello,

What could be wrong?

I made corrections but EA is not closing orders OP_BUY and OP_SELL

 else
   if(buy_orders > 0 && RSI > 70)
      CloseOrdersByType(OP_BUY);
     
   else
   if(sell_orders > 0 && RSI < 30)
      CloseOrdersByType(OP_SELL);
 
Darius:

Hello,

What could be wrong?

I made corrections but EA is not closing orders OP_BUY and OP_SELL:

   else
   if(buy_orders > 0 && RSI > 70)
      CloseOrdersByType(OP_BUY);
     
   else
   if(sell_orders > 0 && RSI < 30)
      CloseOrdersByType(OP_SELL);

Nobody can help you unless you post all of your code. These micro-snippets tell us nothing. And also, have you stepped through your code in the debugger? 
 
When you post code please use the SRC button! Please edit your post.
          General rules and best pratices of the Forum. - General - MQL5 programming forum
 
nicholishen:
Nobody can help you unless you post all of your code. These micro-snippets tell us nothing. And also, have you stepped through your code in the debugger? 

I have never used debugger :) i just test codes on demo accounts

posting whole code:

extern double    MiniLots=0.01;
extern double    MaxLot=10;
extern bool RiskControl = true;
extern int MaxTrades=30;
extern int SL=0;
extern int TP=0;
extern int BE=0;
extern double MAGIC=20171206;

double InitLots;
int ticket;


// ++++++++++++++++++++++++++++++++++++++


int init()
  {   return(0);  }


// ++++++++++++++++++++++++++++++++++++++
 
 
int deinit()
  {  return(0);  }
 
 
// ++++++++++++++++++++++++++++++++++++++


int CountByOrderType(int type)
{
   int count=0;
   for(int i=OrdersTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type)
         count++;
   return count;
}

bool CloseOrdersByType(int type)
{
   bool res = true;
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type)
      {
         if(type==OP_BUY)
         {   if(!OrderClose(OrderTicket(),OrderLots(),Bid,10,Violet))
               res = false;
         }
         else
         if(type==OP_SELL)
         {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,10,Violet))
               res = false;
         }
         else
         {
            if(!OrderDelete(OrderTicket()))
               res = false;
         }
      }
   }
   return res;
}

void OnTick()
{

double PowerRisk;

if(MaxLot > MarketInfo(Symbol(),MODE_MAXLOT)) MaxLot=MarketInfo(Symbol(),MODE_MAXLOT);
if(InitLots < MarketInfo(Symbol(),MODE_MINLOT)) InitLots=MarketInfo(Symbol(),MODE_MINLOT);
   //cal Lot size
   if(RiskControl) {
      PowerRisk=AccountBalance()/5000;
      if(PowerRisk < 1) PowerRisk=1;
      InitLots = MiniLots *PowerRisk;
      if(InitLots > MaxLot) InitLots = MaxLot;
      }
   if(RiskControl==false) InitLots=MiniLots;


   int buy_orders = CountByOrderType(OP_BUY);
   int buylimit_orders = CountByOrderType(OP_BUYLIMIT);
   int sell_orders = CountByOrderType(OP_SELL);
   int selllimit_orders = CountByOrderType(OP_SELLLIMIT);
  
   if(buy_orders <= 0    /* &&  OTHER CONDITIONS */    )
      {      ticket=OrderSend(Symbol(),OP_BUY,InitLots,Ask,10,SL,TP,0,MAGIC,0,Lime);      }
     
   else
   if(buy_orders > 0 && buylimit_orders < 1)
      {      //open BUY limit
      ticket=OrderSend(Symbol(),OP_BUYLIMIT,InitLots,Ask-NormalizeDouble(atr,4),10,SL,TP,0,MAGIC,0,Lime);      }
     
   else
   if(buy_orders <=0)
      CloseOrdersByType(OP_BUYLIMIT);
     
   else
   if(sell_orders <= 0    /* &&  OTHER CONDITIONS */   )
      {      ticket=OrderSend(Symbol(),OP_SELL,InitLots,Bid,10,SL,TP,0,MAGIC,0,Red);      }
     
   else
   if(sell_orders > 0 && selllimit_orders < 1)
      {      // open SELL limit
      ticket=OrderSend(Symbol(),OP_SELLLIMIT,InitLots,Bid+NormalizeDouble(atr,4),0,SL,TP,0,MAGIC,0,White);      }
     
   else
   if(sell_orders <=0)
      CloseOrdersByType(OP_SELLLIMIT);
     
   else
   if(buy_orders > 0 && RSI > 70)
      CloseOrdersByType(OP_BUY);
     
   else
   if(sell_orders > 0 && RSI < 30)
      CloseOrdersByType(OP_SELL);


 return;
}
 
Darius:

I have never used debugger :) i just test codes on demo accounts

posting whole code:


extern double    MiniLots=0.01;
extern double    MaxLot=10;
extern bool RiskControl = true;
extern int MaxTrades=30;
extern int SL=0;
extern int TP=0;
extern int BE=0;
extern double MAGIC=20171206;

double InitLots;
int ticket;


// ++++++++++++++++++++++++++++++++++++++


int init()
  {   return(0);  }


// ++++++++++++++++++++++++++++++++++++++
 
 
int deinit()
  {  return(0);  }
 
 
// ++++++++++++++++++++++++++++++++++++++


int CountByOrderType(int type)
{
   int count=0;
   for(int i=OrdersTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type)
         count++;
   return count;
}

bool CloseOrdersByType(int type)
{
   bool res = true;
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC&&OrderType()==type)
      {
         if(type==OP_BUY)
         {   if(!OrderClose(OrderTicket(),OrderLots(),Bid,10,Violet))
               res = false;
         }
         else
         if(type==OP_SELL)
         {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,10,Violet))
               res = false;
         }
         else
         {
            if(!OrderDelete(OrderTicket()))
               res = false;
         }
      }
   }
   return res;
}

void OnTick()
{

double PowerRisk;

if(MaxLot > MarketInfo(Symbol(),MODE_MAXLOT)) MaxLot=MarketInfo(Symbol(),MODE_MAXLOT);
if(InitLots < MarketInfo(Symbol(),MODE_MINLOT)) InitLots=MarketInfo(Symbol(),MODE_MINLOT);
   //cal Lot size
   if(RiskControl) {
      PowerRisk=AccountBalance()/5000;
      if(PowerRisk < 1) PowerRisk=1;
      InitLots = MiniLots *PowerRisk;
      if(InitLots > MaxLot) InitLots = MaxLot;
      }
   if(RiskControl==false) InitLots=MiniLots;


   int buy_orders = CountByOrderType(OP_BUY);
   int buylimit_orders = CountByOrderType(OP_BUYLIMIT);
   int sell_orders = CountByOrderType(OP_SELL);
   int selllimit_orders = CountByOrderType(OP_SELLLIMIT);
  
   if(buy_orders <= 0    /* &&  OTHER CONDITIONS */    )
      {      ticket=OrderSend(Symbol(),OP_BUY,InitLots,Ask,10,SL,TP,0,MAGIC,0,Lime);      }
     
   else
   if(buy_orders > 0 && buylimit_orders < 1)
      {      //open BUY limit
      ticket=OrderSend(Symbol(),OP_BUYLIMIT,InitLots,Ask-NormalizeDouble(atr,4),10,SL,TP,0,MAGIC,0,Lime);      }
     
   else
   if(buy_orders <=0)
      CloseOrdersByType(OP_BUYLIMIT);
     
   else
   if(sell_orders <= 0    /* &&  OTHER CONDITIONS */   )
      {      ticket=OrderSend(Symbol(),OP_SELL,InitLots,Bid,10,SL,TP,0,MAGIC,0,Red);      }
     
   else
   if(sell_orders > 0 && selllimit_orders < 1)
      {      // open SELL limit
      ticket=OrderSend(Symbol(),OP_SELLLIMIT,InitLots,Bid+NormalizeDouble(atr,4),0,SL,TP,0,MAGIC,0,White);      }
     
   else
   if(sell_orders <=0)
      CloseOrdersByType(OP_SELLLIMIT);
     
   else
   if(buy_orders > 0 && RSI > 70)
      CloseOrdersByType(OP_BUY);
     
   else
   if(sell_orders > 0 && RSI < 30)
      CloseOrdersByType(OP_SELL);


 return;
}

You need to use the debugger to set break points and step through your code. If you don't learn how to do that then you won't be very good at mql development.
 
     {      ticket=OrderSend(Symbol(),OP_BUY,InitLots,Ask,10,SL,TP,0,MAGIC,0,Lime);      }
      ticket=OrderSend(Symbol(),OP_BUYLIMIT,InitLots,Ask-NormalizeDouble(atr,4),10,SL,TP,0,MAGIC,0,Lime);      }
      {      ticket=OrderSend(Symbol(),OP_SELL,InitLots,Bid,10,SL,TP,0,MAGIC,0,Red);      }
      ticket=OrderSend(Symbol(),OP_SELLLIMIT,InitLots,Bid+NormalizeDouble(atr,4),0,SL,TP,0,MAGIC,0,White);      }
Check your return codes for errors and report them.
          What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
          Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
 

I made this code:

   if(buy_orders <= 0 /* &&  -OTHER CONDITIONS- */ )
      {      ticket=OrderSend(Symbol(),OP_BUY,InitLots,Ask,10,SL,TP,0,MAGIC,0,Lime);      }
      if( ticket > 0 )   {   Print("Order placed # ", ticket);}
      else   {   Print("Order Send failed, error # ", GetLastError() );   }

and i get error every second i.e. "EURUSD,M5: OrderSend failed, error # 0"

 

currently i have made EA to open sell, buy and sell_limit, buy_limit orders and close on conditions

next i will learn more about debugging, we'll see if code has some bugs

Reason: