Too Many Orders

 

Guys I'm trying to keep this as simple as possible but have hit a wall. The EA should place orders when the RSI period gets to a cetain point which works fine. However when sending the Order I only wish to send one and only one order but unfortunately sends hundreds all at the one time. I would be also greatful if someone point where the stoploss in code should be place. And where can I get sample code of trailing stops? Apprieciate the help. ..

   int Ticket;
   if(_OrderProfit[aoi]>=Take_Profit) { CloseAllOrders(); }  
 
   if(iRSI(NULL,0,21,PRICE_CLOSE,0)>iRSI_Buy_Value && iRSI(NULL,0,21,PRICE_CLOSE,0)>iRSI(NULL,0,21,PRICE_CLOSE,1))
      {
         Ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Long Order",MagicNumber,0,Blue);
         if(Ticket>0) { if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Buy Order: ",OrderOpenPrice()); }
         else Print("Error opening Buy Order:  Lots " + Lots,GetLastError());
         return(0);
 
      }
   if(iRSI(NULL,0,21,PRICE_CLOSE,0)<iRSI_Sell_Value && iRSI(NULL,0,21,PRICE_CLOSE,0)<iRSI(NULL,0,21,PRICE_CLOSE,1))
      {
         Ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Sell Order",MagicNumber,0,Red);
         if(Ticket>0) { if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("ell Order: ",OrderOpenPrice()); }
         else Print("Error opening Sell Order:  Lots " + Lots,GetLastError());
         return(0);
      }
 

Show the code where you keep track or take count of how many open orders you have.

 

Try this:

I added Function to find number of open orders and only open new order when that couint = 0. I also added a CloseAllOrders() routine.

I added TakeProfit and StopLoss code.

The TrailingStop routine is one that is found in many free EAs.

I hope it helps.

// Inputs ========================================
extern int   TakeProfit = 50;
extern int   StopLoss = 50; 
extern int   TrailingStop = 15; // 0 = no trailing stop
 
 
 
// Program Start =================================
int start()
{
 
int Ticket=0;
   if(_OrderProfit[aoi]>=Take_Profit) { CloseAllOrders(); }  
 
 
// only open one order ===========================
 
if(OpenOrders()==0)
   {
 
    double tp=0,sl=0;
    if(iRSI(NULL,0,21,PRICE_CLOSE,0)>iRSI_Buy_Value && iRSI(NULL,0,21,PRICE_CLOSE,0)>iRSI(NULL,0,21,PRICE_CLOSE,1))
      {
         if(StopLoss > 0) sl=normalizeDouble(Ask-StopLoss*Point,Digits);
         if(TakeProfit > 0) tp=NormalizeDouble(Ask+TakeProfit*Point,Digits);
         Ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,sl,tp,"Long Order",MagicNumber,0,Blue);
         if(Ticket>0) { if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Buy Order: ",OrderOpenPrice()); }
         else Print("Error opening Buy Order:  Lots " + Lots,GetLastError());
         return(0);
 
      }
   if(iRSI(NULL,0,21,PRICE_CLOSE,0)<iRSI_Sell_Value && iRSI(NULL,0,21,PRICE_CLOSE,0)<iRSI(NULL,0,21,PRICE_CLOSE,1))
      {
         if(StopLoss > 0) sl=normalizeDouble(Bid+StopLoss*Point,Digits);
         if(TakeProfit > 0) tp=NormalizeDouble(Bid-TakeProfit*Point,Digits);
         Ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sl,tp,"Sell Order",MagicNumber,0,Red);
         if(Ticket>0) { if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("ell Order: ",OrderOpenPrice()); }
         else Print("Error opening Sell Order:  Lots " + Lots,GetLastError());
         return(0);
      }
   }
 
 
//TrailingStop Routine =================================
 
int total = OrdersTotal();
for(int cnt=0;cnt<total;cnt++)
   {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
     if(OrderSymbol() == Symbol()  && TrailingStop > 0)
       {
          if(OrderType()==OP_BUY) {
          if((Bid-OrderOpenPrice()) > (Point*TrailingStop)){
          if((OrderStopLoss()) < (Bid-Point*TrailingStop)){
              OrderModify( OrderTicket(), OrderOpenPrice(), Bid-TrailingStop*Point, OrderTakeProfit(), 0,GreenYellow);
            }
            }
            }               
      
          if(OrderType()==OP_SELL  ) {                           
          if((OrderOpenPrice()-Ask) > (TrailingStop*Point)){      
          if(OrderStopLoss()==0 || (OrderStopLoss()) > (Ask+Point*TrailingStop)){   
             OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,GreenYellow);        
            }
            }
            }
       }
     }
return(0);
}
 
// Function Routines ====================================
 
   int OpenOrders() { // to find number of open orders
   int _total=0; 
   for (int i=0; i<OrdersTotal(); i++) { 
     if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
       if((OrderSymbol()==Symbol())) 
          _total++; 
   }  
   return(_total); 
   } 
 
 
void CloseAllOrders() {
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol()) { 
            if (OrderType()==OP_BUY) 
               OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
            if (OrderType()==OP_SELL) 
               OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
            if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT || 
                OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) 
               OrderDelete(OrderTicket());
         }
      }
   }
}
 

phy that was my mistake I wasn't counting open orders at all. Yes another rookie mistake but you know I've only been in the MQL game for 1 1/2 weeks...

wackena I am forever greatful that you took the time to type that up. "Hope this Helps?" is an understatment as you've gone well beyond the call of duty. Also great work with FireBird 3.2 as its one EA I keep in my 'acitive' folder.

If I every come across the "Holy Grail" (Told it doesn't exist) you guys are at the top of the list. Check this space in like 5 years, ha ha!

Cheers Guys!