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

 
Alexey Viktorov:
For example here https://docs.mql4.com/ru/basis/preprosessor/compilation
Thank you!
 
trader781:

In the current example, I want to be able to place an infinite number of orders with any lots in order not to exceed its limits.

Of course, we all know the limits, but I want to do it this way

I proceed from the fact that if you specify an element in brackets, it will be the last one

So I got this kind of crap. I was expecting an order by order... lot? I want the result (lot of such an order *coefficient) to be added to the order instead of the lot

and the question of retrieving data from there

I want to get the following result as I see it

MyArray[0][0.01]

MyArray[1][0.01]

MyArray[2][0.02]

etc...

So what's the actual question?

You wrote an array with lots, then sort it by lot size, it will sort by opening numbers, because in your case, each next position opens with a larger lot

double BPosMass[];
double SPosMass[];

void OnTick()
{
// Заполняем массивы
int b=-1,s=-1; // Объявим переменные с минусом
  for(int i=0; i<OrdersTotal(); i++) {
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
    if(OrderSymbol()==Symbol() && (OrderMagicNumber()==Magic || Magic<0)) {
     if(OrderType()==OP_BUY) {
      b++;
       ArrayResize(BPosMass,b+1);
       BPosMass[b]= OrderLot();
     }
     if(OrderType()==OP_SELL) {
      s++;
       ArrayResize(SPosMass,s+1);
       SPosMass[s]= OrderLot();
     }
  }}} // конец записи массив

// Читаем отсортированный массив с лотами
// Buy
  if(b>0) { // Если он не пустой и больше двух элементов - [0], иначе будет ошибка: "Выход за пределы массива"
    ArraySort(BPosMass, WHOLE_ARRAY, 0, MODE_ASCEND); // Отсортируем по размеру лота
    // Работа с полученными данными
    Comment("Самый старый Buy лот: ",    BPosMass[0],
            "\nПоследний Buy лот: ",     BPosMass[b],
            "\nПредпоследний Buy лот: ", BPosMass[b-1]
           );

  } // end Buy

// Sell
  if(s>0) { // Если он не пустой и больше двух элементов - [0], иначе будет ошибка: "Выход за пределы массива"
    ArraySort(SPosMass, WHOLE_ARRAY, 0, MODE_ASCEND); // Отсортируем по размеру лота
    // Работа с полученными данными
    Comment("Самый старый Sell лот: ",    SPosMass[0],
            "\nПоследний Sell лот: ",     SPosMass[s],
            "\nПредпоследний Sell лот: ", SPosMass[s-1]
           );

  } // end Sell
  
// Конец функции OnTick()
}


Then apply as follows: count the total number of positions, and if less than three, then do not access the array, if more, then read the array and take data from it.

The output of the array is not at the moment of writing, but at the moment of reading.

 
Vitaly Muzichenko:


The output of the array does not occur at the time of writing, but at the time of reading.

Nope. At the moment of accessing a non-existent array index.
 
Vitalie Postolache:
Nope. At the moment of accessing a non-existent array index.
So, what did I write?
 
Vitaly Muzichenko:
So what did I write?

2017.01.1312:51:14.3722017.01.0516:30:00 martin GBPUSD,M5: MyArray[CountOrders][0]0.02
2017.01.1312:51:14.3722017.01.0516:25:00 martin GBPUSD,M5: MyArray[CountOrders][0]0.01

CountOrders constant and the lot changes. It shouldn't be like this, but it doesn't work any other way

Ideally, the entryMyArray[CountOrders][OrderLots()]

so the output would beMyArray[0][0.01]MyArray[1][0.01]

but that won't work in µl.

and then somehow extract it from the function to the stream
 
trader781:

2017.01.1312:51:14.3722017.01.0516:30:00 martin GBPUSD,M5: MyArray[CountOrders][0]0.02
2017.01.1312:51:14.3722017.01.0516:25:00 martin GBPUSD,M5: MyArray[CountOrders][0]0.01

CountOrders constant and the lot changes. It shouldn't be like this, but it doesn't work any other way

Ideally, the entryMyArray[CountOrders][OrderLots()]

so the output would beMyArray[0][0.01]MyArray[1][0.01]

But that doesn't work in µl

Structures will help you. You create a structure with necessary fields, declare an array of such structures, fill it with order data in the loop by opening time, and then look through it for everything you need. Check the fields of the structure by the index and compare them with the necessary value. The index will indicate the order number by its opening time, and all the necessary data on this specific order will be shown in the fields of the structure.
 
Artyom Trishkin:
Structures will help you. You create a structure with necessary fields, declare an array of such structures, fill it with order data in the loop by opening time, and then search everything you need in it. Check the fields of the structure by the index and compare them with the necessary value. The index will indicate the order number by its opening time, and all the necessary data on this specific order will be given in the fields of the structure.

This is the kind of shit we've got

struct myorder
{
int    Ticket;
double orderopenprice;
int   ordertype;
double profit;
double stoploss;
double  lot;
};

myorder orders[];

int i;
void CalcOrders()
{
for(i=OrdersTotal()-1; i>=0; i--)
     {
      if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) && (OrderSymbol()==Symbol())
         && (OrderMagicNumber()==Magic) && (OrderType()<2))
         orders[i].Ticket=OrderTicket();
         orders[i].lot=OrderLots();
         orders[i].orderopenprice=OrderOpenPrice();
         orders[i].ordertype=OrderType();
         orders[i].profit=OrderProfit();
         orders[i].stoploss=OrderStopLoss();
     }
}    


I want to take out, for example, lot 5 of an order and compare it with the lot 3

sum the opening price and divide by the number of positions

we need a command entry for this kind of thing.

 
trader781:

We've got this kind of shit.

struct myorder
{
int    Ticket;
double orderopenprice;
int   ordertype;
double profit;
double stoploss;
double  lot;
};

myorder orders[];

int i;
void CalcOrders()
{
for(i=OrdersTotal()-1; i>=0; i--)
     {
      if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) && (OrderSymbol()==Symbol())
         && (OrderMagicNumber()==Magic) && (OrderType()<2))
         orders[i].Ticket=OrderTicket();
         orders[i].lot=OrderLots();
         orders[i].orderopenprice=OrderOpenPrice();
         orders[i].ordertype=OrderType();
         orders[i].profit=OrderProfit();
         orders[i].stoploss=OrderStopLoss();
     }
}    


I want to take out for example lot 5 of an order and compare it with lot 3

sum the opening price and divide by the number of positions

I need an entry itself in the form of commands for this kind of thing

Since the loop index i refers to any order, not only the ones you need, you should declare a variable (for example, n=0;) in the function that will be in charge of the array size. After passing the checks, increase the value of this variable inside the loop, increase the array size by the value of this variable and fill out the structure fields according to the index n-1: orders[n-1].xxx=XXX;

 

Please tell me why the OrdersTotal() function will write -1 when searching for orders.

Example: for (i=OrdersTotal()-1 ;i>=0; i--)

why not just OrdersTotal()?

Does the count of orders in this function start from 0 or 1? That is, if there is one order, is OrdersTotal() equal to 0 or 1?

 
Artyom Trishkin:

Since the loop index i refers to any orders, not only the ones you need, you should declare a variable (e.g. n=0;) in the function which will be responsible for the array size. After passing the checks, increase the value of this variable inside the loop, increase the array size by the value of this variable and fill out the structure fields by the index n-1: orders[n-1].xxx=XXX;

Since we have a pack of orders, why can't we just pull out the right order? (the necessary ones) and do what we want to do with them? e.g. order (i-4)
Reason: