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

 
trader781:

I know theoretically why this happened, so my question is, can we talk briefly about the use of arrays?

Where do you need to initialise, delete and process them to make them work?

P.S

when google banned it looks something like this:

What makes you think it's about an array?
 
Alexey Viktorov:
Why do you think it's because of the array?
Because this has never happened before, and if it has, I've made a mistake somewhere, which is why I'm coming here. You could do it yourself, but it takes a lot more time.
 
trader781:

need

1) create it somewhere (in a global area, as a function, in onit or ontic?)

2) write each ticket in it when sending an order, giving it a number in order

3) pull them out from there as needed through the number

4) When closing the whole grid, reset the parameters to zero

You don't need to keep it in memory all the time. When you need to find it, you need to call the function with a local array where you will put all the orders/positions in the loop, sort the array as you need, select the necessary orders from it, do whatever you need with them, and when you exit the function, you forget about that local array. And the next time you call the function again, it will sort out the orders/positions that will be there at the moment of such call, and do the same with them. And you don't need a global array that you have to constantly monitor and control.
 
Artyom Trishkin:
You don't need to keep it in the memory all the time. When you need to find it, you call the function with a local array inside of it where you will put all the orders/positions in the loop, sort the array as you need, select the necessary orders from it, do whatever you need with them, and when you exit the function, you forget about that local array. And the next time you call the function again, it will sort out the orders/positions that will be there at the moment of such call, and do the same with them. And you don't need a global array that you have to constantly watch and control.

I don't know how much cheaper. Each situation has its own solution. There is no OnTradeTransaction() event in MQL4, but we have to keep track of "our" orders somehow... This is where the global variable array comes in handy.

Just imagine that there are several Expert Advisors running on an account, one of which has heavy calculations, and the other one has a huge number of orders... How would it work with heavy calculations? Will this EA have to search a huge amount of orders? You can't do without search in general, but you should minimize this procedure.

And to determine whether the order is closed or not? Go through the entire history? Isn't it too expensive?

 
trader781:
I have never made this kind of mistake before, but if I have, I have made a mistake, that's why I am asking here. I could do it myself, but it takes a lot more time.

As you understood from my previous post, I use arrays quite often and have never had any problems with them. Once I wrote an EA that opened more than 2000 orders whose tickets were stored in an array and there were no problems at all. I cannot believe that the problems appear because of the array. I also cannot believe what error may have caused such problems.

I'm reminded of the film "It Can't Be"... "It's not beer that kills people, it's water that kills people"...

 
Alexey Viktorov:

I don't know how much cheaper. Each situation has its own solution. There is no OnTradeTransaction() event in MQL4, but we have to keep track of "our" orders somehow... This is where the global variable array comes in handy.

Just imagine that there are several Expert Advisors running on an account, one of which has heavy calculations, and the other one has a huge number of orders... How would it work with heavy calculations? Will this EA have to search a huge amount of orders? You can't do without search in general, but we should minimize this procedure.

How to check if the order was closed? Go through the entire history? Isn't it too expensive?

Everything can be solved.

For example, I have long ago created a class that monitors all this. You can create/delete class objects dynamically for each symbol, magik, timeframe, or all for one account or a combination of them - you can choose. Any necessary search cycle is already performed on a new tick once, and there is all necessary data. Of course, there are cases when it is impossible to do without one more cycle, but it is the second additional cycle per tick. And not having a separate loop in every function is a terrible waste of resources.

 
Artyom Trishkin:

Everything is solvable.

I, for example, have long ago made a class that takes care of all this. You can create/delete class objects dynamically for each symbol, magik, timeframe, or all for one account, or combinations of them - you can choose. Any necessary search cycle is already performed on a new tick once, and there is all necessary data. Of course, there are cases when it is impossible to do without one more cycle, but it is the second additional cycle per tick. And not having a separate loop in every function is a terrible waste of resources.

Well, no one argues with that. It became even more convenient with the advent of structures.
 
Artyom Trishkin:
You don't need to keep it in memory all the time. When you need to find it, you should call the function with a local array inside it where you will put all the orders/positions in the loop, sort the array as you need, select the necessary orders from it, do whatever you need with them, and when you exit the function, you forget about that local array. And the next time you call the function again, it will sort out the orders/positions that will be there at the moment of such call, and do the same thing with them again. And you don't need a global array that you have to constantly monitor and control.

OK, here's a simplified view.

If the array is local where do you want to put it? Obviously not in the ontik.

void OnTick()
{  

       if(FindLastOType()==OP_BUY)  //+------ если последний покупка
           {
            if(Ask<=FindLastOrderOpenPrice()-Step*Point())//+------------если ордер в минус
              {
               TotalClose();
              }
            else
            if(Ask>=FindLastOrderOpenPrice()+Step*Point())//+------------если ордер в плюс и это 5 ордер в списке
              {
               ticket=OrderSend(Symbol(),OP_BUY,(лот2 ордера+лот4 ордера),Ask,50,0,0,"",Magic,0,clrAzure);
              }        
           }  
}

 
trader781:

OK, here's a simplified view.

If the array is local where do you want to put it? Obviously not in the ontik.

void OnTick()
{  

       if(FindLastOType()==OP_BUY)  //+------ если последний покупка
           {
            if(Ask<=FindLastOrderOpenPrice()-Step*Point())//+------------если ордер в минус
              {
               TotalClose();
              }
            else
            if(Ask>=FindLastOrderOpenPrice()+Step*Point())//+------------если ордер в плюс и это 5 ордер в списке
              {
               ticket=OrderSend(Symbol(),OP_BUY,(лот2 ордера+лот4 ордера),Ask,50,0,0,"",Magic,0,clrAzure);
              }        
           }  
}

Please read what a function is. Then you will understand that the array declared in the function body will be local - out of sight of the rest of the program.
 
bool WriteToFile(int FileHandle,string DataToWrite)
  {
// Receives the number of bytes written to the file. Note that MQL can only pass
// arrays as by-reference parameters to DLLs
   int BytesWritten[1]={0};

// Get the length of the string
   int szData=StringLen(DataToWrite);

// Do the write
   WriteFile(FileHandle,DataToWrite,szData,BytesWritten,0);

// Return true if the number of bytes written matches the expected number
   return (BytesWritten[0] == szData);
  }

I want to write a line to the file with translations to a new line, but it doesn't work, this code is from herehttps://www.mql5.com/en/forum/118999

this code writes a line with spaces after each letter, I need a replacement for FileWrite() but it works

Reading and writing files anywhere on disk using CreateFileA() etc.
Reading and writing files anywhere on disk using CreateFileA() etc.
  • www.mql5.com
There are frequent questions in this forum (e.g...
Reason: