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

 
trader781:

rewrote post 844 to avoid having to write the code here again

As for the zero-sized array - 0 can be an integer, it can be the equivalent of a boolean falsifier, it can be a void, and it can be the starting point.

It doesn't matter what you mean by zero. If you size an array with zero, call it zero gingerbread and it will be zero. Which means the array will be zero.
 
trader781:

rewrote post 844 to avoid having to write the code here again

As for the zero-sized array - 0 could be an integer, could be the equivalent of a boolean falsifier, could be a void, and could be a starting point.

And please don't make me insert the right code in the right post for you:

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property strict
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
struct myorder
  {
   int               Ticket;
   double            orderopenprice;
   int               ordertype;
   double            profit;
   double            stoploss;
   double            lot;
  };
myorder orders[];

int    i;
int    Magic=444;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   CalcOrders();

  }
//+------------------------------------------------------------------+
void CalcOrders()
  {
   int count1=0;

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

  }
//+------------------------------------------------------------------+

Now look at what array you're filling in and what field you're sorting it by. And most importantly - why do you need this sorting? What is the point of it?

 
Artyom Trishkin:

And please don't make me paste the right code into the right message for you:

А теперь поглядите какой массив вы заполняете и по какому полю его сортируете. И, главное - зачем нужна эта сортировка? Какой в ней смысл?

sort the array of orders, in ascending order from zero value

for example if we have 20 orders and 18 of them quite accidentally went up by a couple of points and I closed it immediately with my hands

 
trader781:

sort the array of orders, in ascending order from zero value

For example, if we have 20 orders and 18 of them absolutely accidentally went up by a couple of points, and I closed them right away with my hands.

If you absolutely need sorting by ticket, you will have to make 2 arrays. One is an array of structures and the second is a two-dimensional array that contains the order ticket in the first dimension, and in the second the counter index, the index under which the order information is stored in the array of structures. And fill these arrays at the same time in one loop.

You have been trying to solve this question for so long, that I don't even remember the purpose you were trying to reach. And probably all the others.

If I remember correctly, you need to select a certain number from the list of orders. So what's the problem with that? Just make a simple one-dimensional array, sort the tickets, and then orderSelect(array[required], SELECT_BY_TICKET) and that's it... then do what you want with this order.

 
Alexey Viktorov:

If you absolutely need to sort by ticket, you have to make two arrays. One is a structure array and the second is a two-dimensional array that contains the order ticket in the first dimension and the counter index in the second, the index under which the order information is stored in the structure array. And fill these arrays at the same time in one loop.

You have been trying to solve this question for so long, that I don't even remember the purpose you were trying to reach. And probably all the others.

If I remember correctly, you need to select a certain number from the list of orders. So what's the problem with that? Just make a simple one-dimensional array, sort it, and then orderSelect(array[required], SELECT_BY_TICKET) and that's it... Then you can do whatever you want with this order.

I do not remember what I should do there either. But the good thing is that it does.

I specifically ask him to write one thing at first, and then I ask him to look carefully at what he has written, so that he understands himself that it won't work this way - he needs another local array right in the function, which we will have to sort and then fill in the global array from the sorted one.

 
Alexey Viktorov:

If you absolutely need to sort by ticket, you have to make two arrays. One is a structure array and the second is a two-dimensional array, which contains the order ticket in the first dimension and the counter index in the second, the index under which the order information is stored in the structure array. And fill these arrays at the same time in one loop.

You have been trying to solve this question for so long, that I don't even remember the purpose you were trying to reach. And probably all the others.

If I remember correctly, you need to select a certain number from the list of orders. So what's the problem with that? Just make a simple one-dimensional array, sort the tickets, and then orderSelect(array[required], SELECT_BY_TICKET) and that's it... then you can do whatever you want with this order.

No, I understand that there are a lot of ways to do it differently and avoid arrays. Why do I need 2 if I will use one of the arrays?
 
trader781:
No, I understand that there are a lot of ways to do it differently and do without arrays. Why 2 if there is one of structures?

Because a simple array is sorted by values in the first array dimension, and how an array of structures is sorted I have no idea.

Interesting question though... Now I'm going to try sorting an array of structures.

 
Alexey Viktorov:

Because a simple array is sorted by values in the first array dimension, but how an array of structures is sorted I have no idea.

It's an interesting question though... Now I'll try sorting an array of structures.

I have no idea what it's for in the form of a structure at all. The mq program is written in a single file, the array is handled in a single scrolling scroll, and fits in a single screen. What is the point of a structure in this case, for sporting interest?
 
Alexey Viktorov:

Because a simple array is sorted by values in the first array dimension, but I have no idea how an array of structures is sorted.

Although the question is interesting... I am going to try sorting out an array of structures.

I have no idea what it is or what it's about

But now that the topic has been brought up, it's silly to throw it away. There are also arrays of classes.

 
Vitaly Muzichenko:
I have no idea why it is needed in the form of a structure at all. The mq program is written in one file, the array is handled in one scrolling scroll, and fits in one screen. What is the point of a structure in this case, for sporting interest?

We write everything for the sake of sport. First we understand what works and how it works, and then we start thinking about how to write it more effectively.


As for sorting the structure array: as I expected, the experiment failed. Already at compile time the compiler said"expression has no effect"

Reason: