Handy MQL4 utility functions
Page 1 of 615 123 ... LastLast
Results 1 to 10 of 41

Thread: Handy MQL4 utility functions

  1. #1
    2 Attachment(s) IMPORTANT UPDATE (May 7, 2017):
    Pips4life has very generously updated these purposes so they will operate correctly in MT4 builds 600 or later. You can download the new versions of these documents, and also read his notes and disclaimers, here. As far as I know, the functions need to continue to work just as they did before.
    ____________________________

    INSTRUCTIONS

    Updated January 7, 2014:</b></b>
    I have added more functions since this article was last updated.
    For the whole list of functions, refer to the' -- function header (np).mqh' file.

    Updated August 7, 2013:
    I have added possibly 5-6 more functions since this article was last updated.
    For the whole list of functions, refer to the' -- function header (np).mqh' file.

    Updated Jan 28, 2013 :
    1. Added the following purposes:
    iRegr() #8212; yields pips, slope, stdev of regression curve characterized by symbol, period, poly level, #points, st devs, hist shift
    PlotTL (objname, delete, window, time1, price1, time2, price2, color, width, style, beam, backg, vis); // Plot trendline
    PlotBox (objname, delete, window#, time1, price1, time2, price2, color, width, style, backg, vis); // Plot rectangle
    PlotArrow (objname, delete, window, time1, price1, color, dimensions, signal, backg, vis); // Plot arrow
    PlotVL (objname, delete, window, time1, color, width, style, backg, vis); // Plot vertical line
    PlotHL (objname, delete, window, price1, color, width, style, backg, vis); // Plot horizontal line
    PlotText (objname, delete, window, time1, price1, text, color, size, font, angle, backg, vis); // Plot text
    PlotLabel (objname, delete, window, corner, hpos, vpos, text, color, size, font, angle, backg, vis); // Plot text label

    Updated Jan 3, 2013:
    1. Added the following purposes: ColorToStr() #8212; converts a MT4 color value to its equivalent string token (e.g. AliceBlue, BurlyWood, Red, b101g96r44 etc)
    Updated Oct 11, 2012:
    1. Added the following purposes:
    DayNumber() #8212; awarded a MT4 datetime, returns number of times since 1 Jan 1970 WeekNumber() #8212; awarded that a MT4 datetime, yields number of weeks since 1 Jan 1970
    MonthNumber() #8212; awarded that a MT4 datetime, yields few months since 1 Jan 1970

    Updated Sept 15, 2012:
    1. Additional StrToColorArray() function.

    Updated April 28, 2012 :
    1. Additional MathFactorial() function.
    2. Fixed bug in StrToDoubleArray(), StrtoIntegerArray(), StrToStringArray() works, which were formerly not handling strings correctly.

    Updated April 22, 2012:
    Throughout my time as a MT4 programmer, I've extended the MQL4 language by adding more than 80 new purposes. These perform date, number, string and variety manipulations, that IMO should be a part of any programming language. Here is a list, and brief description, of these functions:

    AppendIfMissing() #8212; appends a character to a string if it's not already the rightmost character
    AppendIfNotNull() #8212; appends a character to a string if the string is not
    BarConvert() #8212; converts a bar# (candle#) to the equivalent bar# on another timeframe
    BaseToNumber() #8212; performs multibase arithmetic: converts a non-base 10 number (string) to a base 10 integer
    BoolToStr() #8212; converts a boolean value to true or false
    ColorToStr() #8212; converts a MT4 color value to its equivalent string token (e.g. AliceBlue, BurlyWood, Red, b101g96r44 etc)
    DateToStr() #8212; formats a MT4 date/time value to a string, using a very sophistied format mask
    DatesToStr() #8212; performs multiple DateToStr() operations in One function
    DayNumber() #8212; given a MT4 datetime, returns number of days since 1 Jan 1970
    DebugDoubleArray() #8212; unloads a set of values from a double array, prefixed by the element number, to a single string
    DebugIntegerArray() #8212; unloads a set of values from an int array, prefixed by the element number, to a single string
    DebugStringArray() #8212; unloads a set of values from a string array, prefixed by the element number, to a single string
    DivZero() #8212; returns 0 instead of'divide by zero' error, if denominator evaluates to 0
    DoubleArrayToStr() #8212; unloads a set of values from a double array to a single string, inserting a specified delimiter between the values
    EasterDay() #8212; returns the MT4time value of Easter Sunday for the given year
    ExpandCcy() #8212; expands a curency symbol name, e.g. EJ to EURJPY
    ExtractAlpha() #8212; returns alphabetic (or other) characters in a string
    ExtractUnique() #8212; returns 1 occurrence only of each char in a string; result Might Be optionally sorted asc/desc
    FileSort() #8212; shell sorts an ASCII text file, rewriting the file with its records in alphanumeric order
    GetHash() #8212; calculate and return checksum of a string
    GetVisibility() #8212; returns suitable OBJPROP_TIMEFRAMES value from a timeframes string (e.g. M1,M5,M15)
    IntegerArrayToStr() #8212; unloads a set of values from an int array to a single string, inserting a specified delimiter between the values
    iRegr() #8212; returns pips, slope, stdev of regr curve defined by pair, tf, poly degree, #points, st devs, hist shift
    ListGlobals() #8212; lists all GlobalVariables and their values, to a string
    ListOrders() #8212; lists all orders of your chosen stati (open/pending/closed/deleted) to a string
    LookupDoubleArray() #8212; looks up a numeric value in a double array, returning the element number (if found)
    LookupIntegerArray() #8212; looks up a numeric value in an int array, returning the element number (if found)
    LookupStringArray() #8212; looks up a string value in a string array, returning the element number (if found)
    MathFactorial() #8212; calculates a factorial n(n-1)(n-2)...1 using a recursive technique
    MathFix() #8212; returns the value of N, rounded to D decimal places (fixes precision bug in MQL4 MathRound)
    MathInt() #8212; returns the value of N, rounded DOWN to D decimal places (fixes precision bug in MQL4 MathFloor)
    MathSign() #8212; returns the sign (-1,0, 1) of a number
    MonthNumber() #8212; given a MT4 datetime, returns number of months since 1 Jan 1970
    NumberToBase() #8212; performs multibase arithmetic: converts a base 10 integer to a non-base 10 number (string)
    NumberToStr() #8212; formats a numeric (int/double) value to a string, using a very sophistied format mask
    NumbersToStr() #8212; performs multiple NumberToStr() operations in One function
    OrderStatus() #8212; given a ticket number, returns the order status (O=open, P=pending, C=closed, D=deleted, U=unknown)
    ReadWebPage() #8212; reads a page from a specified URL into a string
    ReduceCcy() #8212; reduces a currency symbol name, e.g. EURJPY to EJ
    ReturnDay() #8212; returns the MT4time value of the (e.g.) 3rd Sunday after 14 Feb 2011
    ShellsortDoubleArray() #8212; shell sorts an array of double values into ascending or descending sequence
    ShellsortIntegerArray() #8212; shell sorts an array of int values into ascending or descending sequence
    ShellsortString() #8212; shell sorts the characters in a string into ascending or descending ASCII sequence
    ShellsortStringArray() #8212; shell sorts an array of string values into ascending or descending ASCII sequence
    StrToBool() #8212; converts a suitable string (T(rue)/t(rue)/F(alse)/f(alse)/1) to a boolean value
    StrToChar() #8212; returns the decimal ASCII value of a 1 byte string (inverse of MQL4's CharToStr())
    StrToColor() #8212; converts a string (color name, RGB values, etc) to a MQL4 color
    StrToColorArray() #8212; loads a color array from a delimiter-separated set of string values (e.g. Blue,Green,Red); returns the number of array elements loaded
    StrToDate() #8212; converts a number of different string patterns to a MT4 date/time value
    StrToDoubleArray() #8212; loads a double array from a delimiter-separated set of string values (e.g. 1,2,3); returns the number of array elements loaded
    StrToIntegerArray() #8212; loads an int array from a delimiter-separated set of string values (e.g. 1,2,3); returns the number of array elements loaded
    StrToNumber() #8212; strips all non-numeric characters from a string, returning a numeric (int/double) value
    StrToStr() #8212; left/right/center aligns, or trunes, a string, using a very sophistied format mask
    StrToStringArray() #8212; loads a string array from a delimiter-separated set of string values (e.g. 1,2,3); returns the number of array elements loaded
    StrToTF() #8212; converts a timeframe string to a number (e.g. M15 to 15)
    StringArrayToStr() #8212; unloads a set of values from an string array to a single string, inserting a specified delimiter between the values
    StringDecrypt() #8212; unencrypts a string that was previously encrypted using StringEncrypt()
    StringEncrypt() #8212; encrypts a string
    StringExtract() #8212; extracts the content of string between 2 specified delimiters
    StringFindCount() #8212; returns the number of occurrences of a certain substring in a string
    StringInsert() #8212; inserts characters into a given position in a string
    StringLeft() #8212; returns the leftmost characters, or all but the N rightmost characters, of a string
    StringLeftExtract() #8212; extracts N characters from a string, counting from the left
    StringLeftPad() #8212; inserts specified padding characters at the beginning of a string
    StringLeftTrim() #8212; eliminates all leading characters from a string
    StringLower() #8212; converts all alphabetic characters in a string to lowercase
    StringOverwrite() #8212; overwrites characters in a given position of a string
    StringRepeat() #8212; returns a specified string, repeated N times
    StringReplace() #8212; replaces substring in a string with another substring
    StringReverse() #8212; reverses a string, e.g. ABCDE becomes EDCBA
    StringRight() #8212; returns the rightmost characters, or even all but the N leftmost characters, of a string
    StringRightExtract() #8212; extracts N characters from a string, counting from the ideal
    StringRightPad() #8212; appends specified padding characters into the end of a string
    StringRightTrim() #8212; removes all trailing spaces from a string
    StringTranslate() #8212; translates characters in a sequence, given that a complete translation table
    StringTrim() #8212; eliminates all (leading, trailing and embedded) spaces from a string
    StringUpper() #8212; converts all alphabetic characters in a string to uppercase
    StrsToStr() #8212; plays multiple StrToStr() operations in one purpose
    TFToStr() #8212; converts a number into a span string (e.g. 1440 into D1)
    WeekNumber() #8212; given that a MT4 datetime, returns the week number since 1 Jan 1970, week is supposed to start on Sunday
    YMDtoDate() #8212; converts 3 integers (year, month and day) into a MT4 date/time value
    d() #8212; outputs up to 8 values into the document /EXPERTS/FILES/DEBUG.TXT, appending data to the end of the document
    dd() #8212; outputs up to 8 values into the document /EXPERTS/FILES/DEBUG.TXT, developing a new file
    err_msg() #8212; returns a complete description of a mistake, provided its error code number
    log() #8212; outputs up to 8 values for viewing using Microsoft's http://technet.microsoft.com/en-us/s.../bb896647.aspx centre (see here for more info)

    October 31, 2011: upgraded further. Many new functions added as the last update.

    June 6, 2009: I have added some more functions. Two that might be especially useful are:
    NumberToStr(n,mask)
    DateToStr(date,mask)


    OutputString = NumberToStr(n,mask)
    This formats a number (int or double) to a string, performing alignment, rounding, inserting commas (0,000,000 etc), floating signs, currency symbols, etc, according to the directions supplied in the'mask'.

    The Fundamental mask is n.d where n is the number of digits to the left of the decimal point, and d the amount to the right,
    e.g. NumberToStr(123.456,5) will reunite lt;spacegt;lt;spacegt;123
    e.g. NumberToStr(123.456,5.2) will reunite lt;spacegt;lt;spacegt;123.45

    Other characters that may be Utilized in the mask:Including a - everywhere to the left of n.d can cause a floating minus symbol to be contained into the remaining amount, if the nunber is damaging; no symbol if favorable Including a anywhere to the left of n.d will cause a floating and minus symbol to be contained, to the remaining amount containing a - everywhere to the right of n.d can cause a minus to be contained at the right of the amount, e.g. NumberToStr(-123.456,3.2-) will yield 123.46- containing a ( or ) anywhere in the mask will cause any negative amount to be enclosed in parentheses containing an R or r any place in the mask will cause rounding, e.g. NumberToStr(123.456,R3.2) will reunite 123.46; e.g. NumberToStr(123.7,R3) will reunite 124 Including a $, #8364;, or any place in the mask will cause the floating floating currency symbol to be contained, to the remaining amount Including a, any place in the mask will cause commas to be inserted between each 3 digits, to separate thousands, countless etc in the remaining amount, e.g. NumberToStr(123456.789,,6.3) will reunite 123,456.789 Including a Z or z anywhere in the mask will cause zeros (rather than spaces) to be used to fill any unused areas at the remaining amount, e.g. NumberToStr(123.456,Z5.2) will reunite 00123.45 Including an asterisk * anywhere in the mask will cause asterisks to be used to fill any unused areas at the remaining amount, e.g. NumberToStr(123.456,*5.2) will reunite **123.45 Including a B or b any place in the mask (blank if zero) can cause the entire output to be blanks, if the value of the amount is not any like a ~ anywhere in the mask will cause a tilde to be output, if overflow occurs (the value of in n.d is too small to permit the amount to be output in total ) Including a L or l any place in the mask will cause the output to be left aligned in the output field, e.g. NumberToStr(123.456,L5.2) will reunite 123.45lt;spacegt;lt;spacegt; Including a T or t anywhere in the mask will cause the output to be left aligned in the output field, and trailing spaces trimmed e.g. NumberToStr(123.456,T5.2) will reunite 123.45 Including a; anywhere in the mask will cause decimal point and comma to be juxtaposed, e.g. NumberToStr(123456.789,;,6.3) will reunite 123.456,789 Any combination of the above can be used from the mask. Feel welcome to experiment.



    OutputString = DateToStr(date,mask)
    This converts an MT4 date(/time) value to a formatted string, according to the directions in the'mask'. A d in the mask will cause a 1-2 digit day-of-the-month to be inserted into the output, in the point A D in the mask will cause a 2 digit day-of-the-month to be inserted into the output, at the point A m in the mask will cause a 1-2 digit month amount to be inserted in the output, at the point A M in the mask will cause a 2 digit month amount to be inserted into the output, at the point A y in the mask will cause a 2 digit year to be inserted into the output, at the point A Y in the mask will cause a 4 digit (Y2K compliant) season to be inserted into the output, at the point A W in the mask will cause a day-of-the week (Monday, Tuesday, etc) description to be inserted into the output, in the point A w in the mask will cause an abbreviated day-of-the week (Mon, Tue, etc) description to be inserted into the output, in the point A N in the mask will cause a month name (January, February, etc) to be inserted in the output, in the point A n in the mask will cause an abbreviated month name (Jan, Feb, etc) to be inserted in the output, in the point A h from the mask will cause the hour-of-the-day to be inserted in the output, as 1 or 2 digits, in the point A H in the mask will cause the hour-of-the-day to be inserted into the output, as 2 digits (with placeholding 0, if value lt; 10), in the point An I or in the mask will cause the minutes to be inserted into the output, as 2 digits (with placeholding 0, if value lt; 10), in the point A S or so at the mask will cause the seconds to be inserted into the output, as 2 digits (with placeholding 0, if value lt; 10), in the point An a from the mask will cause a 12-hour version of the Opportunity to be exhibited, with pm or am at the point An A in the mask will cause a 12-hour version of the Opportunity to be exhibited, with AM or PM at the point A T in the mask will cause st nd rd or th to be inserted at the point, depending on the day of the month e.g. 13th, 22nd, respectively All other figures in the mask will be output, as is Examples: if date (a MT4 datetime variable) is June 04, 2009, then:DateToStr(date,w m/d/Y) will output Thu 6/4/2009 DateToStr(date,Y-MD) will output 2009-0604 DateToStr(date,d N, Y is a W) will output 4 June, 2009 is a Thursday DateToStr(date,W D'M'y = W) will output Thursday 04'06'09 = Thursday Directions:


    1. Download the connected .mqh files in your.../ / [MT4]/ / experts/include folder.

    2. Just copy the #include statements as shown in the sample under, in end and the beginning of your EA/indior/script, then the latter has access to each the additional functions.

    Inserted Code //p ------------------------------------------------------------------ //| sample.mq4 | //p ------------------------------------------------------------------ // duplicate the #include statement to the start of your program code #include lt; -- serve header (np).mqhgt; // ------------------------------------------------------------------ int start() // ------------------------------------------------------------------ MessageBox(NumberToStr(20,'#91;'-5.3'#93;'-RRB-); return(0); // ------------------------------------------------------------------ // duplicate the #include statement towards the conclusion of your app code #include lt; -- extensible functions (np).mqhgt;
    Notice that if you compile your EA/indior/script, then you'll find a range of warning messages (Function XXX isn't referenced and will probably be removed from exp-file). These can be safely dismissed. All they're saying is that some functions you are not using will not be compiled to the .ex4 document (which is great, as it retains the executable code efficient). However, any functions remain on your own. Supply, should you ever desire to use them in some stage.

    Appreciate!
    David

    _______________________________

    Basically, you've got two choices:

    #61623; If you want to compile using construct 509: use the connected .mqh files, and you can find the construct 509 compiler either here or here.
    OR
    #61623; If you want to use the latest MQL4 compiler: you can use the .mqh files generously created by pips4life here.

    _______________________________

    TERMS AND CONDITIONS

    By downloading and using the applications offered in this thread, you consent to the following:

    #61623; Each code is supplied FREE of charge. It may NOT be sold or distributed commercially.

    #61623; Software is offered in an'as is' basis. I'm not supplying a programming or troubleshooting service. Source code is connected if anybody wishes to make fixes or enhancements (you may also need the MQH libraries as well as the construct 509 MetaEditor/compiler as explained and connected here).

    #61623; I provide no guarantee that the software is fit for purpose, or without any errors. Download and use the code in your own risk; I accept no liability for personal harm or losses.
    https://www.nigeriaforextrading.com/...000-month.html
    https://www.nigeriaforextrading.com/...ear-diary.html

  2. #2
    See I m making a grid trading platform
    n presently theres a newbie at mql4...
    what so much I hav achived is sending autmatic order based on my calculations.
    However my system ship order again and again....

    Example
    if I run expert it ship such as this
    buy limitation 1.40000
    buy limitation 1.39800
    buy limitation 1.39600
    buy limitation 1.39400
    buy limitation 1.39200
    then it begin untill I stop pro
    1.40000
    .... . . . . .
    1.39200
    again. .

    N cant utilize break be my system will be lasts...
    so I want a loop in my specialist which run following order ship. . N remove multiple pending limitations

    and

    supose if struck 1.40000 its tp is 1.40400 n market moves
    to say 1.40200
    it send a limit order in 1.40000
    I also want a funtion that this kind of order...
    it shud only allow 1.4000 newlimit to be ship once 1.4000 open order tpis achived n it doestnt exist within an open order

  3. #3
    Quote Originally Posted by ;
    see I m making a grid trading system
    n at present theres a newbie in mql4...
    Thanks for taking the time to describe this. It seems complicated. I am too busy with other projects to undertake any new outside job, unless the task is quite simple (i.e. will take less than 30 minutes to code and test) or I believe it will benefit my trading in some way.

    If you're attempting to do almost all of the coding yourself, the following http://book.mql4.com/trading/index (additionally http://book.mql4.com/samples/expert), which clarifies the MQL4 code required to create, modify, close and delete orders, might help you.

    I've written a package that, among its many features, allows multiple entries at equally spaced levels, and may also permit simultaneous modifying and closing/deleting of numerous open/pending orders. However, it is not an EA, and hence will not babysit your trades. And it is not free (it's accessible through the industrial forum).

    Personally I do not trade grid programs. There are lots of grid programs accessible at forum. If you type'grid' to FF's search engine, you get a lot of hits. Perhaps could adapt his grid to suit your requirement.

  4. #4
    I dont think its that complicated... if we hav a verbal talk... I could expln u more.... I dont mind. . .it will barely take a couple hours...

  5. #5

  6. #6
    I've updated the code section is post #1 into the list of functions. There are now over 70 utility functions available.

    There is also an index of functions in the end.

    David

  7. #7
    Hello,

    How Can I call your StrToIntegerArray Work?
    I really don't understand how to place the factor for int a[]. How do I print a test with this function?

    #include lt;HandyUtilityFunctions.mqhgt;
    extern series TP=18,46,72;
    StrToIntegerArray(TP,?? ,,, 0);

    // ----------------------------------------------------------------------------
    //| int StrToIntegerArray(string str, int a[], string delim=,, int init=0)
    // ----------------------------------------------------------------------------
    // Breaks down a single string into integer array'a' (elements delimited by'delim')
    // e.g. series is 1,2,3,4,5; if delim is, then the result will be
    // a[0]=1 a[1]=2 a[2]=3 a[3]=4 a[4]=5
    // Unused array elements are initialized by significance in'init' (default is 0)
    // ----------------------------------------------------------------------------
    int StrToIntegerArray(string str, int a[], string delim=,, int init=0)

    for (int I=0; ilt;ArraySize(a); I )
    a[I] = init;
    int z1=-1, z2=0;
    if (StringRight(str,1)! = delim) str = str delim;
    for (I=0; ilt;ArraySize(a); I )
    z2 = StringFind(str,delim,z1 1);
    a[I] = StrToNumber(StringSubstr(str,z1 1,z2-z1-1));
    if (z2 gt;= StringLen(str)-1) break;
    z1 = z2;

    return(StringFindCount(str,delim));

  8. #8
    Quote Originally Posted by ;
    Hi,

    How do I call your StrToIntegerArray function? ....
    SorryI just saw your post now. I do not subscribe to ribbons.

    Anyhow, if you're still about, here is your answer to your query:

    Inserted Code int TP_array#91;3#93;; string TP = 18,46,72; StrToIntegerArray(TP,TP_array); MessageBox(DebugIntegerArray(TP_array)); That duplicates the content of string TP into the first 3 elements of the range TP_array, and then outputs the range articles in a message box, as an investigation:
    [0]=18.00,[1]=46.00,[two ]=72.00

    David

  9. #9
    New version uploaded, see post #1 for details.

  10. #10
    Found effect.

    Attempt gearing up to zero decimal for worth of 123.456.

    i get 123.000

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
This website uses cookies
We use cookies to store session information to facilitate remembering your login information, to allow you to save website preferences, to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners.