Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 15

 
Vitalie Postolache:
The cycles must be counted in reverse order, fromOrdersTotal()-1 to 0.
When closing in a loop.
 
Vitalie Postolache:
The loops need to count backwards, fromOrdersTotal()-1 to 0.
These are Kim's functions and they work without missing, the problem is something else
 
So you accidentally switched on several advisers on the same symbol, many people have done that.
 
Neron_76:
Hello! I have installed my forex robot on my demo account and it has been running for the second month. I am opening the terminal today and have two open orders by one signal, although I have placed only one order before. I have a restriction on the number of orders. What may be the reason?

Judging by the code, it looks like you have a pending order rather than a market order.

Restriction on the number of open orders only applies to market orders.

That is why there is no error and therefore no problem.

 
Renat Akhtyamov:

Judging by the code, it looks like you have a pending order rather than a market order.

The limit on the number of open orders only applies to market orders.

Therefore there is no error and therefore no problem.

This limitation is applied to open and pending orders. This is the second case I have encountered in a month and a half; all other cases are correct.
 
Neron_76:
The restriction applies to open and pending orders. This is the second time in a month and a half, all the other cases are as they should be.

Try it this way:

if(NumberOfPositions(Symb,-1,Magik_number)==0 && NumberOfOrders(Symb,-1,Magik_number)==0 && Delta_Buy<Low[1] && sar>Close[1]) {
  SetOrder(NULL,OP_BUYSTOP,Lts,sar,sar-SL*Point(),sar+TP*Point(),Magik_number);
  return(0);
}
if(NumberOfPositions(Symb,-1,Magik_number)==0 && NumberOfOrders(Symb,-1,Magik_number)==0 && Delta_Sell>High[1] && sar<Close[1]) {
  SetOrder(NULL,OP_SELLSTOP,Lts,sar,sar+SL*Point(),sar-TP*Point(),Magik_number);
  return(0);
}

//===============================================================================================
//------------------------------- Возвращает количество позиций --------------------------------+
//===============================================================================================
int NumberOfPositions(string sy="", int op=-1, int mn=-1) {
int kp=0;
  if(sy=="") sy=Symbol();
  for(int i=0; i<OrdersTotal(); i++) {
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
    if((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
     if(OrderType()==OP_BUY || OrderType()==OP_SELL) {
      if(mn<0 || OrderMagicNumber()==mn) kp++;
}}}}
  return(kp);
}
//===============================================================================================
//------------------------------- Возвращает количество ордеров --------------------------------+
//===============================================================================================
int NumberOfOrders(string sy="", int op=-1, int mn=-1) {
int kp=0;
  if(sy=="") sy=Symbol();
  for(int i=0; i<OrdersTotal(); i++) {
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
    if((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
     if(OrderType()>1 && OrderType()<6) {
      if(mn<0 || OrderMagicNumber()==mn) kp++;
}}}}
  return(kp);
}
 
Vitaly Muzichenko:

Try it this way:

if(NumberOfPositions(Symb,-1,Magik_number)==0 && NumberOfOrders(Symb,-1,Magik_number)==0 && Delta_Buy<Low[1] && sar>Close[1]) {
  SetOrder(NULL,OP_BUYSTOP,Lts,sar,sar-SL*Point(),sar+TP*Point(),Magik_number);
  return(0);
}
if(NumberOfPositions(Symb,-1,Magik_number)==0 && NumberOfOrders(Symb,-1,Magik_number)==0 && Delta_Sell>High[1] && sar<Close[1]) {
  SetOrder(NULL,OP_SELLSTOP,Lts,sar,sar+SL*Point(),sar-TP*Point(),Magik_number);
  return(0);
}

//===============================================================================================
//------------------------------- Возвращает количество позиций --------------------------------+
//===============================================================================================
int NumberOfPositions(string sy="", int op=-1, int mn=-1) {
int kp=0;
  if(sy=="") sy=Symbol();
  for(int i=0; i<OrdersTotal(); i++) {
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
    if((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
     if(OrderType()==OP_BUY || OrderType()==OP_SELL) {
      if(mn<0 || OrderMagicNumber()==mn) kp++;
}}}}
  return(kp);
}
//===============================================================================================
//------------------------------- Возвращает количество ордеров --------------------------------+
//===============================================================================================
int NumberOfOrders(string sy="", int op=-1, int mn=-1) {
int kp=0;
  if(sy=="") sy=Symbol();
  for(int i=0; i<OrdersTotal(); i++) {
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
    if((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
     if(OrderType()>1 && OrderType()<6) {
      if(mn<0 || OrderMagicNumber()==mn) kp++;
}}}}
  return(kp);
}
I replaced the functions, it works fine on the test. Let's see how it will be in trading on the demo. But for some reason, it seems to me, in my case, both these and these functions perform the same task, i.e. answer the question of whether there are orders and positions.
 
Neron_76:
The functions have been replaced, it works fine in the test. Let's see how it will be in trading on the demo. But for some reason it seems to me that in my case both functions perform the same task, i.e. they answer the question whether there are orders and positions.
If I repeat the double setting of the order, then the"SetOrder" function should be reconsidered, perhaps it gives two requests, after the first request there is no exit from the function, and it has time to send the second one.
 
Vitaly Muzichenko:
If double setting of the order is repeated, then the"SetOrder" function should be reviewed, perhaps it gives two requests, after the first request there is no exit from the function, and manages to send the second one.
If this is the case, why does this not always happen, something happens between ticks? I'm not very good at this.
 
Neron_76:
If this is the case, why does it not always happen, something between tics? I'm not very good at this.
Between ticks of one symbol there may be ticks of another symbol. If the EA is multi-currency, it sees ticks from another symbol. Also, timer events - OnTimer() and OnChartEvent() events may occur between ticks - I almost wrote "event events"... would be a setup...