Multiple orders problem - page 5

 
RaptorUK:

Read through the execution error codes and make your code comply with the information given there, for example . . . 

ERR_TRADE_TIMEOUT128Timeout for the trade has been reached. Before retry (at least, in 1-minute time), it is necessary to make sure that trading operation has not really succeeded (a new position has not been opened, or the existing order has not been modified or deleted, or the existing position has not been closed)

 


Hi RaptorUK,

Thank you for the clue.

Another issue (more one.....);

 Say that on ECN Broker one trade is open and per " Requirements and Limitations in Making Trades " a Market order can not be closed if Stoploss in inside of Freezing distance, in that case will this CloseAll function  close even those orders that are inside the Freezing Distance ? 

Best Regards

Luis

void CloseAllOnSL()
 {
   int OrdType, GLError;
   double OrderClosed;   
        RefreshRates(); 

   int LastClosedTicket=GetTicketFromHistory(Symbol(),MagicNumber);  
   if(LastClosedTicket>0)                                          
   {//28                                                  
      Print("LastClosedTicket=",LastClosedTicket);                   
      if(OrderSelect(LastClosedTicket,SELECT_BY_TICKET))             
      {//29        
         if(MathAbs(OrderTakeProfit()-OrderClosePrice())>            
            MathAbs(OrderStopLoss()-OrderClosePrice()))              
            {//30        
            Print("Order with ticketnr: ", LastClosedTicket,
                  " hit SL! Close all open orders");

      for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--)       
         if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES)
            && OrderMagicNumber()== MagicNumber 
            && OrderSymbol()== Symbol())                                       
            {//31
            OrdType = OrderType();
            if(OrdType == OP_BUY || OrdType==OP_SELL)
              {//32
              if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),RealSlippage, Yellow))
                  GLError = GetLastError();
              }//32               
             }//31      
         }//28 
      }//29     
   }//30 
 
  if(GLError > 0) Print("Error Closing/Deleting Order, error # ", GLError, " for ticket: ", OrderTicket());           
}
 
luisneves:


Hi RaptorUK,

Thank you for the clue.

Another issue (more one.....);

 Say that on ECN Broker one trade is open and per " Requirements and Limitations in Making Trades " a Market order can not be closed if Stoploss in inside of Freezing distance, in that case will this CloseAll function  close even those orders that are inside the Freezing Distance ?  

No,  it will fail,  probably error 130
 

Hi RaptorUK,

If possible would like to ask you for some help in this issue;

This piece of code find  last OrderLots() and multiples it by the multiplier factor, but some times it passes this factor and I have orders in this way; 0.01, 196,83, 590,49, etc.

 Must to say that I've a a limit to open orders, in this case 5. Now, the way I am seen this logic even if comes a condition to open an order above 5 it will not open and then the number of orders in the pool are limited to 5 so, if the code only see 5 orders in the pool the lot size for those orders should be ; 0.01, 0.02, 0.04, 0.08, 0.16. Is anything wrong in this code that I'm not aware about ?

(hope indenting is fine...) 

for(int cnt = 0; cnt < OrdersTotal(); cnt++)
   {//11
 if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
   {//12
 if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
   {//13
     LastLot = OrderLots();
   }//13              
     MLots = 0;                       
     MLots = LastLot * Multiplier;        
   }//12
 else MLots = LotSize;
   }//11 
 return(0);
 }//0 

 Best regards

Luis 

 
luisneves:

Hi RaptorUK,

If possible would like to ask you for some help in this issue;

This piece of code find  last OrderLots() and multiples it by the multiplier factor, but some times it passes this factor and I have orders in this way; 0.01, 196,83, 590,49, etc.

 Must to say that I've a a limit to open orders, in this case 5. Now, the way I am seen this logic even if comes a condition to open an order above 5 it will not open and then the number of orders in the pool are limited to 5 so, if the code only see 5 orders in the pool the lot size for those orders should be ; 0.01, 0.02, 0.04, 0.08, 0.16. Is anything wrong in this code that I'm not aware about ?

(hope indenting is fine...) 

I do it like this . . . 

for(int cnt = 0; cnt < OrdersTotal(); cnt++)
   {//11
   if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
      {//12
      if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
         {//13
         LastLot = OrderLots();
         }//13              
      MLots = 0;                       
      MLots = LastLot * Multiplier;        
      }//12
   else MLots = LotSize;
   }//11 
