Features of the mql5 language, subtleties and tricks - page 312

 
Ryan L Johnson #:
iMaOnArray

Thank you. Does the function count evenly? No errors?

 
Vitaly Murlenko #:

Thank you. Does the function count evenly? No errors?

You're welcome.

Yes, as long as it's implemented correctly. The custom function is lengthy because it's not a standard function. See my master mq5 file there for that indicator as an example.

 
Ryan L Johnson #:

You're welcome.

Yes, provided it is implemented correctly. Custom function takes a lot of time because it is not a standard function. See my main mq5 file for this indicator as an example.

I have copied it. Thanks.

 
fxsaber #:
Pay attention to such constructs in your sources. After b5200 they may stop working
struct A
{
  int Array[];
  
  int ArrayResize( const int Size )
  {
    return(ArrayResize(this.Array, Size));   // wrong parameters count, 2 passed, but 1 requires
    return(::ArrayResize(this.Array, Size)); // OK
  }
};
 
Vitaly Murlenko #:I  have not found the iMAOnArray() function in the MQL5 Help. Is there a substitute for it?

Migrating from MQL4 to MQL5 - MQL5 Articles № 17 (2010)

 
How much free memory is actually available.
// Maximum possible number of elements for the array.
template <typename T>
ulong ArraySizeMax( void )
{
  T Array[];
  
  ulong Left = 0;
  ulong Right = ((ulong)::TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE) / sizeof(T)) << 20;
  
  while (Left < Right - 1)
  {
    const ulong Middle = (Left + Right) >> 1;
    
    if (::ArrayResize(Array, (int)Middle) == -1)
      Right = Middle;
    else 
    {
      Left = Middle;
      
      ::ArrayResize(Array, 0, -1); // https://www.mql5.com/ru/forum/462835/page23#comment_52768225
    }
  }
  
  return(Left);
}

// The amount of free memory in megabytes.
int TerminalMemoryAvailable( void ) { return((int)((ArraySizeMax<MqlTick>() * sizeof(MqlTick)) >> 20)); }


Application.

#define  PRINT(A) Print(#A + " = " + (string)(A))

void OnStart()
{
  PRINT(::TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE));
  PRINT(TerminalMemoryAvailable());
}


Result.

::TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE) = 23913
TerminalMemoryAvailable() = 3050
 
Please advise why one case has an error and the other does not.
class A
{
public:  
  double Array[5];
  int Pos;

  double operator []( const int iPos ) { return(this.Array[this.Pos = iPos]); }
  
  A* operator ~( void ) { return(&this); }
  
  void operator =( const double Value ) { this.Array[this.Pos] = Value; }
};

class B
{
public:  
  MqlTick Array[5];
  int Pos;

  MqlTick operator []( const int iPos ) { return(this.Array[this.Pos = iPos]); }
    
  B* operator ~( void ) { return(&this); }
  
  void operator =( const MqlTick &Value ) { this.Array[this.Pos] = Value; }
};

void OnStart()
{
  A a; 
  (~a)[0] = a[1]; // 'operator[]' - l-value required
  
  B b;
  (~b)[0] = b[1]; // OK.
}
 

An EX5 analysis tester could know exactly whether there are calls to HistorySelect functions, and then not waste computational resources on generating detailed history tables.

Similarly, could know that there are no calls to bars, and then not waste computational resources creating an environment around the bars.

Etc.


And how do you automatically realise from one place to another in the source code that there are no calls to certain functions? So that, accordingly, you can disable your own unclaimed functionality directly in the source.

 
fxsaber #:
Please advise why one case has an error and the other does not.
Because A returns a double value, and you cannot assign a value to an r-value.

B returns MqlStruct and you call the operator= on that, which in turn is a function from the MqlStruct.
 
Dominik Egert #:
Because A returns a double value, and you can't assign a value to an r-value.

B returns a MqlStruct, and you call operator= on it, which in turn is a function from MqlStruct.

Thanks!