Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

Libraries

Arrays class - library for MetaTrader 5

DMITRII PECHERITSA

Views:
770
Rating:
votes: 6
Published:
2020.06.11 08:45
Updated:
2020.06.13 13:12

Sometimes we need to add an element to array, or insert an element.

Standard Mql Array functions are efficient but do not provide such functionality.

For example, if you need to find an element, you can search only in sorted array with ArrayBsearch.

They also do not support complex data types.

This class fills the gap, and provides some frequent array operation methods:

  •    Add/insert elements to array by value/reference.
  •    Find/ReverseFind an element in unsorted array.
  •    Sum/Average of a numeric array.
  •    Min/Max returns the actual min/max value, not index of the value.
  •    Array copy/insert works with arrays containing complex types.
  •    StringToCodes creates an array of symbol codes from a string.

class Arrays
  {
public:
   template<typename T>int    Add(T src,T &dst[]);
   template<typename T>int    Copy(T &dst[],T &srs[],bool clean=0,int dststart=0,int srcstart=0,int srccount=WHOLE_ARRAY);
   template<typename T>bool   Insert(T src,T &dst[],int pos);
   template<typename T>int    Find(T element,T &array[],int start=0);
   template<typename T>int    FindReverse(T element,T &array[],int start=0);
   template<typename T>double Average(T &src[]);
   template<typename T>double Sum(T &src[]);
   template<typename T>double Max(T& src[]);
   template<typename T>double Min(T& src[]);

   template<typename T>int    Addbyref(T &src,T &dst[]);
   template<typename T>bool   Insertbyref(T &src,T &dst[],int pos);
   template<typename T>bool   Insert(T &dst[],T &src[],int dststart);
   int                        StringToCodes(string src,short &dst[],int startpos=0,int count=-1);
protected:
   Types             types;
  };

Notes:

  • FindReverse searches for the element from array end.
  • Addbyref does the same thing as Add by can work with complex types, which are sent only by reference.
  • Copy has the same signature/behavior as the standard Mql ArrayCopy. Unlike ArrayCopy, it allows complex types in src/dst.
  • StringToCodes is basically the standard StringToShortArray, except that the final 0 is removed from the destination array.

Examples of Arrays class usage.

void OnStart()
  {
   int a[],asrc[]= {1,2,3,4};
   int b[],bsrc[]= {5,6,7};
   ArrayCopy(a,asrc);
   ArrayCopy(b,bsrc);
   /**/
   Arrays arrays;
   arrays.Insert(a,b,0);         //insert array in array
   arrays.Insert(0,a,3);         //insert element in array
   arrays.Add(8,a);              //add element to array
   double sum=arrays.Sum(a);     
   double avg=arrays.Average(a);
   double max=arrays.Max(a);
   int findwhat=2;
   int findindex=arrays.Find(findwhat,a);
   int findrev=arrays.FindReverse(findwhat,a);
   int size=ArraySize(a);
   /**/
   string print=StringFormat("size:%d, sum:%g, avg:%g, max:%g, '%d' found in pos:%d, '%d' found rev in pos:%d",
                             size,sum,avg,max,findwhat,findindex,findwhat,findrev);
   short printcodes[];
   arrays.StringToCodes(print,printcodes); //convert string to array of char codes
   Print(print);
   ArrayPrint(a);
   ArrayPrint(printcodes);
  }

Output:

/**
   size:9, sum:36, avg:4, '2' found in pos:5, '2' found rev in pos:3
   5 6 7 0 1 2 3 4 8
   [ 0] 115 105 122 101  58  57  44  32 115 117 109  58  51  54  44  32  97 118 103  58  52  44  32  39  50  39  32 102 111 117 110 100  32
   [33] 105 110  32 112 111 115  58  53  44  32  39  50  39  32 102 111 117 110 100  32 114 101 118  32 105 110  32 112 111 115  58  51
/**/
    Doubles comparer Doubles comparer

    Class for comparing two floating point variables.

    WaveMTF MT5 WaveMTF MT5

    Indicator WaveMTF Bull and Bear System with Signal and Alert for MetaTrader 5 with options to display signal on the chart.

    Bar Time Count Down Bar Time Count Down

    This MT5 indicator is to count down the remaining time of the current bar as the format HH:MM:SS

    M1MA indicator M1MA indicator

    M1-based Moving Average. It gives more adequate estimation of average price per bar compared to any standard price type (close, open, median, typical, weighted, etc).