return(0);
}//0 

if the OrderSelect() fails   MLots = LotSize  ??  why ?   your indenting doesn't help you to see what is happening with your if statements and braces . . .  did you mean to do this ?

for(int cnt = 0; cnt < OrdersTotal(); cnt++)
   {//11
   if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
      {//12
      if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
         {//13
         LastLot = OrderLots();
         MLots = 0;                       
         MLots = LastLot * Multiplier; 
         }//13              
      }//12
   }//11 
if(MLots < 0.001) MLots = LotSize;  //  if MLots == 0.0
return(0);
}//0 

I did this . . .

if(MLots < 0.001)

  . .  as a quick and dirty method to avoid double comparison issues,  have you read this ? Can price != price ?

 
RaptorUK:

I do it like this . . . 

if the OrderSelect() fails   MLots = LotSize  ??  why ?   your indenting doesn't help you to see what is happening with your if statements and braces . . .  did you mean to do this ?

I did this . . .

  . .  as a quick and dirty method to avoid double comparison issues,  have you read this ? Can price != price ?


Hi RaptorUk,

Thank you for your prompt response.

So, I guess that's better to just do not make use  of else because that is there for nothing. And thank you for  the link provided.

Best egards

Luis 

 

Hi RaptorUK,

I've one issue (more one.....)

Some orders close before time, what I mean is;

 These orders have been open and there's  a limit to open up to 6. All orders should close by mean of TrailingStop, but orders 1. 3 and 5 have been closed before the others that have been close after (07:43).

Because of these 3 orders have close before than the others  give space to open orders 7, 8 and 9 and these ones open with a lot based in last lot (2.43).

So, as far I understood those of erratic close of orders at 07:23 shouldn't been happened and no errors  have been found...

1 - 49518192013.01.28 19:51sell0.01eurusdi1.345471.350471.343472013.01.29 07:231.34347-0.060.000.001.49
2 - 49520362013.01.28 20:04buy0.03eurusdi1.345791.340791.347792013.01.29 07:431.34421-0.170.00-0.04-3.53
3 - 49520742013.01.28 20:12sell0.09eurusdi1.345481.350481.343482013.01.29 07:231.34348-0.500.000.0013.40
4 - 49520932013.01.28 20:20buy0.27eurusdi1.345791.340791.347792013.01.29 07:431.34421-1.490.00-0.38-31.74
5 - 49521102013.01.28 20:27sell0.81eurusdi1.345481.350481.343482013.01.29 07:231.34348-4.460.000.00120.58
6 - 49521502013.01.28 20:48buy2.43eurusdi1.345791.340791.347792013.01.29 07:431.34421-13.370.00-3.43-285.63
7 -  49593372013.01.29 07:23sell7.29eurusdi1.343511.348511.341512013.01.29 07:431.34424-40.100.000.00-395.89
8  - 49598582013.01.29 07:40buy21.87eurusdi1.343821.344221.345822013.01.29 07:431.34422-120.290.000.00650.79
9 -  49598602013.01.29 07:40sell65.61eurusdi1.343821.348821.341822013.01.29 07:431.34424-360.860.000.00-2 049.95

 

The code to close those all by mean of TrailingStop is this one;

Could this code fail and close orders at different times ? 

void CloseAllOnSL()
 {
   int OrdType, GLError;
   double OrderClosed;
        
   int LastClosedTicket=GetTicketFromHistory(Symbol(),MagicNumber);  
   if(LastClosedTicket > 0 )                                          
   {//28                                                  
      Print("LastClosedTicket=",LastClosedTicket);                   
      if(OrderSelect(LastClosedTicket,SELECT_BY_TICKET))             
      {//29        
         if(MathAbs(OrderTakeProfit()- OrderClosePrice())>            
            MathAbs(OrderStopLoss()- OrderClosePrice()))             
            {//30        
            Print("Order with ticketnr: ", LastClosedTicket,
                  " hit SL! Close all open orders");

      for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--)       
         if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES)
            && OrderMagicNumber()== MagicNumber 
            && OrderSymbol()== Symbol())                                       
            {//31
            OrdType = OrderType();
            if(OrdType == OP_BUY || OrdType==OP_SELL)
              {//32
              while(IsTradeContextBusy()) Sleep(SleepTime);  
                   RefreshRates();
              if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),RealSlippage, Yellow))                
                 GLError = GetLastError();
              }//32               
             }//31      
         }//28 
      }//29     
   }//30 

 Best regard's

