Opening Orders in "for loop" miss some trades sometimes ?

A Parson
199

I am trying to open trades in loop depending on the variable value (4,6 or 10 trades)

but it sometimes miss the trades, meaning if its supposed to open 10 trades then it will open only 8 or 9 or 7 trades , and if its supposed to open 4 trades its ends up opening only 3 ,2 or 1 trade

here is my code :

      ResetLastError();

      for(int i=0; i<NbrOfTradesToOpen; i++)
        {
         OpenOrder(Symbol(),1,Bid,0.01,NULL,NULL);
        }
      Print(GetLastError());
      ObjectSetInteger(0,btntoreset, OBJPROP_STATE, 0);
.
.
.
.
.
.
.
void OpenOrder(string symbol,int type,double price,double lot)
  {
   RefreshRates();


   double market=MarketInfo(symbol,MODE_BID);
   if(type==0)
     {
      market=MarketInfo(symbol,MODE_ASK);
     }
   double delta;
   delta=MathAbs(market-price)/MarketInfo(symbol,MODE_POINT);
   if(delta>4)
      return;
 RefreshRates();  if(!OrderSend(symbol,type,NormalizeDouble(lot,2),market,50,NULL,NULL,NULL))
     {

      Print("Error: ",GetLastError());
     }
   else
     {
     }
   return;
  }

What am I doing wrong ?

Thank you 

William Roeder
25873
William Roeder  
  1.  if(!OrderSend(symbol,type,NormalizeDouble(lot,2),market,50,NULL,NULL,NULL))

    OrderSend does not return a boolean.

  2. OpenOrder(Symbol(),1,Bid,0.01,NULL,NULL);

    Opening an order takes time. The market can move. Your passed price becomes invalid.

Vitor Palmeira Abbehusen
450
A Parson:

I am trying to open trades in loop depending on the variable value (4,6 or 10 trades)

but it sometimes miss the trades, meaning if its supposed to open 10 trades then it will open only 8 or 9 or 7 trades , and if its supposed to open 4 trades its ends up opening only 3 ,2 or 1 trade

here is my code :

What am I doing wrong ?

Thank you 

Use Sleep(...) function to delay between the orders and use IsTradeContextBusy() to check wether it can send the order or not.

and of course as @William Roeder has stated, OrderSend returns an integer which is the number of ticket.

and add slippage to the OrderSend function

Jeremie Courchesne
227

Hi guys!

Reading this discussion made me realize that I should probably modify my Trade Related functions.
For Closing Orders, I have read that I should save Ticket numbers in an Array instead of Counting up with OrdersTotal() in a for loop, which I did.
I feel like the Program is more Robust now.

However, when it comes to Requesting/Sending Orders, I am a bit confused as to what would be the best way to make sure the order is sent.
I had never considered that the Broker Server may be busy and/or that the Order wouldn't go through because of external factors. (Connection, Trading Hours, etc.)
I have now restricted Trading hours using MqlDateTime but I still feel that my OrderSending Function might "Miss a Trade".

I made a loop that would last 1 sec max trying to execute the request a 100 time with 10 milliseconds between each iteration in case the Trade Context is busy.
I am not sure if this is enough or if this is useful at all.
Please tell me what you guys think and any advice would be much appreciated. Here's the code.

//-----------------------------------------------------------------------------------\\
//-| Trade Entry & Modification Logic - Ticket# or -1                              |-\\
//-----------------------------------------------------------------------------------\\
   int EnterTrade(int Operation, double Size, double Price, double SL, string Note, int TradeID)
   {
      for(int i = 0; i <= 100; i++)
      {      
         //- Status Check & Error
         if(i == 100){
            Print(_Symbol," - EnterTrade - Could not Enter Position. Sending Alert Now.");
            Alert(_Symbol+" - EnterTrade - Could not Enter Position."); 
            break;
         }
         if(!IsTradeAllowed())     {Sleep(10); continue;}
         if(IsTradeContextBusy())  {Sleep(10); continue;}
         
         //- Refresh Rates if Market Order
         if(Operation == OP_BUY){
            RefreshRates();
            Price = Ask;
         }
         else if(Operation == OP_SELL){
            RefreshRates();
            Price = Bid;
         }
         
         //- Order Sending
         ResetLastError();
         int Ticket = OrderSend(_Symbol,Operation,Size,Price,30,0,0,Note,TradeID,0,clrNONE);
         if(Ticket > 0){
            
            //- Order Modification
            if(SL > 0){
               if(OrderSelect(Ticket,SELECT_BY_TICKET)) Price = OrderOpenPrice();
               else Print(_Symbol," - EnterTrade - Could not Select Ticket #",(string)Ticket,". Error: ",ErrorDescription(_LastError));
               ResetLastError();
               if(OrderModify(Ticket,Price,SL,0,0,clrNONE)) return(Ticket);
               else Print(_Symbol," - EnterTrade - Could not Modify Ticket #",(string)Ticket,". Error: ",ErrorDescription(_LastError));
            }
            else return(Ticket);
         }
         else Print(_Symbol," - EnterTrade - Could not send Order.. Error: ",ErrorDescription(_LastError));
         Sleep(10);
      }
      return(-1);
   }