My MQL4 Journal

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

  1. Be aware that MQL 4 and MQL5 are not identical. MQL4 code might result in "syntax error" messages in MQL5.
    Indeed. That example is a bit misleading: the OnTick() is not required here. But it is corrected in his next example, where he shows that you don't want to use an EA for showing something on a chart: you don't want to trade, you only want a custom indicator.
     
    #31     Jul 30, 2021
    Mr Subliminal likes this.
  2. expiated

    expiated

    4135TwjMthL._AC_UY218_.jpg

    Review by Ricky B.

    This is a very good resource about the new MQL4 programming standards. I’m very grateful to have it in my hands because there aren’t that many resources out there. Granted there are a few, but Andrew Young has obviously spent countless hours of blood sweat and tears pulling this together. The book is not 1500 pages long like some programming books, but believe me when I say that all 271 pages are impressively JAM PACKED and full of information! If you really want to know the ins and outs of MQL4 this is the book to get!

    I would like to give prospective readers my experience of learning MQL4 through Andrew’s book.

    I got the book and dove in. This is my first attempt to learn any sort of programming language. Because this book is so full of information I shortly realized I wasn’t going to be able to just flip through the pages and just fling out an Expert Advisor (E.A.), but I was determined to learn. I took a deep breath and committed myself to really STUDY, STUDY, STUDY and absorb this stuff.

    So, here is what I did:

    I decided to treat this as a full Collage Course. I bought myself some blank ruled index cards and as I went through the book I would write down important points to remember. In most cases every page would have several cards of data to store in my brain. Most of the concepts are pretty easy and I was getting it. After each new concept I whipped out the Meta Editor and experimented with the code and tried a lot of different things that seemed to compile correctly.

    However, a few weeks in and I was only at page 50. I already had a few hundred flash cards of important points to remember. I was retaining it and learning it, but I was feeling quite discouraged because I couldn’t see the big picture and how all of this stuff would come together. I was really feeling a lot of inner resistance and my frustration level was growing.

    On page 61 the topic of Object-Oriented Programming, (OOP) was introduced. That’s when I hit a mental Wall. I really had a difficult time. I would stare at this stuff and the thought of giving up really crossed my mind. I would stare at the pages and let the words blur.
    Mind numbing concept after concept – I thought, “Where in the Bleep is the “HELLO WORLD?” program? (laughing) I somehow kept going.

    Using the MQL4 online reference helped a bit here and there. Finally, at page 99 on the verge of giving up and throwing the book across the room - I saw a small light at the end of the tunnel when a Simple Expert Advisor is introduced. It was as if I started to climb out of a dark tunnel. I really started to get the structure of how it all fit together.

    I got to a point that I stopped trying to memorize every single blasted little thing and just kept going. I could always turn back and reference something if I needed to. I learned how to send orders to the market, how to modify them and close them. The information on indicators was good and I was finally starting to relax a bit.

    I find it superbly ironic that at the “END” of the book in chapter 24 you FINALLY get to create a simple Message Box and learn how to Comment on the screen. Really? For those Star Trek fans out there I can hear Dr McCoy’s voice on Star Trek say - Quote, “Good God Man!”…. Andrew… Andrew…. Andrew… (slapping my forehead with my palm) Why in the world couldn’t you introduce this concept at the front of the book and not at the end? It would have made my life so much easier… (light hearted hearty belly laugh).

    So any prospective readers who are new to programming, read a bit of chapter 2… THEN skip to chapter 24 and learn “a bit” about the Comment() function, Alert() function and MessageBox() function. Then, go back to chapter 2 and try some of this stuff so you can output some code to the screen. That one thing would have made my life so much easier. So don’t expect a lot of flashy stuff on your screen when your learning this stuff. It’s a lot of brain cramming memorization and finally at the end it all comes together and you get it.

    SUMMARY: I did it! I learned how to create E.A’s using this book. It wasn’t easy for a person learning programming for the first time, but it can be done. I give it a 3 stars for the learning format and 5 stars for all the material that is referenced. So to balance it out I gave it a solid performer at 4 stars. I picked up a “C” language beginner’s book and found much of it familiar. I probably should have started there. But I’m better off for having made the attempt and for ultimately succeeding.

    Despite my frustrations, thank you Andrew for writing this book. I am glad that I bought it.

    NOTE TO SELF: You already own this publication in PDF format.
     
    Last edited: Jul 31, 2021
    #32     Jul 31, 2021
  3. expiated

    expiated

    I have one more topic to cover (OnChartEvent()) before I begin looking for other sources of information. I was planning to use Jimdandy's (Jim Hodges') course available online, but it's all subdivided, with each section costing more money, and no way that I can see for me to preview what any of the "pages" actually look like in advance. So, I won't be using it after all.

    I did discover that I had the above Andrew R. Young book in PDF form, but I'm not all that crazy about it. On the other hand, I DO like the material by Coder's guru that I was able to track down, so I will start there. Unfortunately, it is not organized in the way that I teach my students and in the way that I learn best myself, which is to introduce topics as, and in the way, that they are actually used in real-time application.

    Hence, I am going to be forced to start writing about stuff in the abstract initially, which I really hate. But, I will try to stay on the alert for any ways that I can reorganize the lessons to begin putting whatever information I come across to practical use as soon as possible.
     
    #33     Aug 1, 2021
  4. ph1l

    ph1l

    You could see this if you didn't have me on ignore.:p
    https://book.mql4.com/
    upload_2021-8-1_11-18-50.png
     
    #34     Aug 1, 2021
  5. expiated

    expiated

    Also, the date on these documents is 2005, so I will need to make sure I am aware of anything (and everything) that changed in 2014.
     
    #35     Aug 2, 2021
  6. expiated

    expiated

    HOW THE ONCHARTEVENT() WORKS: GRIDSWITCH INDICATOR
    Perform the standard initial steps for creating an indicator template:

    STEP ONE: As before, click on the MetaQuotes Language Editor Icon.

    upload_2021-8-12_22-22-34.png

    When the MetaEditor opens up, click on File in the upper left-hand menu.

    upload_2021-8-12_22-23-36.png

    Select New Project, and when the MQL Wizard: Project window appears, select the Custom Indicator option.

    upload_2021-8-12_22-25-4.png

    Then click on the Next > button.

    STEP TWO: Once again, fill in the Name (but do not delete Indicators\), Author and Link. (Name this custom indicator "GridSwitch".) Then click the Next > button.

    STEP THREE: In the MQL Wizard: Project | Event handlers of the Custom indicator program window that follows, this time select the OnChartEvent to tell the program to include the OnChartEvent() function, and then click the Next > Button once again.

    upload_2021-8-12_22-26-47.png

    STEP FOUR: Next you get an MQL Wizard: Project | Drawing properties of the Custom indicator program window. As previously speculated, this "Indicator in separate window” option is probably for creating lower panel indicators, which you will not be doing. So, skip this widow once again by simply clicking on the Finish button and move on.

    STEP FIVE: If you are looking at text found under the GridSwitch.mqproj tab, go ahead and click on the GridSwitch.mq4 tab instead.

    Note that, like the On Calculte and On Deinit functions, the On ChartEvent function section includes a number of parameters between its parentheses — four of them, to be exact.

    upload_2021-8-12_22-29-24.png

    You have seen some of what you are looking at before. For example, you will recall that a constant (const) is an access specifier that prevents a parameter from being changeable.

    And of course, int refers to initialization; the term double references a decimal number; the term string signifies a word or name; and the ampersand (&) means "the address of."

    (But, what is the meaning of the term "long"?)

    Now, there are a number of different events that the On ChartEvent() function is able to detect, including:
    • Keystrokes
    • Mouse movement
    • Creation, property modification, or deletion of a graphical object
    • Clicking on the chart
    • Clicking on a graphical object
    • Editing a graphical object's text
    • Modifying chart size/properties
    • The initial or final number of an event from a range of custom events
    What you want the function to do is the fifth option in the above list, which is to say, to detect when you click on a designated graphical object that was placed on the chart.

    Again, the ChartEvent function section includes four parameters. So, when the On ChartEvent() function is called, it is sent four pieces of information (the addresses of four parameters):

    & id
    & lparam
    & dparam
    & sparam

    For what you want to do, your interest lies in the first and last parameters—the id of CHARTEVENT_OBJECT_CLICK, and the sparam, which is the name of the created graphical object (i.e., GRID).

    The "Query" in the Metaquotes Query Language means that the program will often want to know if it is dealing with "this" or "that." Accordingly, it often requires the use of if statements that constantly want to know the answer to their inquiries.

    In this case, the if statement is:

    if(id==CHARTEVENT_OBJECT_CLICK && sparam=="GRID")

    Understand that when you see the equal (=) sign, it is assigning the value of one thing to another.

    On the other hand, a double equal sign (==) compares (in contrasts) one thing to another.

    It is sort of like the equal sign is stating "this is that" and the double equal sign is inquiring "is this the same as that?" The equal sign is giving the identity of one thing to another, whereas the double equal sign is asking if that identical status already exists.

    And finally, the double ampersand (&&) does not mean: "is the address of the address of", but rather, it simply means "and." So in this instance, IF we have the designated id AND IF we have the correct name, carry out the instructions stipulated between the braces.

    So then, you need to type this monitoring piece of code between the braces that follow the On ChartEvent()'s braces.

    upload_2021-8-12_22-38-11.png

    Now you need to put opening and closing braces below this text, and type whatever it is that you want to happen if the conditions between the braces already present are detected. And of course, what you want to have happen is for the chart to show the grid.

    Other things on the chart that the program can change include...
    • Modify the scale
    • Display OHLC values
    • Display Bid, Ask, or Last values as a horizontal line
    • Display vertical lines between periods
    • Display volume
    • Pop-up descriptions of graphical objects
    • Number of bars that can be displayed
    • Total number of chart Windows
    • Chart window handle (HWND)
    • Chart window y-axis distance
    In this case, you are going to use the ChartSetInteger() function to turn the grid on by means of typing CHART_SHOW_GRID between its parentheses. When it is true, you will see the grid. But if it is false, you won't...

    ChartSetInteger(0,CHART_SHOW_GRID,true);

    This command will turn on the grid for chart zero. A computer language, true = 1 and false = 0. That is the integer or number that you are setting with this ChartSetInteger() command. Supposedly, you could put a 1 in the place of the true if you wished.

    upload_2021-8-12_22-43-56.png

    Now compile the file, then load the GridSwitch indicator on the chart. Once this is done, drop any object on the chart that you like, and finally, give it the name GRID.

    For example, use the Insert > Shapes > Rectangle menu options to place a rectangle on the main chart. Then open the Objects List and select the Rectangle. Next, click on Edit so you can change the name of the Rectangle to GRID.

    Now, if you click on the rectangle, it will turn the grid on.
     
    Last edited: Aug 13, 2021
    #36     Aug 13, 2021
  7. #37     Aug 13, 2021
  8. expiated

    expiated

    So great… I coded a custom indicator that makes it possible for me to use a graphical object to turn on the chart grid. But now, how do I turn it off again?

    Of course, I could make another rule that when I click on an object with some other name (such as GridOff) it will set the Grid to false. But, how about toggling the grid on and off using same object, so that, no matter what state the grid is in at the moment, when I click on the object, its present state will reverse?

    Doing this requires an understanding of declaring a variable. When you declare a variable, you assign it a value. You also have to let the program know what kind of variable it is dealing with.

    For example, text is referred to as strings.
    Integers are represented by int.
    And numbers with a decimal point are referenced by the term doubles.

    Since I will be using the ChartSetInteger() function to set a true or false value, I will need a variable that is of a datatype that keeps track of whether something is true or false. This datatype is called a "bool" (or a Boolean type) which refers to the last name of a famous mathematician whose algebra of logic is basic to the design of digital computer circuits. Again, I need to declare a variable of the bool type, and then give it some name.

    Since I am going to use this variable to toggle the grid off and on, it only makes sense to give it the name "toggle." So this is how I declare the bool variable called toggle and set it to false.

    bool toggle = false;

    Notice that a variable has a type, a name, and a value.

    As was the case with the sparam for the object I was clicking on before, when I declare that toggle equals false, I have to store that piece of information in a memory address. However, how long it stays in memory depends on where in the code I write it.

    If I declare it within a set of braces { }, it will only stay in memory while it is performing the block of code that is in those braces. Once it gets to the closing brace, it forgets about it, and the address is now released and available to be used again. In this way, I don't run out of memory.

    But, obviously, I don't want this to happen. Rather, I want the program to remember what state the grid is in as long as the indicator is still on the chart. This means I have to put this information outside of braces.

    Consequently, what I will need to do is put it at the top before any functions. This is called the "global" area. Any variable declared in the global area stays in memory until the program is closed.

    NOTE: DECLARE VARIABLES IN THE GLOBAL AREA!

    Here is what it looks like on my laptop...

    ScreenHunter_10564 Aug. 14 06.50.jpg

    And now I have a bool variable named toggle set to false. To use it, I need to replace the word "true" in…

    ChartSetInteger(0,CHART_SHOW_GRID,true);

    …with the word "toggle" like so…

    ChartSetInteger(0,CHART_SHOW_GRID,toggle);

    Now, if the grid is on and I click on the GRID rectangle, it will turn the grid off. However, if the grid is already off and I click on the GRID rectangle, nothing will happen. So, to turn it back on, I will need to do something more. I am going to need to use something that will change that which is false to true, and that which is true to false. The thing that does this is called a "bang" in MQL4 language, but is more commonly known as an exclamation point (!).

    So then, an exclamation point has the power to reverse the state of a Boolean variable. It is used by placing it in front of the variable to reverse its state, like this…

    !toggle

    …which is technically known as “logical negation.”

    So, instead of assigning the toggle variable the value of true or false, I need to assign it the value of !toggle (the opposite of whatever it is at present).

    Given that I want it to toggle every time I click on the "GRID" object, I have to put this line of code in the OnChartEvent() function, just before I use the toggle variable to set the grid state to true or false:

    toggle = !toggle
    ChartSetInteger(0,CHART_SHOW_GRID,toggle);

    Again, here's what it actually looks like on my laptop...

    ScreenHunter_10565 Aug. 14 06.56.jpg

    Now I will have the variable getting changed every time I click on the graphical object. If I go ahead and compile it and then try it out, every time I click on the rectangle, the grid should toggle on and off.
     
    #38     Aug 14, 2021
  9. expiated

    expiated

    CREATING A BUTTON

    Okay, so now I can place an object on my chart, give it the name GRID, and use it to toggle back and forth between grid off and grid on.

    But, let’s suppose I would rather have a button on the chart that I could click on to turn the grid off and on rather than have to create an object and give it the name GIRD in order to do so.

    Of course, I would want the button to be there automatically, as soon as I drop the custom indicator on the chart. It therefore follows that this is going to have to happen in the OnInit() function.

    The function for creating an object (such as a button) is:

    ObjectCreate()

    Between its parentheses, I need to type the button’s name ("GRID"), the type of object it is (OBJ_BUTTON), and the numeral zero (0), which instructs the program to place the object on the main chart.

    There is also a time (horizontal scale) and a price (vertical scale) parameter for specifying where on the chart to place certain objects. But, this is not necessarily required for buttons. So, I will be substituting each of these parameters with the numeral zero as a place holder.

    Here is the corresponding code…

    ObjectCreate("GRID",OBJ_BUTTON,0,0,0)

    And here is what it actually looks like on my laptop:

    ScreenHunter_10566 Aug. 15 07.30.jpg

    Now, if I compile the custom indicator and drop it on the chart, I should see a button. If I click on it, it should toggle the grid on and off, given the fact that I named it "GRID".

    upload_2021-8-15_7-47-36.png
     
    #39     Aug 15, 2021
    studentofthemarkets likes this.
  10. expiated

    expiated

    LABELING A BUTTON

    Great, now I have a button to toggle the grid on and off. However, I can see that it's a button. I don't need to have a label to tell me this. What I need is a label that tells me what the button does so that I don't have to click on the button to find out, should I happen to forget.

    So, how do I get the button to say "GRID" instead of saying "Button"?

    First of all, I need to be clear on the fact that the name of the object has nothing to do with the label on the object. So, to make the desired text appear on an object, I have to use another different function...

    ObjectSetText()

    Given that I want the object which is named GRID to be labeled with the term GRID, I will be using the following line of code:

    ObjectSetText("GRID","GRID");

    Again, I am going to want my button to already possess whatever characteristics/features I am expecting as soon as I drop the indicator on the chart, so this will once again require me to note the specifications within the initialization function…

    upload_2021-8-16_19-26-44.png

    Now, after I compile the modified indicator, my button should say "GRID" instead of saying "Button"... and it does.

    upload_2021-8-16_19-28-45.png
     
    Last edited: Aug 16, 2021
    #40     Aug 16, 2021