MQL4 TUTORIAL BASICS – 4 WHAT ARE DATA TYPES

video
play-sharp-fill

In this video we are going to talk about data types, you can see a lot of data here on the chart.
Data is used whenever something is automated in an Expert Advisor and it’s important to choose the right kind of data type to get the proper output, so let’s find out how to do that.
We start by clicking on the little icon here or you can press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, let’s click on “Continue”, I will call this version: “SimpleDataTypes”, click on: “Continue”, “Continue” and “Finish” and now we can delete everything above the “OnTick” function and the two comment lines here.
Let’s start with the most obvious data type, this is for text values, we will use a so called “string” variable, in our case the words: “The current value is:” are assigned to our variable called: “Text” and probably you have already seen the data type variable in one of the previous videos, this one is used to output time, it’s called: “MyTime” and I’m using a function here that is called “TimeLocal”, it says that it will return the time of a computer were the client terminal is running, so let’s use the “Comment” function to output our “Text” followed by the value and when you are done you can click on the “Compile” button or press F7, we don’t have any errors here so now we click here or press F4 to go back to Metatrader.
In the previous videos we have learned how to use the Strategy Tester, so let’s click on: “View/ Strategy Tester”, pick the new file for the data types, enable the visual mode here and start a test.
Now we get the output: “The current value is:” followed by a strange number, we would have expected the time here but it’s not in a format that we can read. This is also possible, we are going to talk about that later in a video about typecasting, but let’s continue with data types.
We use the data type: “double” whenever we are dealing with floating type values, for example for the balance we need to have a few digits behind the dot, we just add the 12 cents in our source code, let’s see how the new balance (NewBalance) value will look on the chart, restart the test and this time our output is: “The current value is: 100,000.12” but we don’t always need floating type values, for example the account number is a whole number, we get it by using “AccountInfoInteger” for the account log in (ACCOUNT_LOGIN), let’s recompile, start a new test and here we have our account number, it’s: 1652880, so whenever you deal with whole numbers you can use the data type” int” and when you want to know if something is true or false we would use the “bool” type, “bool” type is intended to store logical values of true or false and it will create an output of 1 or 0, so let’s put this one into the “Comment” line, recompile, restart the test, “The current value is: 1”, that stands for true, let’s change that to false and now we get: “The current value is: 0” that stands for false.
There are a few more data types available, just press F1 and search for data types and you should find some more, it’s always a good idea to write short programs to find out what’s going on and in this little video you learned how to create outputs for different data types and you have done it yourself with a few lines of MQL4 code.

Download “CODE - SIMPLE DATA TYPES”

SimpleDataTypes.mq4 – Downloaded 141 times – 307.00 B