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

Binary Flags - library for MetaTrader 5

DMITRII PECHERITSA

Views:
862
Rating:
votes: 10
Published:
2020.06.01 08:23
Updated:
2020.06.13 12:31

You can use binary flags to minimize bool parameters in a function signature.

For example, MQL int size is 32 bits, so you can pack 32 1/0 parameters in a single int flag variable.

Before:

void Function(bool flag1,bool flag2,bool flag3,bool flag4,bool flag5,bool flag6,bool flag7,bool flag8,...flagN);

After:

void Function(int flags);

BinFlags can be initialized with any integer data type:char, bool, short, int, color, long, datetime.

Your maximum flag length will vary: 1 byte = 8 bits, 2 bytes = 16 bits, etc.

A flag represents a number which has only one '1' bit in any position.

Such numbers are 1, 2, 4, 8, 16, 32, etc. You get it.

Large numbers of this kind look better in hex: 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, etc.

This class works with flags, which follow the rule.

BinFlags must be initialized first. You can overwrite later the internal flags with Write.

You can check, set, reset any number of flags.

Multiple flags should be separated by '|'.

template<typename T>class BinFlags
  {
public:
                     BinFlags():mflags(NULL) {} BinFlags(T flags):mflags(flags) {}
   void              Write(T flags) {mflags=flags;}
   T                 Read() {return mflags;}
   bool              Chk(T flags) {return (mflags&(flags))==flags;}
   void              Set(T flags) {mflags|=(flags);}
   void              Rst(T flags) {mflags&=~(flags);}
   string            Format();
protected:
   int               Bits() {int i=0; for(i; i<sizeof(T)*8; i+=4) {if(mflags<(int)pow(2,i)) {break;}} return (i)?i:1;}
   T                 mflags;
  };

Flag names can be enumerated for convenience.

Example of BinFalgs usage.


void OnStart()
  {
   BinFlags<int>bf(A|B);         //init & write
   /*MANIPULATE*/
   Print(bf.Format());           //0011 (2 flags on)
   Print("A:",bf.Chk(A));        //A:true
   Print("AD:",bf.Chk(A|D));     //AD:false
   bf.Set(C|D);                  //set C & D
   Print(bf.Format());           //1111 (all flags on)
   bf.Rst(A);                    //reset A
   Print("BCD:",bf.Chk(B|C|D));  //BCD:true
   Print(bf.Format());           //1110 (one flag down)
  }

Output:

/**
   BinFlags:0011
   A:true
   AD:false
   BinFlags:1111
   BCD:true
   BinFlags:1110
/**/



    Quick Market-watch Launcher Quick Market-watch Launcher

    This script opens all market watch symbols with the default template using the selected period. Save prefferred template as default.tpl to have all charts open with same template of your choice. Happy Trading.

    Base64Encrypt Class Base64Encrypt Class

    This a library for a quick and easy encryption and decryption using base64. The usage is very simple and can be done in a few lines of code. <<< The return value of a method is the required output. <<< Feel free to use this library at your convenience.If it is helpful, please reward me by rating this item on mql5 site. >>From a developer, for developers.<<

    EMA on RSI EMA on RSI

    Calculates a Exponential Moving Average based on RSI Data, instead of the regular Pricing data. Simple and and to the point.

    Alphabet structure Alphabet structure

    Sets of latin, russian characters, digits, punctuation, etc.