Luis 

 
luisneves:


 The code to close those all by mean of TrailingStop is this one;

Could this code fail and close orders at different times ? 

OK,  this makes no sense . . .  a trailing SL is set so that if price goes with an Order the SL is moved towards price so if the new SL is hit there will be less loss or even a profit.  Why have a trailing SL if you are going to OrderClose() the trade anyway ?  to me that is not a trailing SL,  it is not a SL of any kind as you are actively closing the trade via the EA not the SL.

Maybe your CloseAllOnSL()  function does something different from what I think it does but,  yet gain,  you have no comments explaining what your code is trying to do.   There is no trailing Sl as there is no OrderModify().

 

Read this:  What are Function return values ? How do I use them ?

 

if(OrdType == OP_BUY || OrdType==OP_SELL)
              {//32
              while(IsTradeContextBusy()) Sleep(SleepTime);  
                   RefreshRates();
              if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),RealSlippage, Yellow))                
                 {
                 GLError = GetLastError();

                 //  ? ? ? ? ?  why aren't you printing the information about the OrderClose() failure ? ? 
                 Print("OrderClose failed, error# ", GLError);  //<-----  like this  but more info
                 } 
              }//32 

 add more information to the Print statement,  add OrderTicket(), OrderLots(), OrderClosePrice(), Bid, Ask, etc, etc  any information you need to determine what the problem was when the error occurred,  yo can't easily go back in time and find all this out,  so when it happens print it to the log.

 
luisneves:


So, as far I understood those of erratic close of orders at 07:23 shouldn't been happened and no errors  have been found...

1 - 49518192013.01.28 19:51sell0.01eurusdi1.345471.350471.343472013.01.29 07:231.34347-0.060.000.001.49
2 - 49520362013.01.28 20:04buy0.03eurusdi1.345791.340791.347792013.01.29 07:431.34421-0.170.00-0.04-3.53
3 - 49520742013.01.28 20:12sell0.09eurusdi1.345481.350481.343482013.01.29 07:231.34348-0.500.000.0013.40
4 - 49520932013.01.28 20:20buy0.27eurusdi1.345791.340791.347792013.01.29 07:431.34421-1.490.00-0.38-31.74
5 - 49521102013.01.28 20:27sell0.81eurusdi1.345481.350481.343482013.01.29 07:231.34348-4.460.000.00120.58
6 - 49521502013.01.28 20:48buy2.43eurusdi1.345791.340791.347792013.01.29 07:431.34421-13.370.00-3.43-285.63
7 -  49593372013.01.29 07:23sell7.29eurusdi1.343511.348511.341512013.01.29 07:431.34424-40.100.000.00-395.89
8  - 49598582013.01.29 07:40buy21.87eurusdi1.343821.344221.345822013.01.29 07:431.34422-120.290.000.00650.79
9 -  49598602013.01.29 07:40sell65.61eurusdi1.343821.348821.341822013.01.29 07:431.34424-360.860.000.00-2 049.95

 

Show the output from the log when this happened . . .
 
RaptorUK:

Read this:  What are Function return values ? How do I use them ?

 

 add more information to the Print statement,  add OrderTicket(), OrderLots(), OrderClosePrice(), Bid, Ask, etc, etc  any information you need to determine what the problem was when the error occurred,  yo can't easily go back in time and find all this out,  so when it happens print it to the log.

 


Hi RaptorUk,

Thanks you for your time.

The code for TrailingStop is in another part, the code sent is only to close all once order have been close by mean of TrailingStop.

Nevertheless I go after your advise and look for any error that could give more additional information why orders close before others.

By the way, in following code makes sense to change position of MLots = 0; from the the actual place to before the for loop ?

 

   MLots = 0; <-----------------------------------------------------------------to here 
   for(int cnt = 0; cnt < OrdersTotal(); cnt++)
      {//11
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         {//12
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
            {//13
            LastLot = OrderLots();
            }//13            
         MLots = 0;  <-----------------------------------------------------------from here                      
         MLots = LastLot * Multiplier;        
         }//12      
      }//11 
   return(0);
   }//0 

 Best regards

Luis 

Reason: