help needed with too many orders opened.

 

Hi all,

Im quite new to programming and I am trying to make an EA that does the following:

When the price gets above a certain level, any pending sell order for the same symbol is deleted and a pending buy is placed at an x number of pips above the current price. The same counts when the price drops below a certain level, only the other way around.

There should be only one pending or market order per symbol. The problem I have is that when I run a backtest, the first few trades are opened an deleted correctly, one at the time.
But after a few trades, it keeps spitting pending buy or sell orders, no matter how many trades are already placed in the relevant symbol. The same happens when I let this ea run on a demo account.

I also have 2 functions that set stop loss at break even and close half of the order as a certain profit level is reached. Maybe they have something to do with the problem.

I cant figure out what's happening. I tried putting Print at some points to see what values are stored, but it hasn't helped me so far.


Could somebody please have a look and tell me what I am missing?


Here's the relevant code:

bool buy=false;

if (Ask>=boven) 
    {
     if(delete_pending_sell()==0)     
     {
     buy =true;    
     }
    }


if (buy==true)
   {
   ticket=   OrderSend(Symbol(),OP_BUYSTOP,lots,Ask+kick,0,(Ask+kick)-straal,0,0,magicb1,0,0);   
   }


//Function that deletes pending and counts already opened trades. 

int delete_pending_sell()
{
   int t = 0;
   int i;
   for(i=OrdersTotal()-1;i==0;i--) 
      {
      OrderSelect(i, SELECT_BY_POS);
     
         
         if(OrderMagicNumber() == magics1&&OrderType()==OP_SELLSTOP)
         {
          OrderDelete(OrderTicket());
         } 
     
         if(OrderMagicNumber()==magicb1)
           {
           t++;
           }
     } 
return(t);
}


//------------------------------set sl to b/e----

if (OrdersTotal()>0&&breakeven==true)
  {
  for(i=OrdersTotal()-1;i==0;i--) 
     {
     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

       if (OrderMagicNumber()==magicb1&&OrderType()==OP_BUY)
          {
          if(Bid>=(OrderOpenPrice()+straal)&&OrderOpenPrice()>OrderStopLoss())
            {
             if(OrderOpenPrice()!=OrderStopLoss())
               {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),0,0,0);
               }
            } 
            
          }
          
          
        if (OrderMagicNumber()==magics1&&OrderType()==OP_SELL)
          {
          if(Ask<=(OrderOpenPrice()-straal)&&OrderOpenPrice()<OrderStopLoss())
            {
             if(OrderOpenPrice()!=OrderStopLoss())
               {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),0,0,0);
               }
            } 
          }      
      }
}
//==================================close 1/2====================================

if (OrdersTotal()>0&&scale==true)
  {
  for(i=OrdersTotal()-1;i==0;i--) 
     {
     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

       if (OrderMagicNumber()==magicb1&&OrderType()==OP_BUY)
          {
          if(Bid>=(OrderOpenPrice()+straal))
            {
             if(OrderLots()==lots)
               {
               OrderClose(OrderTicket(),NormalizeDouble(lots/2,1),Bid,0);
               }
            } 
            
          }

       
       if (OrderMagicNumber()==magics1&&OrderType()==OP_SELL)
          {
          if(Ask<=(OrderOpenPrice()-straal))
            {
             if(OrderLots()==lots)
               {
               OrderClose(OrderTicket(),NormalizeDouble(lots/2,1),Ask,0);
               }
            } 
            
          }

    }
}
 
bool buy=false;

if (Ask>=boven) 
    {
     if(delete_pending_sell()==0)     
     {
     buy =true;    
     }
    }

if u r using it for a function it's supposed to be

bool Fun_buy()
{
bool buy=false;
if (Ask>=boven) 
   {
   if(delete_pending_sell()==0)     
      {
      buy =true;    
      }
      }
return(buy);
}
 

I do not see anywhere in your code, where is your limit for only a single Order

if (buy==true && OrdersTotal() == XX)// or if (buy==true && delete_pending_sell().....)
   {
   ticket=   OrderSend(Symbol(),OP_BUYSTOP,lots,Ask+kick,0,(Ask+kick)-straal,0,0,magicb1,0,0);   
   }
 
qjol:

if u r using it for a function it's supposed to be


hi qjol,

Thanks for your quick reply. Im not sure if I understand how putting the above code into a function should prevent this ea from producing so many trades at once; I have to call it from somewhere else in the code than. The trigger should be Ask>=certain level which should result in deleting pending orders in the opposite direction and counting open orders. When put in a function like above, this trigger is included in the function it self. Could you please explain a bit more?


thanks!!

 
qjol:

I do not see anywhere in your code, where is your limit for only a single Order


In the delete_pending function the number of open trades in the same direction are counted and returned.

if(OrderMagicNumber()==magicb1)
           {
           t++;
           }
     } 
return(t);

Only of this function returns 0 (and pending sells where deleted) buy is allowed:

if(delete_pending_sell()==0)     
     {
     buy =true;    
     }
This should prevent the ea from opening multiple trades, right?


 

If this is not a function & that is written after the start()

Every time he reaches this line (bool buy=false;) it becomes false & After it passes all the lines that follow if (Ask>=boven) & if(delete_pending_sell()==0) It becomes back to True

but if this is a function, the EA does not comes to this line over n over all the time, just if you are calling it

 
remco:

hi qjol,

Thanks for your quick reply. Im not sure if I understand how putting the above code into a function should prevent this ea from producing so many trades at once; I have to call it from somewhere else in the code than. The trigger should be Ask>=certain level which should result in deleting pending orders in the opposite direction and counting open orders. When put in a function like above, this trigger is included in the function it self. Could you please explain a bit more?

thanks!!


If this is not a function & that is written after the start()

Every time The EA reaches this line (bool buy=false;) it becomes false & After it passes all the lines that follow, if (Ask>=boven) & if(delete_pending_sell()==0) It becomes back to True

but if this is a function, the EA does not comes to this line over n over all the time, just if you are calling it

 
qjol:

If this is not a function & that is written after the start()

Every time he reaches this line (bool buy=false;) it becomes false & After it passes all the lines that follow if (Ask>=boven) & if(delete_pending_sell()==0) It becomes back to True

but if this is a function, the EA does not comes to this line over n over all the time, just if you are calling it


I see, but the result should be the same; buy can only be set to true of ask<boven and there are 0 open buy trades. In my case there are open buy trades, but still it makes new ones.
 
remco:

I see, but the result should be the same; buy can only be set to true of ask<boven and there are 0 open buy trades. In my case there are open buy trades, but still it makes new ones.

NOP, buy can only be set to true if ask >= boven....
 
qjol:

NOP, buy can only be set to true if ask >= boven....

My mistake, i meant ask>=boven.
 

Sorry, that's the max I see, I tried to help, Unfortunately without success

Reason: