My MQL4 Journal

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

  1. expiated

    expiated

    Fortunately, it looks like I have a couple of additional options to Jim Dandy that weren't around back in 2014. So, my plan is to code as many indicators using step-by-step instructions as I can and see if this doesn't help me come to understand the process of writing in MetaQuotes Query Language better and better, little by little.

    Then I will go back and read all the boring material, at which time, I will probably comprehend a whole lot more of it than I would understand right now, making better sense of it because it will no longer be as if I am reading information purely in the abstract.

    The next project on my list is coding a simple Bollinger band expert advisor...

     
    Last edited: Aug 24, 2021
    #51     Aug 24, 2021
  2. expiated

    expiated

    This is a simple re-entry strategy where trades are executed when price moves back inside the Bollinger bands.

    The way the strategy works is that once a re-entry occurs, at the beginning of the very next candlestick (which will be the third candlestick in a three-candlestick series) a buy stop in the case of long positions, or a sell stop in the case of short positions, will be triggered just above or below the previous candle's high or low (which is to say, the first new candle to close inside the Bollinger band, meaning the second candlestick in the series) as appropriate. If the buy stop or sell stop is breached, the trade is executed.

    (Buy and sell stops will be set to expire at the end of the fourth candlestick.)

    To keep it simple, exits will be based on (set according to) take profit and stop loss. So, I will need to set take profits and stop losses from the entry point, basing them on a given number of deviations. (The default I am going to use in this strategy is one deviation.)

    I think that the GridSwitch.mqproj tab I kept seeing, which required me to click on the other tab—the GridSwitch.mq4 tab—in order to begin coding, is associated with a FOLDER. And therefore, I think I can eliminate its popping up by doing the following...

    Go to one of the existing main folders inside the Navigator folder (one among the indicators/expert advisors listed under the Navigator menu).

    Right click on the folder and then select "Create in MetaEditor."

    Since I am coding an expert advisor, I will select the Expert Advisor (template) option.

    upload_2021-8-25_16-18-54.png

    I am going to name this EA Bollinger Band Re-entry

    upload_2021-8-25_16-21-31.png

    No need to change anything else here…

    I am not using OnTimer, nor am I using OnChartEvent, so I can leave these unselected.

    upload_2021-8-25_16-23-5.png

    I am not using OnTester either, so I can leave this unselected as well

    upload_2021-8-25_16-23-54.png

    Now the Wizard has created the template for me—no need to click on any tabs anymore...

    upload_2021-8-25_16-25-52.png
     
    #52     Aug 25, 2021
  3. expiated

    expiated

    I wrote the code today, but I think I will retype the explanations/descriptions little by little throughout the week. I need to remember to click on "Use date" and "Visual mode" when I want to back test. Also, the back test worked only one time and then wouldn't work again until I pushed the Visual mode slider all the way to the right.

    upload_2021-8-25_20-55-33.png

    And unlike all the times I have run a back test before (in the past) the graph did not update as the tester was running, but painted the chart only AFTER the test finished. (I need to remember to click on the "Graph" tab to to get the visual as well.)
     
    #53     Aug 26, 2021
  4. expiated

    expiated

    #property strict is an instruction to the compiler to follow the MQL4 strict compilation mode that came about as a result of updates to the MQL4 language. It was introduced a while back, probably around build 600. Before this MetaTrader build number, there were a number of defaults which the language would imply, and they were not strictly good coding practice. So, MetaTrader introduced better practices in the compiler. But, to maintain compatibility with code that had already been written, they added this property. It is therefore highly recommended that #property strict always be used.
     
    #54     Aug 26, 2021
  5. expiated

    expiated

    CODING AN EXPERT ADVISOR STEP BY STEP:

    The first thing I am going to do is use input statements at the beginning of the code to set up the inputs for the indicators I will be using, which in this case, are going to be Bollinger bands. (Typically, I will usually want to input the default values that are going to be used by any indicators.)

    upload_2021-8-26_17-31-42.png

    Obviously, these input variables are in the global scope. Hence, they occur outside any other functions. In other words, variables defined in the global scope can be seen inside all other functions. This might seem like a convenient way to define variables, but this is not recommended, except that one has no choice when it comes to inputs, which must be defined in the global scope.

    I am using the first line to input the number of periods. Of course, each input statement begins with input. Next comes the variable/data type (which in this case, is an integer) int, which is followed by the name of the variable. Note that it makes sense (i.e., it seems rather logical or rational) to begin input names with the prefix: inp as a standard naming convention so that you always know which variables are input variables. The last part of the input statement is the assignment, which as I just stated, is the number of periods in this instance.

    When you fist run the expert advisor, the assignment is the default value that will appear in the input box. (Of course, you need a semicolon to finish the statement.)

    Normally, inside code, there are comments, which always begin with a double slash if they are not very/too long. But, note that if you add inline comments here (comments on the same line as input statements) they replace the name that appears in the input box.

    So then, my default assignment is twenty (20) periods, and by adding a comment, it will appear in the input box as "Bands periods," which will be a better prompt than seeing the variable name (i.e., InpBandsPeriods).

    The next input I have is the decimal number type double, which is the number of deviations, which I am defaulting to 2 deviations.

    And the third input I am using is an ENUM (or enumerator) statement, which is an applied price type, and I am defaulting it to the close price. (Actually, there is a whole series of ENUMs that are already defined in MetaTrader, though you can create your own. But here, I am using a predefined enumerator called ENUM_APPLIED_PRICE.)

    Other possible price values include open, high, low, and several others. Of course, it is normal to use the closing price, but by having this input statement here, it lets the user choose which price they are going to apply to the indicator inside the expert advisor.

    Also, although ENUMs are just integers and I could have therefore simply said input int here, by nameing this as an ENUM, when I run the expert advisor and get the input box, this will show as a drop down menu that lists the options to choose from, whereas if I simply defined it as an integer, the user would have been able to put in any number they liked, which might not have been a valid applied price.
     
    #55     Aug 26, 2021
  6. expiated

    expiated

    Next, I want inputs for my take proft and stop loss deviations, which are both double data types. I will name them InpTPDeviations and InpSLDeviations, and I will set both of their default deviation values to 1.0.

    upload_2021-8-27_15-3-16.png
     
    #56     Aug 27, 2021
  7. expiated

    expiated

    STANDARD EA INPUTS

    Because this is an expert advisor, there are some standard inputs that I will be using—inputs that I am likely to have in almost every expert advisor:

    upload_2021-8-27_16-25-11.png

    The first is the type double, which is the volume, or in plain language, it is going to be the default order size of each trade, because when you place trades, they have to be of a particular size, and it is good to let the end user decide how big this is going to be.

    It is possible that I might decide to use some algorithm that is based on my equity to calculate this, but typically, I am more likely to want to enter a fixed size here. As you can see, I am setting this value to 0.01 lots. I am not sure if I would need to change this input if I planned to use this EA to trade yen pairs. But for sure, for almost all other currencies, 0.01 is equal to 100 pips. (What!? You need to investigate this further, because this does not make any sense to you whatsoever!)

    It is a good Idea to use 0.01 because a trader would not want mistakes to happen where s/he was accidentally trading large numbers simply because s/he forgot to change this. It is much safer to accidentally trade small lot sizes rather than large ones.

    Next is a magic number, which is an integer (int) type, and which one might wish to use even if a particular expert advisor does not require it. This is because the magic number is attached to every trade when it is created. The normal use for magic numbers might be where you have more than one expert advisor running on your account, and that way, the expert advisors can identify which trades belong to them simply by finding the matching magic number.

    Also, if you enter trades at the terminal (from the keyboard) they will have a magic number of zero (0) by default, so this is a good way to identify trades entered manually compared to trades entered by an expert advisor (since there is no way to set the magic number if you are simply executing the trade via a buy or sell yourself).

    And while some coders, or rather, while some expert advisors you find on the market do not allow you to enter a magic number, it is nice to let the end user have an option for this just in case an EA turns out have a magic number that is already being used by some other EA—so that the trader can modify this number, if necessary, to make sure it is unique.

    Again, it is good to make this an input, because you never know when you might have another expert advisor that is using the same magic number, so allowing you to set this gives you more flexibility.

    And finally, I have a trade comment. Here I am using the compiler macro: underscore underscore FILE underscore underscore, and this will be a constant, which is the name of this file as it was compiled. This will attach a comment to every trade that is created, and it will appear in the trade history window.

    So then, though it is good to let the end user be able to set this, you will typically want to default it to something the describes the expert advisor.
     
    #57     Aug 27, 2021
  8. Another way of looking at this: most brokers allow a smallest trade size of 0.01 lot. So you'll have to resize your order quantity to multiples of 0.01. By making this a variable you allow for situations where the broker has a different minimum order step size.
     
    #58     Aug 27, 2021
    expiated likes this.
  9. expiated

    expiated

    I do not need anything in the OnInit section (yet). Nonetheless, if it were required, this is where I would place error handling code. (I am going to assume that I have taken care to make sure I entered the correct values above.)

    upload_2021-8-28_14-6-19.png

    I also don’t need anything here in the OnDeinit section. But, I do need to remind myself that since the word "void" essentially means "nothing," this label is used to identify functions that report back nothing to whatever told them to carry out their tasks.

    upload_2021-8-28_14-8-34.png

    So, all the work now will be in the OnTick section, with this function also being of the void data type (and will therefore have no return at the end). Here I need to remind myself that this is the Expert Advisor Start Function, so the EA will not run until this function is triggered by a tick, subsequently repeating its assigned task with each new tick (having waited upon its arrival before doing anything else).

    I am supposed to write code between the brackets following the start function to tell it what to do, and based on this strategy, I only want to trade at the beginning of each bar. So, for the first line of code between the brackets, I am using an if statement with !IsNewBar() (not is new bar) which will return true or false depending on whether this is the first time the OnTick function has been called for the bar.

    upload_2021-8-28_14-17-19.png

    So, now let me go down and write that (create the outline of the function) before I go any further. However, I should note that there will eventually be a whole bunch of additional code that I will have to add between the above "if" statement and the bool function (return value) that follows, which like above, is simply called !IsNewBar(), with no arguments…

    upload_2021-8-28_14-20-23.png
     
    Last edited: Aug 28, 2021
    #59     Aug 28, 2021
  10. expiated

    expiated

    Simply click on "New File" instead of "New Project" when using the Wizard to set up your template and you won't end up with the Local Time mqproj tab in the first place..:thumbsup:
     
    #60     Aug 28, 2021