Multiple Orders in EA - Problem while decrease counters

Back to topics list To post a new topic, please log in or register
avatar
31
leourb_ 2015.09.15 16:48
 

I have a problem regarding the number of orders that EA can open simultaneously. I set the code as it follows:

sinput string MAXORDERS; // Maximum Number of orders open simultaneously
extern int buys=5; // Number of buys
extern int sells=5; // Number of sell orders opened simultaneously

int gBuyTicket, gSellTicket;

//...

int OnInit()
  {
//---
   double minLot=MarketInfo(_Symbol,MODE_MINLOT); // Check if the input lot is lower than minimum market size
   if(LOTS<minLot) LOTS=minLot;
   MAX_LOSS=-MAX_LOSS;
   if(leverage<1) leverage=1;
   if(buys<1) buys=1;
   if(sells<1) sells=1;

//---
   return(INIT_SUCCEEDED);
  }

//...

void OnTick()
{
if (Volume[0]==1)
{

//...

if(/*TRADE CONDITIONS*/ && countBuy<buys)
        {
         // Open buy order
         gBuyTicket=OrderSend(_Symbol,OP_BUY,LOTS,Ask,slippage,0,TP,"Market Buy Order Opened",MAGIC,0,clrGreen);
         countBuy++;
        }

      // Sell Order Condition
      if(/*TRADE CONDITIONS*/ && countSell<sells)
        {
         // Open sell order
         gSellTicket=OrderSend(_Symbol,OP_SELL,LOTS,Bid,slippage,0,TP,"Market Sell Order Opened",MAGIC,0,clrRed);
         countSell++;
        }

      // Select and Evaluate Orders
      for(int order=OrdersTotal()-1; order>0; order--)
        {
         if(OrderSelect(order,SELECT_BY_POS))
           {
            if(OrderType()==OP_SELL && OrderMagicNumber()==MAGIC)
              {
               if(OrderClosePrice()<=OrderTakeProfit()) countSell--;
               if(/*CLOSING CONDITIONS*/)
                 {
                  // Close Order
                  bool closed=OrderClose(OrderTicket(),OrderLots(),Ask,slippage,clrGreen);
                  if(closed) countSell--;
                 }
              }
            // Close Buy Order if condition is violated
            if(OrderType()==OP_BUY && OrderMagicNumber()==MAGIC)
              {
               if(OrderClosePrice()>=OrderTakeProfit()) countBuy--;
               if(/*CLOSING CONDITIONS*/)
                 {
                  // Close Order
                  bool closed=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,clrRed);
                  if(closed) countBuy--;
                 }
              }
           }
        }
     }
  }

 It works partially because it opens orders but it does not decrease them! So once reached 5 trades for buys and 5 for sells it does not trade anymore. I have commented inside the loop for orders to see if it effectively loops and "order" variable keeps stack on 1. I cannot figure out why. Could you help me? Thanks!

avatar
13001
WHRoeder 2015.09.15 18:08 #
 
  1. EA's must be coded to recover. If you restart the terminal, crash, power glitch, BSOD, your counts are bogus.
  2. if(OrderClosePrice()>=OrderTakeProfit()) countBuy--;
    This will never be true. IF the order has closed you won't find it in the OrderSelect loop. Move the loop to above the open code, then count buys and sells that you don't close.
  3.          gBuyTicket=OrderSend(_Symbol,OP_BUY,LOTS,Ask,slippage,0,TP,"Market Buy Order Opened",MAGIC,0,clrGreen);
             countBuy++;
    
    Check your return codes. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
avatar
31
leourb_ 2015.09.15 18:27 #
 

Ok.. Mistery solved!

 One last question.. How could I select OrderClosingPrice() for each close order then? I should switch to MODE_HISTORY?

Thanks! 

avatar
125
zirkoner 2015.09.15 19:20 #
 
leourb_:

Ok.. Mistery solved!

 One last question.. How could I select OrderClosingPrice() for each close order then? I should switch to MODE_HISTORY?

Thanks! 

Yes.
avatar
31
leourb_ 2015.09.16 13:47 #
 
Thnaks!
Back to topics list  

To add comments, please log in or register