Join our fan page
RegularExpressions in MQL4 for working with regular expressions - library for MetaTrader 4
- Published by:
- MetaQuotes
- Views:
- 8596
- Rating:
- Published:
- 2017.10.04 12:09
- Updated:
- 2018.09.12 09:27
- Need a robot or indicator based on this code? Order it on Freelance Go to Freelance
Real author:
Microsoft Corporation. Source code taken from .Net Framework 4.6.1.
Note: The library works on the MetaTrader 4 build 1384 and higher.
Unzip the archive into the terminal_data_folder.
The library codes are located in the: <terminal_data_folder>\MQL4\Include\RegularExpressions\
Sample test scripts can be found in the <terminal_data_folder>\MQL4\Scripts\RegularExpressionsExamples\
Here is a translation of the RegularExpressions from .Net Framework 4.6.1.
To work with the library, include the Regex.mqh file from the "\MQL4\Include\RegularExpressions\" directory in your code.
Several illustrative examples are provided with the library, which at the same time serve as the test cases. All the samples are taken from the official site of Microsoft Corporation, they vividly demonstrate the main differences from the regular expressions in C# and the features of their use in the MQL4.
Below is more detailed information about RegularExpressions MQL4 ported library packages:
Packages |
Description |
---|---|
CharUnicodeInfo.mqh |
Archived txt file to determine the Unicode categories for all symbols (including non-Latin characters). |
RegexCapture.mqh |
Represents the results from a single successful subexpression capture. |
RegexCaptureCollection.mqh |
Represents the set of captures made by a single capturing group. |
RegexGroup.mqh | Represents the results from a single capturing group. |
RegexGroupCollections.mqh | Represents a collection of Group objects. |
RegexMatch.mqh | Represents the results from a single regular expression match. |
RegexMatchCollection.mqh | Represents a collection of successful matches found by iteratively applying the regular expression pattern to the input string. |
Regex.mqh | Represents an immutable regular expression |
RegexOptions.mqh | Provides enumerated values to use to set regular expression options. |
The regular expression parameters from the file RegexOptions.mqh:
Parameter | Description |
---|---|
None | Specifies that no options are set. |
IgnoreCase | Specifies case-insensitive matching. |
Multiline | Specifies multiline mode. |
ExplicitCapture | Do not capture unnamed groups. Specifies that the only valid captures are explicitly named or numbered groups of the form (?<name> subexpression). |
Singleline | Specifies single-line mode. |
IgnorePatternWhitespace | Eliminates unescaped white space from the pattern and enables comments marked with #. |
RightToLeft | Specifies that the search will be from right to left instead of from left to right. |
Debug | Specifies that the program works in the debug mode. |
ECMAScript | Enables ECMAScript-compliant behavior for the expression. This value can be used only in conjunction with the IgnoreCase and Multiline values. |
Working with RegularExpressions for MQL4:
- As withe the version for .Net, this library implements a storage (static cache)
of regular expressions. All implicitly created regular expressions
(instances of the Regex class) are written to that storage. This
approach speeds up the operation of the scripts, as it eliminates the
need to rebuild the regular expression if its pattern matches any of the
existing ones. The default size of the storage is 15. The Regex::CacheSize() method returns or sets the maximum number of entries in the current static cache of the complied regular expressions.
- The second feature of working with regular expression in MQL4 directly follows from the first one. And it lies in the fact that the above storage must be cleared. To do that, call the Regex::ClearCache() static function. It is recommended to call this function only after the work with the regular expressions has been completed, otherwise there is a risk to delete necessary pointers.
- Unlike the .Net, the MQL4 does not implement the foreach loop, and hence the enumeration handling will be different. Example:
//--- Code in C# Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b", RegexOptions.IgnoreCase); string text = "The the quick brown fox fox jumped over the lazy dog dog."; MatchCollection matches = rx.Matches(text); foreach (Match match in matches) { //--- handling } //--- Code in MQL4 Regex *rx = new Regex("\\b(?<word>\\w+)\\s+(\\k<word>)\\b", RegexOptions::IgnoreCase); string text = "The the quick brown fox fox jumped over the lazy dog dog."; MatchCollection *matches = rx.Matches(text); IEnumerator<Match*> *en = matches.GetEnumerator(); while(en.MoveNext()) { Match *match = en.Current(); //--- handling } delete rx; delete matches; delete en; Regex::ClearCache();
- As can be seen from the above example, the C# syntax allows to put the '@' symbol in front of the strings to ignore all formatting marks in it. In MQL4, this approach is not provided, so all control characters in a regular expression pattern should be explicitly defined.
To learn more about the RegularExpressions for MQL4 and its features, use the provided Tests.mqh expert. It implements numerous examples of regular expression usage, which cover all the main functionality of the library.
Tr
Translated from Russian by MetaQuotes Ltd.
Original code: https://www.mql5.com/ru/code/16566
Not really profitable but the code fits on one page.
Previous Candle Hi-LoPrevious Candle Hi-Lo is used to check the last candle multi-timeframe position relative to the current price.
This custom indicator will show you 28 pair's daily candle range, High-Low range, candle bullishness or bearishness from real tick market. So you can understand the overall market situation very short time. You can change the default timeframe from Daily to any period and default candle (bar) number from 0 to any previous number. Also you can open the required symbol by clicking the symbol button.
BarNumbersDisplays the number of each bar - both, relative to the most current bar and in absolute terms from the beginning of the chart.