Download MetaTrader 5

CustomSignal Object

To add comments, please log in or register
Burton
73
Burton  

  Good Day, MQL5 Community!

Could somebody, please, explain to me this macro?

//+------------------------------------------------------------------+
//| Macro definitions.                                               |
//+------------------------------------------------------------------+
//--- check if a market model is used
#define IS_PATTERN_USAGE(p)          ((m_patterns_usage&(((int)1)<<p))!=0)

I know, it is used for "voting" for long and short positions. I understand, bits are involved (bitMasks, bitMaps and flags is something I know very little of, but I'm willing to learn). My question is: how do I describe a market model with it? I know, one can do such things as "price upward cross of rising indicator line - pattern0" or "indicator's line1 is well above line2 - pattern1" and then check, whether those patterns are present on every tick with the above macro, consecutively passing it different pattern numbers. Could somebody show me how to transform my sentences into MQL5 code? I wouldn't mind if you go into detail or direct me to an appropriate reading material. I really want a CustomSignal Object!

Respect to anyone who can lend a hand!

Cheers

Burton

Documentation on MQL5: Standard Constants, Enumerations and Structures / Indicator Constants / Indicators Lines
Documentation on MQL5: Standard Constants, Enumerations and Structures / Indicator Constants / Indicators Lines
  • www.mql5.com
Standard Constants, Enumerations and Structures / Indicator Constants / Indicators Lines - Documentation on MQL5
Rogerio Figurelli
Moderator
43155
Rogerio Figurelli  

Hi Burton, 

In my opinion, this kind of masks are useful to simplify input parameters, so you can define a range that will change bit to bit, for example 0 to 15, instead of using 16 parameters, one for each pattern.

Maybe this article can help you: https://www.mql5.com/en/articles/488

The macro is just for easily test if a bit is on or off (without bits manipulation), and each bit represents a different pattern (using just one input parameter), so you test using for example: 

if (IS_PATTERN_USAGE(4)) {

 ....

}

So if you need any different pattern, you have to code it and define some new mask. 

I'm not sure this is enough to solve your doubt, please give more informations if not.

 


Exploring Trading Strategy Classes of the Standard Library - Customizing Strategies
Exploring Trading Strategy Classes of the Standard Library - Customizing Strategies
  • 2012.09.18
  • Harvester Trading
  • www.mql5.com
In this article we are going to show how to explore the Standard Library of Trading Strategy Classes and how to add Custom Strategies and Filters/Signals using the Patterns-and-Models logic of the MQL5 Wizard. In the end you will be able easily add your own strategies using MetaTrader 5 standard indicators, and MQL5 Wizard will create a clean and powerful code and fully functional Expert Advisor.
Burton
73
Burton  

figurelli:

Hi Burton, 

In my opinion, this kind of masks are useful to simplify input parameters, so you can define a range that will change bit to bit, for example 0 to 15, instead of using 16 parameters, one for each pattern.

Maybe this article can help you: https://www.mql5.com/en/articles/488

The macro is just for easily test if a bit is on or off (without bits manipulation), and each bit represents a different pattern (using just one input parameter), so you test using for example: 

So if you need any different pattern, you have to code it and define some new mask. 

I'm not sure this is enough to solve your doubt, please give more informations if not.

 


figurelli to my aid again! Thank you. Very complete reply. I checked out the article and it is exactly what I've been looking for. Just to clarify, in the following method you only "vote" short if the indicator is declining, plus all the macros, no matter of the int you pass them, would return true? That is, the first  - if(IS_PATTERN_USAGE(0)) would always execute and give the result the value of m_pattern_0? And for the second IS_PATTERN_USAGE(1) the macro would also return true (like with any other int) but the weight is assigned to the result variable only if 2 other conditions are satisfied, so that those 2 conditions really make up the entire market model? With the third || forth conditions, CompareMaps() is used to construct the model, could you please explain the logic behind this method in simple terms if it is possible?

In addition, as far as I understand, the order of conditions is important, since several of them can return true, with the latter giving the vote it's final weight. If I understand correctly, is it a good practice to put more significant models after less significant when evaluating the vote?

Also, I saw some methods of adjusting the weights of the models. Could somebody explain if/when/how often it is good to adjust the weights?

If I said something stupid, please ignore it, I'm still very lost in the world of bits. I've researched masks a little and saw their use in graphics and routing. Sorry, I didn't quite get "each bit represents a different pattern"; am I right to say that following figurelli's example 16 input parameters would be represented by a 4-bit nibble giving us 16 different combinations of 1s and 0s? 

Thank you for taking your time to help me. This will really speed me up on my way!

//+------------------------------------------------------------------+
//| "Voting" that price will fall.                                   |
//| INPUT:  no.                                                      |
//| OUTPUT: number of "votes" that price will fall.                  |
//| REMARK: no.                                                      |
//+------------------------------------------------------------------+
int CSignalCCIxx::ShortCondition()
  {
   int result=0;
   int idx   =StartIndex();
//---
   if(Diff(idx)<0.0)
     {
      //--- the oscillator is directed downwards confirming the possibility of falling of price
      if(IS_PATTERN_USAGE(0)) result=m_pattern_0;      // "confirming" signal number 0
      //--- if the model 1 is used, search for a reverse of the oscillator downwards behind the level of overbuying
      if(IS_PATTERN_USAGE(1) && Diff(idx+1)>0.0 && CCIxx(idx+1)>100.0)
         result=m_pattern_1;      // signal number 1
      //--- if the model 2 or 3 is used, perform the extended analysis of the oscillator state
      if(IS_PATTERN_USAGE(2) || IS_PATTERN_USAGE(3))
        {
         ExtState(idx);
         //--- if the model 2 is used, search for the "divergence" signal
         if(IS_PATTERN_USAGE(2) && CompareMaps(1,1))      // 00000001b
            result=m_pattern_2;   // signal number 2
         //--- if the model 3 is used, search for the "double divergence" signal
         if(IS_PATTERN_USAGE(3) && CompareMaps(0x11,2))   // 00010001b
            return(m_pattern_3);  // signal number 3
        }
      if(IS_PATTERN_USAGE(4) && CCIxx(idx+1)<0.0 && CCIxx(idx+2)>0.0)
         result=m_pattern_4;      // signal number 4 
      if(IS_PATTERN_USAGE(5) && CCIxx(idx+1)<0.0 && CCIxx(idx+2)>0.0 && CCIxx(idx+3)<0.0)
         result=m_pattern_5;      // signal number 5  
     }
//--- return the result
   return(result);
  }
Burton
73
Burton  

I think I just don't understand the whole masking process and the role of IS_PATTERN_USAGE in the code. Gotta research a bit more.
Burton
73
Burton  

Hello again,

I'm not sure if the topic is alive but if anyone has time, please respond, it would help me a lot. I saw the bit shift by the int parameter passed to IS_PATTERN_USAGE() macro which hinted me that 16 parameters would require a 16-digit-long bit sequence to represent each parameter individually and not a 4-bit nibble. That is:

0000 0000 0000 0001 - is the first (hex 0x01)

0000 0000 0000 0010 - is second (hex 0x02)

0000 0000 0000 0100 - third (hex 0x04)

0000 0000 0000 1000 - hex 0x08

...

1000 0000 0000 0000 - sixteenth


Each parameter would then be represented by a sequence of bits which saves resources on the heap, since the type safe parameter would be more heavy on the memory, am I right?

If the IS_PATTERN_USAGE() macro only checks if the bit is on or off, then where and when does the actual bit-mapping occur? Again why do we have this macro in this method? What the code would not do if we got rid of it all together? I'm not even sure if all that applies to this macro, at least that's how I see it. Please help me understand this coding pattern.

Also, I really wanna know how the CompareMaps method works. How does one obtain the map of extremums and what are the implications of comparison of such maps? An example on how to describe a market model with it would be greatly appreciated.

Many thanks to anyone who can help!

Burton

Burton
73
Burton  

I understand that it might take a real genius to write something like Compare Maps method, I was just wondering if somebody could share a little of his intellectual greatness with us, mortal folks, who's only starting to study computer science. I guess it would be a great lesson in mathematics as well! Looking forward to your replies! Thank you, in advance!
graziani
2026
graziani  

This is a macro which checks if bit number p is set in m_patterns_usage.

So it actually checks if a variable is true(1) or false(0). 

Macros are considered obsolete from C++, so they should be avoided.

To save memory use bit variables i.e. boolean type.


This is equivalent of code used above: 

bool patternUsed[16] = {true, false .......};

if (patternUsed[p] == true)
{

.....

}

Of course, it is normal to give each pattern a separate variable with a good name :

bool triangle = true;
bool sHS = false;

if (triangle == true)
{
// do something about triangle
}

if (sHS == true)
{
// do something about SHS;
}

 

You should first read a book about programming and then start writing code.

This way you are completely lost. You don't know what you are doing, you are mixing terms and loosing time.

 

Heinz Traub
314
Heinz Traub  

Bitwise operations like the macro you posted are only useful for optimizing memory or optimizing computations in rare cases. But all the memory and performance gained is barely noticeable or does not have an impact for most EA applications.

Personally i'm very familiar with bitwise operations, but again, they were useful to me in rare cases like when i implemented an AES library or when i developed my own game engine. For EAs it is not a common programming practice.

I wouldn't give it much more attention to this. If it's in the code of an EA you're studying then let it be. But try to implement what graziani suggests, if you feel comfortable with that.

Burton
73
Burton  

Guys! Thank you very much! I believe I got it now!

I can see the value of (int) -1 is assigned to m_patterns_usage mask in the constructor of ExpertSignal which is equivalent to binary  1111 1111 1111 1111 - meaning all models are used and all the bits are on, that is the IS_PATTERN_USAGE would always return true. So simple, and yet I struggled to find any info on bit masks online. In two weeks I've read only about image processing and sub-netting. Maybe that is due to macros becoming an obsolete tech, as graziani suggested. 

My background is in Economics and I only started taking an interest in programming about a year ago. So far I've read "C# Professional", a book on servlets and jsp, some articles on genetic algorithms and NNs and mql5 articles and forum threads. There is still the world of unknown programming patterns out there, so if you have a good book that can help me become an efficient mql5 coder, I would definitely follow your advice.

Btw, I have no problem understanding all the other members of ExpertSignal class, except those using binary operations, like my beloved CompareMaps method. Mapping indicator line to and array of 4-bit fields representing ratios of extremums  - that alone sounds like it is beyond my understanding, so comparison of such maps is a total science fiction. Though, in spite of the few lines of code constituting it, I can feel this method is a very powerful tool for function analysis and market models description, and I would really like to try to understand its inner-workings, if somebody is willing to explain. 

TripleHeinz, graziani and figurelli, thank you, fellas, for taking your time to help a novice like me! Once and if at all, I reach your level of expertise, I will share my knowledge with inexperienced just like you do!

To add comments, please log in or register