Help me learn how to program.

 

How do I collect and use information about trades? I need to collect information about all EA positions, their volume, profit, type, opening price. And then refer to information about the most recent position, or the largest position. I find it difficult to write in arrays.

1. Is it necessary to deal with arrays at all?

2. is it better to use an array of structures or different arrays?

3. Why do I need a dynamic array if I have to allocate memory anyway? Isn't it easier to use a static array? Or can't we do without a dynamic one, because it can be reverse-indexed?


Example code without oop, because I couldn't even write this construct, if you don't mind, thanks.

 
pribludilsa:

How do I collect and use information about trades? I need to collect information about all EA positions, their volume, profit, type, opening price. And then refer to information about the most recent position, or the largest position. I find it difficult to write in arrays.

1. Is it necessary to deal with arrays at all?

2. is it better to use an array of structures or different arrays?

3. Why do I need a dynamic array if I have to allocate memory anyway? Isn't it easier to use a static array? Or can't we do without a dynamic one, because it can be reverse-indexed?


Example code without oop, because I couldn't even write this construct, if it's not too much trouble, thanks.

Why build anything at all? You need to loop through the positions and process the information - and you don't need to assemble it. Anything you collect at the same moment is obsolete information.

 

1. When dealing with trade history, arrays are usually dispensed with. This is roughly as in the example:https://www.mql5.com/ru/docs/trading/historyselect. It's worth exploring all the functions in the column on the left hand side of the link (and all the examples there).

2. An array of structures is very handy. But the built-in functions that can be applied to multidimensional arrays - ArraySort() etc. - do not work with it.

3. If you are sure that the initial specified array size is enough, you can use static arrays as well. If their size is reasonable.

 
Vladimir Karputov:

Why collect anything at all? You need to cycle through the items and process the information - and you don't need to collect it. Anything you collect at the same time is obsolete information.

I'm trying to write a grid without an oop. So I decided to practice for a start, and I can't seem to construct an entry into the array. Or maybe I don't need it? What's the best way to do it? Well, I was thinking in a loop. That's how I started.
int OnInit()
{
ENUM_POSITIONS_TYPE ar [1000]; //проинициализирую нулем еще, просто я не копирую, а вручную всё пишу, и некоторые очевидные куски не пишу.
 return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{}
void OnTick()
{
for(int index=PositionsTotal()-1; index>=0; index--)
{
ulong ticket=PositionGetTicket(index);
PositionSelectByTicket(ticket); // И вот я готов вызывать функции для выуживания данных, только я не знаю //как их вписывать в массив. 
 
Dmitry Fedoseev:

1. When dealing with trade history, arrays are usually dispensed with. This is roughly as in the example:https://www.mql5.com/ru/docs/trading/historyselect. It's worth exploring all the functions in the column on the left hand side of the link (and all the examples there).

2. An array of structures is very handy. But the built-in functions that can be applied to multidimensional arrays - ArraySort() etc. - do not work with it.

3. If you are sure that the initial specified array size is enough, you can use static arrays as well. If their size is reasonable.

It's not me with the story, it's the positions I want to analyse that are open. What is the best way to do it?
 
I am writing a grid without an OOP. I need to find out which of the open positions is the biggest, and what type of deal it is, buy or sell. I understand that I need to first collect data from each position in the loop, write it into an array, then analyze the array, or take the most recent element, if it is dynamic with reverse indexation.
 
pribludilsa:
It's not with history, I want to analyze opened positions. What is the best way to do it?

Almost the same, but without HistorySelect(). We start with the PositionsToatal() function and then by that link we study all functions beginning with Position

 
pribludilsa:
I write a grid without OOP. I need to know which of the open positions is the largest, and it is by which type of deals, buy or sell. I understand that I should first collect data from each position in the loop, write it into an array and then analyze the array or take the newest element if it is dynamic with reverse indexation.

You don't have to array the positions and look for the highs and lows as you go along.

 
Dmitry Fedoseev:

You don't need to go through the array and look for highs and lows as you go along.


Thank you. But can you give us a short example? We need to write it down to compare it, don't we? And write it down where else but in an array? I can't figure out how to do it without writing. We are working with one position in a loop pass, how can we compare another position if it's in another loop pass?

 
pribludilsa:

Thank you. Can you just give us a short example? We need to write it down to compare, don't we? And write it where but in an array? I can't figure out how to do it without writing. We are working with one position in a loop pass, how can we compare another position if it's in another loop pass?

int Magic=123;

double maxBuyPrice=0;
ulong maxBuyTicket=0;
double minSellPrice=DBL_MAX;
ulong minSellTicket=0;   

for(int i=0;i<PositionsTotal();i++){
   ulong ticket=PositionGetTicket(i);
   if(ticket!=0){
      long magic=PositionGetInteger(POSITION_MAGIC);
      string symbol=PositionGetString(POSITION_SYMBOL);
      if(magic==Magic && symbol==Symbol()){
         long type=PositionGetInteger(POSITION_TYPE);
         if(type==POSITION_TYPE_BUY){
            if(PositionGetDouble(POSITION_PRICE_OPEN)>maxBuyPrice){
               maxBuyPrice=PositionGetDouble(POSITION_PRICE_OPEN);
               maxBuyTicket=ticket; // тут можно и другие данные позиции запоминать в переменные
            }
         }
         else if(type==POSITION_TYPE_SELL){
            if(PositionGetDouble(POSITION_PRICE_OPEN)<minSellPrice){
               minSellPrice=PositionGetDouble(POSITION_PRICE_OPEN);
               minSellTicket=ticket; // и тут
            }      
         }
      }
   }
}

if(maxBuyTicket!=0){
   
}
if(minSellTicket!=0){

}
 
Dmitry Fedoseev:

Thanks for that example.

      long magic=PositionGetInteger(POSITION_MAGIC);
      string symbol=PositionGetString(POSITION_SYMBOL);
      if(magic==Magic && symbol==Symbol())

I thought I was the only one who preferred to put it that way, rather than writing "I don't know".

      if(PositionGetInteger(POSITION_MAGIC)==Magic && PositionGetString(POSITION_SYMBOL)==Symbol())

hz means "I'd like to know"... don't get me wrong...


ps; It helps to keep track of everything during program debugging.

Reason: