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.

- 2012.09.18
- Harvester Trading
- www.mql5.com
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); }
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.
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
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!
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.
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.
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!
My understanding is that MT's idea of events are Signals, and I like this so I wanted to try and conform to the MQL5 way of doing things. But when looking at the "simple" RSI example code, I too came accross this unreadable function. The bit/model selection part I understand but it's the innards that I don't understand even though I'm quite an experienced programmer.
I think that it is working over a "window" of the past X bars - up to 32. Finding minima and maxima along the way and doing various things but I would really like more explaination if I am to also follow this convention of writing signals for my own indicators.
What is this code doing, can anyone write more comments for us all?
For this long condition, when it detects patterns 2-5 (which are written here https://www.mql5.com/en/docs/standardlibrary/expertclasses/csignal/signal_rsi) - what are these inputs to CompareMaps?
int CSignalRSI::LongCondition(void) { int result=0; int idx =StartIndex(); //--- if(DiffRSI(idx)>0.0) { //--- the oscillator is directed upwards confirming the possibility of price growth 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 upwards behind the level of overselling if(IS_PATTERN_USAGE(1) && DiffRSI(idx+1)<0.0 && RSI(idx+1)<30.0) result=m_pattern_1; // signal number 1 //--- if the model 2, 3, 4 or 5 is used, perform the extended analysis of the oscillator state if(IS_PATTERN_USAGE(2) || IS_PATTERN_USAGE(3) || IS_PATTERN_USAGE(4) || IS_PATTERN_USAGE(5)) { ExtStateRSI(idx); //--- search for the "failed swing" signal if(IS_PATTERN_USAGE(2) && RSI(idx)>m_extr_osc[1]) result=m_pattern_2; // signal number 2 //--- search for the "divergence" signal if(IS_PATTERN_USAGE(3) && CompareMaps(1,1)) // 0000 0001b result=m_pattern_3; // signal number 3 //--- search for the "double divergence" signal if(IS_PATTERN_USAGE(4) && CompareMaps(0x11,2)) // 0001 0001b return(m_pattern_4); // signal number 4 //--- search for the "head/shoulders" signal if(IS_PATTERN_USAGE(5) && CompareMaps(0x62662,5,true) && RSI(idx)>m_extr_osc[1]) // 01100010011001100010b result=m_pattern_5; // signal number 5 } } //--- return the result return(result); }
Then this code, I can barely understand need help.
bool CSignalRSI::CompareMaps(int map,int count,bool minimax,int start) { int step =(minimax)?4:8; int total=step*(start+count); //--- check input parameters for a possible going out of range of the bit-map if(total>32) return(false); //--- bit-map of the patter is an "array" of 4-bit fields //--- each "element of the array" definitely describes the desired ratio //--- of current extremums of the oscillator and the price with previous ones //--- purpose of bits of an elements of the pattern of the bit-map pattern //--- bit 3 - is equal to if the ratio of extremums of the oscillator is insignificant for us //--- is equal to 0 if we want to "find" the ratio of extremums of the oscillator determined by the value of bit 2 //--- bit 2 - is equal to 1 if we want to "discover" the situation when the current extremum of the "oscillator" is "more extreme" than the previous one //--- (current peak is higher or current valley is deeper) //--- is equal to 0 if we want to "discover" the situation when the current extremum of the oscillator is "less extreme" than the previous one //--- (current peak is lower or current valley is less deep) //--- bit 1 - is equal to 1 if the ratio of extremums is insignificant for us //--- it is equal to 0 if we want to "find" the ratio of price extremums determined by the value of bit 0 //--- bit 0 - is equal to 1 if we want to "discover" the situation when the current price extremum is "more extreme" than the previous one //--- (current peak is higher or current valley is deeper) //--- it is equal to 0 if we want to "discover" the situation when the current price extremum is "less extreme" than the previous one //--- (current peak is lower or current valley is less deep) uint inp_map,check_map; int i,j; //--- loop by extremums (4 minimums and 4 maximums) //--- price and the oscillator are checked separately (thus, there are 16 checks) for(i=step*start,j=0;i<total;i+=step,j+=4) { //--- "take" two bits - patter of the corresponding extremum of the price inp_map=(map>>j)&3; //--- if the higher-order bit=1, then any ratio is suitable for us if(inp_map<2) { //--- "take" two bits of the corresponding extremum of the price (higher-order bit is always 0) check_map=(m_extr_map>>i)&3; if(inp_map!=check_map) return(false); } //--- "take" two bits - pattern of the corresponding oscillator extremum inp_map=(map>>(j+2))&3; //--- if the higher-order bit=1, then any ratio is suitable for us if(inp_map>=2) continue; //--- "take" two bits of the corresponding oscillator extremum (higher-order bit is always 0) check_map=(m_extr_map>>(i+2))&3; if(inp_map!=check_map) return(false); } //--- ok return(true);

- www.mql5.com

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Good Day, MQL5 Community!
Could somebody, please, explain to me this macro?
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