My MQL4 Journal

Discussion in 'Journals' started by expiated, Jul 14, 2021.

  1. expiated

    expiated

    What is and indicator?

    An indicator is a graphic drawn on a chart or information provided to viewers when a given set of rule(s)/events has been satisfied/occurs.

    Examples include:
    • draw such and such moving average
    • when this line reverses its slope, draw an arrow pointing in the new direction
    • sound an alarm when…
    • send a push notification to my iPhone when…
    Unlike a script, which acts "in the moment" just one time and then goes away, an indicator monitors conditions on an ongoing basis, remaining on the chart until removed. Moreover, not only does it "do its thing" with respect to the present and the future, it also goes back and does so with respect to everything that already happened in the past. It does this as soon as it is introduced to the chart, but does not continue operating until the next tick (i.e., price change) is triggered, at which point, it reassesses the situation to determine if anything else needs to be done. In other words, it makes choices based on if/then rules.

    So, whereas scripts and expert advisors are "dropped onto" a chart, in indicator is "inserted into" a chart. It becomes an actual part of the chart.
     
    #11     Jul 18, 2021
  2. expiated

    expiated

    What is an expert (electronic) advisor (EA)?

    An EA is a combination of a script and an indicator. It has a set of rules to be followed for making decisions, along with a script to open, close, and modify trades (of its own volition, without necessarily informing the trader/programmer) if and when told to do so.

    Though an EA will not automatically draw graphics on the chart, it can be made to do so. But if you do, such objects (i.e., an arrow, a line, etc.) will be placed ON the chart as if put there manually, and can therefore be moved around using the mouse.

    Also, the EA does not begin to run immediately, but waits instead, until it is triggered by a tick. In addition to deciding when to open and close trades, EAs can...
    • decide what lot size to use based on certain criteria
    • group some trades together into a basket (and treat them as one jumbo trade with a shared [the same] stop loss and take-profit target)
    • manage trades
    When it comes to this last type of EA (one that manages trades) the trader himself or herself must enter the position, at which point, the EA takes over, adjusting stops, etc., until the trade is closed. There is no actual trigger for initiating the corresponding trade.
     
    #12     Jul 19, 2021
  3. expiated

    expiated

    How is the code constructed?

    Apparently, whenever coding something in Metaquotes Query Language, you need to begin with something called a directory (I think). If this is not in error, then before March of 2014 (when I began learning what I already know about coding). The directory looked like this:

    Metatrader/Experts/Scripts/script.mq4
    Metatrader/Experts/Indicators/indicator.mq4
    Metatrader/Experts/expert.mq4

    But after that date, this is what the directory looked like:

    Metatrader/MQL4/Experts/expert.mq4
    Metatrader/MQL4/Scripts/script.mq4
    Metatrader/MQL4/Indicators/indicator.mq4

    So, what does all this mean? I will try to decipher it, and then record whatever speculations result in my next post...
     
    #13     Jul 24, 2021
  4. expiated

    expiated

    From what I've read, I take it that before March of 2014, the expert directory (/Experts/?) contained not only the file labeled: expert.mq4, but also contained the script sub-directory (/Scripts/?) and indicator sub-directory (/Indicators/?).

    upload_2021-7-24_17-23-11.png

    So then, it would seem that files (.mq4?) were contained within both directories AND sub-directories; and that in addition to files, sub-directories were ALSO contained within directories.

    However, I gather that after March of 2014, everything—both directories AND files—were contained within the folder labeled: Metatrader/MQL4, with the previously mentioned files located under the directories...

    upload_2021-7-24_17-28-19.png
     
    #14     Jul 24, 2021
  5. They did that because that's when they introduced MQL5. The MQL5 directory structure is the same as that for MQL4. Now you can separate the MQL4 and MQL5 code base.
     
    #15     Jul 25, 2021
    expiated likes this.
  6. expiated

    expiated

    THE OLD START FUNCTION:

    As you know,. f.(x) is often used as the symbol for a function when working in calculus. Similarly, when writing in Metaquotes Query Language, the expression start(), with the indicated term and accompanying punctuation marks constitutes the function that is named start.

    The old start function looked like this:

    Void start ()

    {
    Xxxxxxx xxxxxx xxx xxxxxxx xxxxx xxxxxxxx;
    }

    (Note: Different parts of the text will be different colors when actually writing code for reals.)

    The word "void," which essentially means "nothing," reflects the fact that this type of function reports nothing back to whatever tells it to carry out the task it is meant to do.

    The start() function belongs in a special function category in that it does not require a command to begin running (i.e., to "start" working). Rather, it is triggered by the occurrence of a given event, which can be as simple as it's being dropped on a chart, at which point, the function begins running immediately.

    The text between the braces { } is the block of code that tells the function what to do as soon as the event that triggers this start section occurs.

    The semicolon at the end of the text operates much like a period, or full stop, and is critical in the sense that the program will not work without it (i.e., it will return an error message when you try to compile it, which is to say, when you click on the button to create a new file after having finished writing your code).

    But, the problem with the old start function was that it looked exactly the same, whether it was being used with a script file, an indicator file, or an expert file; so that one would have to analyze the text to figure out which kind of file it was, because they ALL had a start() function in them, and they ALL ended with the .mq4 suffix.

    In the next few posts, I will begin to take a look at the new start function(s), and attempt to explain about (i.e., understand) how the start function changed after March 2014 in ways that make it possible to distinguish between its use with a script, indicator, advisor, initializer, de-initializer, timer, tester, etc.
     
    Last edited: Jul 25, 2021
    #16     Jul 25, 2021
  7. expiated

    expiated

    THE NEW START FUNCTION FOR SCRIPTS:

    Given that a script begins running as soon as it is dropped on a chart, the new name for the Scripps start function is: OnStart(). Note that the letters must be capitalized exactly as illustrated. Also, since the data type for the script start function is...void, the function is coded as follows:

    void OnStart()

    {
    xxxxx xx xxxx x xxxxxxx;
    }
     
    #17     Jul 25, 2021
  8. expiated

    expiated

    THE NEW START FUNCTION FOR INDICATORS:

    Though being dropped in a chart is the initial event that causes an indicator to run, from that point on, it is in "tick wait" mode and will not do anything more until and unless a "special event" (i.e., a new price) comes in.

    It is this subsequent event that causes the function to recalculate everything. It performs this recalculation for the new updated price, hence the new name for the indicators start function is...

    OnCalculate()

    When the function finishes running, it returns a number, or integer. So then, the datatype for the indicators start function is…int, so that the function is coded as follows:

    int On Calculate()
    {
    Xxxx xxxxxxx xxx xxxxxxx xxxxxx xx xxxxx;
    return(rates_total);
    }

    In a real file, this "special event" driven OnCalculate() function looks like…

    int OnCalculate .(..const int rates_total,
    ...const int prev_calculated,
    ...const datetime &time[],
    ...const doubled &open[],
    ...const doubled &high[],
    ...const doubled& &low[],
    ...const doubled& &close[],
    ...const long &tick_volume[],
    ...const long &volume[],
    ...const long &spread[] .)​
    {
    Xxxxx xxxxx xx xxxxxx xxxx x xxxxxx;
    return(rates_total);
    }

    Note that there is a list of constants (const) that go between the parentheses, and that they are separated by commas. (The const is a type of "access specifier" that prevents a parameter from being changeable, which is to say, it keeps it constant—or constantly maintained at whatever it was originally set to be.)

    At the end, the function returns the rates_total.

    The rates_total typed inside the parentheses following "return" is the information that the indicator start function provides or delivers. It is the integer returned on every run of the function, and is the reason OnCalculate() is of the int data type.
     
    #18     Jul 26, 2021
  9. expiated

    expiated

    THE NEW START FUNCTION FOR EXPERT/ELECTRONIC ADVISORS:

    Since an advisor will not run until it is triggered by a tick (performing its task when a tick comes in and then waiting for the next tick before doing anything else), and given that the EA start function is of the void data type (and will therefore have no return at the end), the new start function for EAs is named...


    OnTick()


    ...and it looks like this:


    void OnTick()
    {
    Xxxxx xxx xxxxx x xxxx xxx;
    }
     
    #19     Jul 26, 2021
  10. expiated

    expiated

    THE RETURN FUNCTION

    Besides being used to send information somewhere, the return function can also be used to fill yet another role. This is because the word—return—will stop a function from continuing any further and abandon what it was doing, then wait to restart on the next tick event.

    So, if at some point during or within the function, the program discovers that a condition exists such that it is no longer necessary to go any further (such as getting a signal to open a position when one is open already) you can simply insert the statement... return;

    It will then ignore its remaining lines of code.
     
    Last edited: Jul 26, 2021
    #20     Jul 26, 2021