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.
When the MetaEditor opens up, click on File in the upper left-hand menu.
Select
New Project, and when the
MQL Wizard: Project window appears, select the Custom Indicator option.
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.
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.
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.
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.
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.