Questions from Beginners MQL4 MT4 MetaTrader 4 - page 221

 

What is the best way to access a property of a class to another property of the same class?

That is, instead of "this", I would like to use something like "parent".

struct _Seeker {
        int holder;
        void Get(){holder = parent.prize;};
};
class Map {
        _Seeker seekerForPrize;
        int prize;
};
 

I used ArraySetAsSeries( array[], false) in indicator. In indicator I need to use iMa(...,i), it turns out indexing in another way. Can I also change the indexing order for iMa() as ArraySetAsSeries?

So far I did it like this: rates_total-i (count at the other end), but I'm not sure if this is the right solution?
ArraySetAsSeries(ExtMapBuffer4,Series);      
...
while(i<rates_total-1)
        {
         double maf=iMA(NULL,Used_Period,FAST_MA,0,Fmode,Fprice,i); // rates_total-i
         ExtMapBuffer4[i]=maf; // test 
...
 
Evgeny Potapov:

What is the best way to access a property of a class to another property of the same class?

That is, instead of "this", I would like to use something like "parent".

Learn the math)

class Map;

struct _Seeker {
        Map* parent;
        int holder;
        _Seeker(Map* _parent):parent(_parent){}
        void Get(){holder = parent.prize;};
};

class Map {
        _Seeker seekerForPrize;
public:
        int prize;
        Map():seekerForPrize(&this){}
};
 

Guys. Can you please give me the code to bind an indicator to an account number, and in addition to binding to an account, you could add a license key?

Форум трейдеров - MQL5.community
Форум трейдеров - MQL5.community
  • www.mql5.com
MQL5: форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий
 

A question, how do I refer to a structure element by substituting its name as a string?

struct My
   {
   datetime time1;
   datetime time2;
   datetime time3;
   datetime time4;
   } my;

datetime val=GetValue(my,"time3");


datetime GetValue(My &my, string name)
   {
   return(my."name"); ?????????
   }
Whoever can tell me, well done!
 
Nauris Zukas:

I used ArraySetAsSeries( array[], false) in indicator. In indicator I need to use iMa(...,i), it turns out indexing in another way. Can I also reverse the indexing order for iMa() as ArraySetAsSeries?

So far I did this: rates_total-i (count from the other end ), just not sure if this is the right solution?

Let me answer myself (maybe somebody will need it too). You can create (remake) data buffer by example include MovingAverages.mqh (SimpleMAOnBuffer) and don't even bother with ArraySetAsSeries, because it already has ArrayGetAsSeries in the example.

//+------------------------------------------------------------------+
//| Data on array                                              |
//+------------------------------------------------------------------+
int DataBuffer(const int rates_total,const int prev_calculated,const int begin,
               const double& data[],double& buffer[])
  {
   int i,limit;
//--- check for data
//--- save as_series flags
   bool as_series_data=ArrayGetAsSeries(data);
   bool as_series_buffer=ArrayGetAsSeries(buffer);
   if(as_series_data)
      ArraySetAsSeries(data,false);
   if(as_series_buffer)
      ArraySetAsSeries(buffer,false);
//--- first calculation or number of bars was changed
   if(prev_calculated==0) // first calculation
     {
      limit=begin+1;
      //--- set empty value for first bars
      for(i=0; i<limit-1; i++)
         buffer[i]=0.0;
      //--- calculate first visible value
      double firstValue=0;
      for(i=begin; i<limit; i++)
         firstValue=data[i];
      buffer[limit-1]=firstValue;
     }
   else
      limit=prev_calculated-1;
//--- main loop
   for(i=limit; i<rates_total-1; i++)
      buffer[i]=data[i];
//--- restore as_series flags
   if(as_series_data)
      ArraySetAsSeries(data,true);
   if(as_series_buffer)
      ArraySetAsSeries(buffer,true);
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Aleksei Stepanenko:

A question, how do I refer to a structure element by substituting its name as a string?

Who will prompt, it will be well done!

No variable names after compilation,

I need to invent something with macrosubstitutions.

i don't likestring type, i always expect a lot of unexpected things with it:

struct My
{
   datetime time1;
   datetime time2;
   datetime time3;
   datetime time4;
   enum ENUM_FILDS {TIME_1, TIME_2, TIME_3, TIME_4};
} my;
#define  TIME_1 My::TIME_1
#define  TIME_2 My::TIME_2
#define  TIME_3 My::TIME_3
#define  TIME_4 My::TIME_4
datetime GetValue(My &MyStruct, My::ENUM_FILDS name)
{
   datetime result = 0;
   switch(name)
   {
      case TIME_1: result = MyStruct.time1; break;
      case TIME_2: result = MyStruct.time2; break;
      case TIME_3: result = MyStruct.time3; break;
      case TIME_4: result = MyStruct.time4; break;
   }
   return(result);
}
//+------------------------------------------------------------------+
void OnStart()
{
   GetValue(my,TIME_1);
}
//+------------------------------------------------------------------+


UPD:

although it would be more logical to put a method of working with fields in the structure, i.e. so:

struct My
{
   datetime time1;
   datetime time2;
   datetime time3;
   datetime time4;
   enum ENUM_FILDS {TIME_1, TIME_2, TIME_3, TIME_4};
   datetime GetValue(ENUM_FILDS name)
   {
      datetime result = 0;
      switch(name)
      {
         case TIME_1: result = time1; break;
         case TIME_2: result = time2; break;
         case TIME_3: result = time3; break;
         case TIME_4: result = time4; break;
      }
   return(result);
   }
} my;
#define  TIME_1 My::TIME_1
#define  TIME_2 My::TIME_2
#define  TIME_3 My::TIME_3
#define  TIME_4 My::TIME_4

//+------------------------------------------------------------------+
void OnStart()
{
   my.GetValue(TIME_1);
}
//+------------------------------------------------------------------+
 
Igor Makanu:

there will be no variable names after compilation,

I see, Igor, I guess it won't work gracefully. I thought I would shorten the path to the data for clarity, but I'll get sheets of case and macro substitutions. I'll probably settle for a long record. I'll think about it though, it's an interesting solution. Thanks!

 
Aleksei Stepanenko:

A question, how do I refer to a structure element by substituting its name as a string?

Whoever can tell me, well done!
struct My
{
   datetime time1;
   datetime time2;
   datetime time3;
   datetime time4;
   
   datetime operator[]( const string name )
   {
      if( name == "time1" )
         return time1;
      if( name == "time2" )
         return time2;
      if( name == "time3" )
         return time3;
      if( name == "time4" )
         return time4;
      return datetime( 0 );
   }
};

datetime GetValue( My& my, const string name )
{
   return my[name];
}

void OnStart()
{
   My my;
   datetime val = GetValue( my, "time3" );
}
 
Koldun Zloy:
Thanks, thought there was a magic substitution option, without the matching list. But I guess it wasn't meant to be. Thank you!