Compose variables to assign value to them

 
Hello,

In an EA that I have in development, there are cycles of operations that I need, from each one of them, to store a series of variables for statistical purposes.
The number of cycles is indefinite.

To do this, I thought that variables could be created through programming or at least, assign values to them by iteratively constructing their name.

For example, you could have the following variables:

int Cycle1; 
double Cycle1Pips, Cycle1NumTrades, Cycle1Result ;

int Cycle2; 
double Cycle2Pips, Cycle2NumTrades, Cycle2Result ;

int Cycle2; 
double Cycle2Pips, Cycle2NumTrades, Cycle2Result ;

..........................

Is there any way to assign a value to a variable by accessing it from string concatenation?
For example, for Cycle2Pips=7:
     "Cycle "+ (string)Num+ "Pips" ? =7.

Is this possible?
Should it be done differently?

Thanks in advance,
Juan
 
  1. Please edit your post and place your sample code with the "</>" icon or Alt-S. It makes it easier to read your code. Don't just paste it as plain text.
  2. Do some research on structuresDocumentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
  3. Study the following code and see if it suites your needs (untested code, just typed):
// Define a structure for a "cycle"
   struct SCycle {
      double dbPips, dbResult;
      int    nTrades;
   };

// Declare a array for the "cycle" structure
   #define MCycles 3
   SCycle oCycle[ MCycles ];

// Do something with the array of "cycle"
   void SomeFunction( void ) {
      for( int i = 0; i < MCycles; i++ )
         PrintFormat( "Cycle: %d, Pips: %.1f, Trades: %d",
            i, oCycle[ i ].dbPips, oCycle[ i ].nTrades );
   };

 
febrero59: Is this possible?
Should it be done differently?

No. yes. Create an array of a struct.

struct Cycle{ int cycle, nTrades; double pips, result; };
Cycle saved[3];
saved[0].pips=7
 
Thank you very much.

With your indications I have understood how struct works, according to the following example development approaching my objectives.

   struct EstructuraCiclo
   {
      double   CicloPips;
      string   CicloMotivo;
      int      CicloTrades;

   }; 
  #define Num 3 
   EstructuraCiclo mVar[Num];

   int Contador=0;   

//+------------------------------------------------------------------+
void OnTick()
  {
     if(Contador<Num-1)
     { 
         mVar[Contador].CicloPips=0.1; 
         mVar[Contador].CicloMotivo="Buy"; 
         mVar[Contador].CicloTrades=Contador+1; 
        
         Contador+=1;
         
         mFuncion(); 
     }    
  }

//+------------------------------------------------------------------+
   void mFuncion() 
   {
      Print(TimeCurrent()); 
      for( int i = 0; i <= Contador; i++ )
      {
        Print( "Ciclo: ",i," | Pips: ",mVar[ i ].CicloPips," | Motivo: ",mVar[ i ].CicloMotivo, " | Trades: ", mVar[ i ].CicloTrades );
      }
 
   }



So far so good. 

But now, what can I do to increase the number of struct elements?

Thanks in advance,

Juan

 
febrero59 #:Thank you very much.With your indications I have understood how struct works, according to the following example development approaching my objectives. So far so good. But now, what can I do to increase the number of struct elements? Thanks in advance, Juan

Use a dynamic array of the structure instead of a static array — Documentation on MQL5: Language Basics / Data Types / Dynamic Array Object

A dynamic array can be resized to any any size at any time — Documentation on MQL5: Array Functions / ArrayResize

 

TThank you very much.

I have successfully implemented a data structure, with which I can continue with my current project.

I have been looking at dynamic arrays, but it is another level: I will read it again to be able to apply it, although I see that it is not a simple subject: The mql5 Manual, when you don't know anything about it, doesn't make things easy with simple examples.