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

 

That's it, I've found my mistake. Some people need to voice their question in order to understand later how they got it wrong.

Unfortunately, apparently, I am one of those characters. (((

 
Konstantin Nikitin:

OrderSend
OrderSelect

I assume you mean byticket number?

Any pitfalls?

 
Decromor:

Greetings!


An order can be accessed using the OrderSelect command by its number.

Question, is it possible to know the numbers of existing orders in advance, so as not to go through them all? For example, we can memorize this number when opening a new order.

I understand that this number is assigned in order starting from 1. What are the possible nuances?

Thank you beforehand.

  1. We may address this order by its index in the list of orders (when trying it in the loop), or by its ticket (if it is known in advance).
  2. Not desirable - in case of any failure (restart of the Expert Advisor, restart of the terminal, system failure, etc.), the memorized tickets may be lost. So, they must be stored either in global variables of the terminal, or in a file, which is feasible, but more difficult than the usual search, which is not done on every tick, but only when needed.
  3. No. Starting with number 1, and in ascending order - only in the tester.
  4. You're welcome.
 
Artyom Trishkin:
  1. You can refer to it by its index in the list of orders (when searching in the loop), or by its ticket (if it is known in advance)
  2. Not desirable - in case of any failure (restart of the Expert Advisor, restart of the terminal, system failure, etc.), the memorized tickets may be lost. So, they must be stored either in global variables of the terminal, or in a file, which is feasible, but more difficult than the usual search, which is not done on every tick, but only when needed.
  3. No. Starting with number 1, and in ascending order - only in the tester.
  4. Please.

Is it possible to run in the code OnInit() in the loop OrdersTotal() and gather all of the information on open orders, in particular OrderTicket()?

string orders[][3];

int OnInit() {
   for (int n = OrdersTotal() + 1; n >= 0; n--) {              
      if (OrderSelect(n, SELECT_BY_POS, MODE_TRADES)) {
         int i = (ArraySize(orders) / 3);
         ArrayResize(orders, i + 1);
         orders[i][0] = IntegerToString(OrderTicket());
         orders[i][1] = IntegerToString(OrderType());
         orders[i][2] = DoubleToString(OrderOpenPrice(), Digits);   
      } 
   }
   return(INIT_SUCCEEDED);
}


When an order is automatically closed by TP or SP, is it possible to obtain the data of this order?

 
Decromor:

Is it possible to run the program in the OnInit() body and collect all the information on open orders, in particular OrderTicket(), in the OrdersTotal() loop?


When an order is closed automatically by TP or SP, is it possible to obtain the data of this order?

  1. Yes, we can. And then what? The orders may change over time, and the information collected during the initialization is outdated.
  2. We must find the last closed order and obtain all the necessary information about it.
 
Artyom Trishkin:
  1. You can. And then what? Orders may change with time, and the information on them, gathered during initialization, will become outdated.
  2. Need to find the last closed order and get all the information needed on it.

During BUY, SELL I save the required data into an array and then I roll this array and depending on the price I make the required changes (transfer SL to Breakeven, close order after the reverse, etc.), addressing to the required order via the ticket.

But I have orders with TP and SL which may close without my knowledge. I don't like the option of looking up the last closed order every tick and searching for it in my array to correct it.

"Orders may change over time, and the information collected on them during initialization will become outdated." I assume this is what is meant - closing the order?

The only option I see is to loop 2 loops in a tick, mine with orders andOrdersTotal() and I don't like it either.

"But more complicated than the usual enumeration, which is not done on every tick, but only as needed" Please share this algorithm.

 
Decromor:

Is it possible to run the program in the OnInit() body and collect all the information on open orders, in particular OrderTicket(), in the OrdersTotal() loop?


When an order is automatically closed by TP or SP, is it possible to obtain the data of this order?

And why do we need to convert all of this into a string? Is it a string type?

 
Decromor:

Is it possible to run the program in the OnInit() body and collect all the information on open orders, in particular OrderTicket(), in the OrdersTotal() loop?


When an order is automatically closed by TP or SP, is it possible to obtain the data of this order?

struct SOrders
{
     int ticket,
         type;
     double price;
} orders[];

int OnInit() {
   for (int n = 0; n < OrdersTotal(); n++)
   {              
      if(!OrderSelect(n, SELECT_BY_POS, MODE_TRADES)) continue;
      int i = ArraySize(orders);
      if(ArrayResize(orders, i+1) < i+1) continue;
      orders[i].ticket = OrderTicket();
      orders[i].type   = OrderType();
      orders[i].price  = OrderOpenPrice();
   }
   return(INIT_SUCCEEDED);
}
 
Konstantin Nikitin:
Thank you very much for the information, this is new to me.
 
Alexey Viktorov:

Why translate all this into a string? String type?

Because, for example, a ticket has type Int, price Double, and an array can only be one type.