MQL4TUTORIAL.COM

MQL4TUTORIAL.COM

By MQL4TUTORIAL.COMBusinessInvesting
Download on the App Store

MQL4TUTORIAL.COM episodes

  • MQL4 TUTORIAL BASICS – 7 HOW TO USE SWITCH AND CASE
    In this video we want to find out how to change the control-flow of an Expert Advisor by using “switch” and “case”. In our little example here we have an output: the “customer wants RSI” and now we are going to find out how to change that by using “switch” and “case”. To do that please click on the little button here or press F4 in Metatrader. Now you should see the Metaeditor window and here you want to click on: “File/New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleSwitchCase”, click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and lets also remove the two comments here. We start with an integer variable called: “choice”, in our case the initial value for our “choice” is 5 and based on that choice we want to calculate an entry string. In our case we don’t assign any value because we want to calculate that later on. So let’s use the “switch” statement to go through our choices, let’s add two curly brackets here and everything within these two curly brackets will be executed. The first “case” is if the value equals 5 and when that is the case we want to add the text: “the customer wants RSI” to our entry – in this video we are using a plus and equal sign that has to do with a special kind of behavior that I’m going to explain later on for now let’s add a “break” statement, “break” is where we are going to leave the “switch and case” loop because the break operator terminates the execution of the nearest nested outward switch loop. Let’s add another “case” here; if our value is 4 we want to add the text: “the customer wants Bollinger Bands”, for the value 3 we want to add the text: “the customer wants MacD”, we could also do a little calculation inside of the two round brackets here but it‘s not possible to use variables that would cause an error so in case that our value is 1 plus 1 we want to add the text: “the customer wants Random entries” to our entry. Last but not least we can add a “default” option; the “default” branch will be executed when none of the above statements is true and in that case we add the text: “customer does not know” to our entry. Finally we use the “Comment” statement to create an output on our chart. So far so good, now please click on the “Compile” button here or press F7 – I have a special character here that I need to remove –, let’s recompile the code, now we don’t have any errors and no warning so we can click on a little button here or press F4 to go back to Metatrader. In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, here we pick the new file: “SimpleSwitchCase.ex4”, please enable the visual mode here and start a test. Now we get the output: “the customer wants RSI”, so let’s change the value, let’s pick 4, recompile the code, stop the current test and start a new one and now we get: “the customer wants Bollinger Bands”. Let’s pick a value that is not in our list, recompile again and now we should get the text: “the customer does not know” because 11 is not in our list and that causes the default option to be executed. Well, there is a special thing that you might not know, if we remove the “break” statements here or we simply forget them — we also change our “choice” value back to 5, now we recompile the code and now we get the output: “the customer wants RSI” and “the customer wants Bollinger Bands” and “the customer wants MacD” because the “switch-case” statement is executed from the first case that is actually true and it will run through all the options until it hits the next “break” statement. Even if our value is 5 the code for “case” 4 and for “case” 3 is executed, so let’s keep that in mind. I’m not the biggest fan of the “switch-case” statement I rarely use it but now you know how to use “switch” and “case” to change the control-flow of your Expert Advisor and you have coded it yourself with a few lines of MQL4 code. MQL4 Tutorial searches:case mql4
    6 min
  • MQL4 TUTORIAL BASICS – 6 SIMPLE IF STATEMENT
    In this video we want to find out how to check if a condition is true or false. Obviously whenever you trade with an automated Expert Advisor like this one you need to find out if something is true or false and now we are going to find out how to do that with MQL4. First please click on a little icon here or press F4 on your keyboard and now you should see the Metaeditor and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file “SimpleIfCondition”, click on “Continue”, “Continue” and “Finish”, and now you can delete everything above the “OnTick” function here. Okay, we want to start by using two integer values; “int a should be 5” and “int b should be 3”, and if “a” is bigger than 5 I would like to use the “Comment” function to output the text: “a is greater than 5” otherwise we use the “else” statement – “else” is what is going to be executed whenever none of the “if” statements above is true – and in that case we want to have the output: “Conditions not true”, that’s about it! Now we can click on the “Compile” button here or press F7 and the compiler says we don’t have any errors here, so now we can click on the little icon here or press F4 to go back to Metatrader. In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, let’s pick our new file: “SimpleIfCondition.ex4”, please mark the visual mode here and start a test. Here is our output: “Conditions not true” because this condition is not true, so let’s copy that, paste it here and this time we are going to check if “a” equals 5 and in that case we want to output: “a is equal to 5”. Please remember a single equal sign will assign the value 5 to the variable “a” and two equal signs will check if the value 5 is equal to the variable “a”. Okay, let’s go back to Metatrader and start another test, and this time our output is: “a equals 5” and that’s because this “if” statement is true, so let’s copy that and add another one. This time I want to know of “b” equals 3, if that is true I want to have the text: “b equals 3” on my chart. Let’s recompile, restart the test and yes, “b equals 3”, but I am seeing the output for the former condition where “a equals 5”, so what I also can do is I can use two “if” statements and if “a equals 5” and “b equals 3” I want to have the text: “a equals 5 and b equals 3” on my chart, so now MQL4 is going to check this expression and if that is true it is going to check the next expression, and only if both are true the code here is executed. Let’s recompile, restart the test and now we get “a equals 5 and b equals 3”. “if” only one way to check if something is true, there are more possibilities that we are going to check out in other videos. If you use several lines behind the “if” statement, for example “int c could be a plus b” and I would like to have the output for c… I would get an error when I click on the “Compile” button because now MQL4 tells me that I’m using an illegal “else” statement without a matching “if”. That can be confusing but it just means we have to use curly braces here because we execute more than one line in our “if” statement, so let’s recompile and now the error is gone and this time we should get the output: “c equals 8”. Okay, in this little video you have learned how to use the “if” statement to check if some condition is true or false and you have coded it yourself with a few lines of MQL4 code. MQL4 Tutorial searches:mql4 if statementmql4 if statement examples
    6 min
  • MQL4 TUTORIAL BASICS 5 – HOW TO DO BASIC CALCULATIONS
    In this video we want to talk about simple calculations, obviously whenever you use an automated program like this Expert Advisor you need to do calculations. For example in our case we calculate the equity or the max number of allowed positions, so let’s find out how to do that. First we need to click on the little icon here or press F4 to bring up Metaeditor and in Metaeditor we click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleCalculations”, click on “Continue”, “Continue” and “Finish” and now you can remove everything above the “OnTick” function and let’s also delete the two comment lines here. So let’s start by defining two integer variables, they should be 5 and “b” should be 3, and let’s use the “Comment” function to output: ”The result is:” followed by the calculation. In our case we start with “a” plus “b” and that’s it, when you’re done you can click on the “Compile“ button or press F7, you shouldn’t get any error messages here and if that is the case you can click here or press F4 to go back to Metatrader. In Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R, let’s pick the new file: “SimpleCalculations.ex4”, enable the visualization and start the test. And now we see the text: “The result is: 8” because 5 plus 3 is 8, let’s try a minus “b”, recompile the code, stop the test and start a new one, and now the result is 2 because 5 minus 3 equals 2. We can also try to multiply “a” and “b”, so now it’s 5 times 3 and that of course equals 15. So far so good, let’s try “a” divided by “b” and this time Metatrader says that the result is 1! – and the reason is that we have used integer variables so we can only deal with whole numbers. Let’s change the data type and use “double” and now we should get “The result is: 1.6667”. That is better! Okay, what do you think is going on when we try to calculate this expression here? “a” equals 5, 5 plus 5 would be 10, “b” equals 3, 3 times 10 would be 30, so let’s find out! – and our strategy tester says that the result is 20, and the reason is that the multiplication is calculated first, so that’s 5 times 3 and that equals 15 and afterwards “a” is added, and if you want to change the calculation order you can use round brackets and now you should get the result is 30 because everything within round brackets is calculated first and the part outside the brackets comes afterwards. Okay, that’s it for the basic calculations. If you open the MQL4 Reference by pressing F1 you will find lots of other math functions and it’s always a good idea to write such a small program here to find out how they work because in the future you are going to do more complex stuff and it will be helpful if you understand the basic stuff first because in this video you have learned how to do simple calculations and how to output the result on the chart and you have done it yourself with a few lines of MQL4 code.
    5 min
  • MQL4 TUTORIAL BASICS – 4 WHAT ARE DATA TYPES
    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.
    5 min
  • MQL4 TUTORIAL BASICS – 3 HOW TO USE THE STRATEGY TESTER
    In this video we want to find out how to use the Strategy Tester. This is an Expert Advisor, an automated program for Metatrader 4 and its running with historical data that is called a strategy test or a back test, and we want to find out how to use the Strategy Tester. Last time we have created this simple template here, it just creates an output: “Hello MQL4”, so let’s click on; “View/ Strategy Tester” or press CTRL and R. Now you should see this “Strategy Tester” panel here, here we can pick an Expert Advisor, in our case the “SimpleEATemplate”, we also pick a “Symbol”. A Symbol is a currency pair in our case but you need to have historical data for the currency pair you choose otherwise the strategy test will not start, so let’s click on: “Tools/ History Center” or press F2 and here we see we have data for 1 minute charts for the currency pair that we have chosen, but when you scroll down here you will see that we have some data for 2017, and we have data for 2013 but we couldn’t start a strategy test for 2014 because the data is missing. There are some websites where you can download data, like: histdata.com, they offer 1 minute data for Metatrader 4, just pick the currency pair you need, select the year, in our case it’s 2014 and click on the download link and once the data has downloaded you will see that it contains a coma separated file, let’s open it and here we are! This file contains all the data for each minute of 2014 – I have already downloaded a lot of those files – but in our case we just pick the year 2019, I have selected the 1 minute chart, the spread is set to 10, we will trade every tick and to see what’s happening just set the mark here to enable the visual mode and start your test. Ooh… I need to change the year, let’s start the test again and this time it runs. Not very spectacular, we just see the text: “Hello MQL4”, so let’s exchange that with the output: “Local Time: “, there is a function for that which is called: “TimeLocal”, let’s recompile the code, start the test again and this time we have a rising counter here that is calculated with each price change on the chart and in this video you have learned how to use the Strategy Tester, how to use the function: “TimeLocal” to create a dynamic output directly on your chart and you have coded it yourself with a few lines of MQL4 code.
    4 min
  • MQL4 TUTORIAL BASICS – 2 WHAT IS A FUNCTION?
    In this video we are going to talk about functions that can be used for an Expert Advisor, an automated program that runs in Metatrader 4. In our case we are using the Forex Trading Framework in the MQL4 version and in the last video we started by creating a simple template using the Metaeditor, so let’s click on the icon for the Metaeditor or press F4. This is the template that we have created, it’s already complete I can compile it, that means it’s translated for the Metatrader. When we use Metaeditor we create an “mq4” file, that’s the source code file, you could also open this file with Notepad or Notepad++ and it would be readable but if you compile the file it will create an executable file for Metatrader 4. This has the extension “ex4” and when we try to open this file in Notepad we get a lot of messy stuff because this file is already compiled and humans can no longer read it, but it’s easy to read for Metatrader 4. So the “OnTick” function is called whenever we have a price change on the chart and these two round brackets here are used for so called parameters, so whenever you see something like this in combination, it’s most likely a so-called function. You may know functions from your calculator, when you click on one of those buttons here you have called a function and it also uses the round brackets here, so let’s use another one the: “Comment” function. You see as I’m typing Metaeditor is going to complete the word: “Comment” here, that is called auto-complete function, when I type: “Com” it already knows there is only one function that is already existing, so let’s click on the “Comment” suggestion here and you see we get the same color as the “OnTick” function, so let’s use a round bracket and I would like to see the output: “Hello MQL4” and this is a text so I need to use quotation marks and at the end of the line we are going to finish our statement by using a semicolon. This is for the compiler, so the compiler knows that this line is completely finished. It would be also possible to use multiple lines like this way, the compiler would still see everything from the last semicolon to this semicolon as a complete statement, so let’s click the “Compile” button or press F7 and now the compiler tells us we don’t have any errors and we don’t have any warnings here, so let’s drag the Expert Advisor on to a chart and when the next price change comes in we see the words: “Hello MQL4”. Now you know how to use the “Comment” function with one parameter, you know that the semicolon is the end of a statement, you know the difference between an “mq4” file and an “ex4” file and you know how to create a text output on your chart and you have done it yourself with a few lines of MQL4 code.
    5 min
  • MQL4 TUTORIAL BASICS – 1 WHAT IS INSIDE OF THE EA TEMPLATE
    In this video we want to talk about the MQL4 template that is used to create a basic Expert Advisor and we also want to talk about the content inside of that template. An Expert Advisor is an automated program that is able to do everything that humans also could do. This is one of those Expert Advisors, its trading right now; it is able to open and close positions without any human interaction and to create one of those Expert Advisors you would click on this little icon here or press F4 on your keyboard. This is the programming environment that comes with Metatrader 4 and MQL4 and to create a new Expert Advisor we can use the wizard, we just need to click on: “New/ Expert Advisor (template)” from template, now we click on “Continue”, I will call this version: “SimpleEATemplate”, click on “Continue”, “Continue” and “Finish”. And here is our template! The upper part contains some information about the name of the template, the copyright and the website that we have provided in the process, these double slashes are comments, so everything that comes behind double slashes is ignored by the complier it’s just for us humans to make things more readable Here we have some properties for the copyright, the link, the version and there is also a property called: “strict”. If you mark the property expression and hit the F1 key you will see the help file and it will explain that a property is additional and it’s a specific parameter. For example “strict” means that the compiler should use the strict compilation mode, the copyright property would be used for the company name and so on. Inside of the template you will find an “OnInit” and an “OnDeinit” and an “OnTick” function. These are built-in functions, let’s mark “OnInit”, press F1 and we learn that the “OnInit” function is used for initialization because it’s the first thing that is running as soon as we start our Expert Advisor. “OnDeinit” is the function that will run before you close your Expert Advisor; you can use it to clean up before you close your Expert Advisor. The most important function is the “OnTick” function because this one will run every time when a new tick for a symbol is received and that is always the case when the price changes. You will find further functions like: “OnTimer” and “OnTester”, if you are interested just open the MQL4 reference by pressing F1. For our simple example we can remove everything above the “OnTick” function because this one is the one that is most interesting, we can also remove the two comment lines here and everything between the two brackets here will be executed each time when the price changes. Right now you could click on the “Compile” button here and you would already have a working version. So far it doesn’t do very much; we are going to change that in the next few videos. In this video you have learned how to create a new empty Expert Advisor that is created from a template and you have done it by generating a template with a few lines of MQL4.
    5 min
  • MQL4 TUTORIAL – WHERE TO DOWNLOAD METATRADER4 IN 2019
    This is an automated back test for a so called Expert Advisor, I’m using Metatrader 4, right now it’s the beginning of 2019 and Metatrader4 still is around. I used to automate it since 2014 and there are a few outdated videos so I will start over this year and do it all again. To get started you need to download Metatrader 4, you could do it before by going to metatrader4.com but it isn’t available there, the site is still around but if you start the download you will see it here with the pop-up text it’s going to download Metatrader 5 and the reason is that Metaquotes wanted everybody to switch to Metatrader 5 about two years ago but there are still a lot of brokers who refuse to switch because they have clients that like to use and continue to use Metatrader 4. I have just checked the Google search console and I see that the traffic is indeed increasing for mql4tutorial.com so obviously there is still a lot of demand for Metatrader 4 automation and if you are just starting out you could visit mql4tutorial.com and you will find a download link for the Metatrader 4 setup file. Let’s download it and as you can see it really is Metatrader 4 and when you download and open it you will see that it is actually going to download Metatrader 4 and install Metatrader 4, so if you are looking for the download file this is the one to get, just go to mql4tutorial.com click on: “METATRADER4 DOWNLOAD” and we are going to continue and start with automated trading. MQL4 Tutorial searches:metatrader 4 mql4 tutorial
    3 min
  • MQL4 TUTORIAL – THE SECRETS OF SUCCESSFUL TRADING
    My daily updated real account results: https://www.myfxbook.com/statements/2559459/statement.html In this video I wanted to talk about a presentation that I have created for the last I think about two weeks, you can find it on: robottradingsystem.com and it contains so much useful information that I think that you might want to watch it. Actually it’s over two hours long, most of it is pure content, I talked about my numbers and my systems, this slide is showing the results for the Robot Trading System on I think September the 28th because that’s what it says here. Let’s visit the website, click on the real time account statement, today we have October the 11th 2018, let’s click on the Robot Trading System to show the current results and currently we have made 7.67%, the profit is 3,819 Dollars and 86 cents, that’s a little bit more than you see here and if you are interested we are going to talk about the 5 steps to develop a trading system that works. It doesn’t matter if you are ever going to buy something from me, I think that the content is something that you might find very interesting and it should help you even if you never consider being an automated trader. Here is a little spoiler; the one big breakthrough that actually became the foundation for the Robot Trading System can be found in the book: “Trade your way to financial freedom” by Van Tharp, Van Tharp once talked to a guy named Tom Basso and this guy thought it was possible to make profits with a random based entry if the other components are good enough. Let’s do that live, we will test it for the year 2018 up to the current day, let’s click on start, this shouldn’t take very long. Okay, in this case we made 50,249 Dollars end-result, that’s a total net profit of: 255 Dollars and 19 cents. Let’s repeat the test, this time it says we made: 125 Dollars and 59 cents and because of the random entry we have different results for the same time span. So if you are interested to find out the details and watch the results for the Robot Trading System for the last two weeks just follow the link and watch a little over 2 hours of video content to find the solution that works for you.
    4 min
  • MQL4 TUTORIAL – SIMPLE SMA STANDALONE EXPERT ADVISOR
    In this video we are going to talk about a standalone Expert Advisor that is going to actually trade. Whenever the price is below this yellow line here – this is a Moving Average – we have already made a sell trade and in this video we are going to find out how to do a standalone Expert Advisor that actually trades in MQL4. To do that please click on the little button here or 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, “Continue” (Next), I will call this version: “SimpleStandaloneSMA”, click on “Continue” (Next), “Continue” (Next) and “Finish”. Now you can remove everything above the “OnTick” function and let’s also delete the two comment lines here. Okay. Let’s start by creating a string variable for the signal, that variable will be also called: “signal” and we don’t assign a value here because we are going to calculate it later on. To calculate it we use the built-in “iMA” function that comes with MQL 4 and it will calculate the Moving Average for the current symbol on the chart and the currently selected period on that chart – for example I’m on the 1-minute chart right now – we want to calculate the Moving Average for 20 candles, I don’t want to use a shift value, I could move the Moving Average drawing to the right or to the left but we are going to use a zero (0) shift value here. “MODE_SMA” will calculate the Simple Moving Average, “PRICE_CLOSE” we’ll use the close price for the calculation and the last value here is candle 0 (zero) – that’s the current candle – I could also calculate the Moving Average based on one of the other candles here but in this simple version we are going to use the current candle. And if the Moving Average is below the current price – so if my Moving Average (MyMovingAverage) is below the close price for candle 0 (zero)… …and please notice that close for candle 0 (zero) actually doesn’t exist because candle 0 (zero) is always the candle that is just in development and close price for candle 0 (zero) gives you the current price here and when that price is bigger than the Moving Average value we want to set the signal variable to buy so we assign the word: “buy” to our signal. And guess what? If the Moving Average is above the current price, so if my Moving Average (MyMovingAverage) is bigger than the current price we want to set the signal variable to sell so we assign the word: ”sell” to our signal. Now let’s actually buy something! When the signal equals buy and “OrdersTotal” equals 0 (zero) – and that is true when we have no open orders – we use the “OrderSend” command to buy 10 micro lot and the take profit value is 150 points above the Ask price. If you are interested in the “OrderSend” command just mark it here and press F1 and you will see how it works, but let’s continue with the selling part! To sell 10 micro lot we check if the signal equals “sell” and if we have no open orders and in that case we use the “OrderSend” statement to sell 10 micro lot and this time the take profit value will be 150 points below the Bid price. Please notice that we use “OP_BUY” to buy something and “OP_SELL” to sell something. That’s almost it. In the last step we use the “Comment” function to output the words: “The current signal is:” and the calculated signal on our chart. Okay, so far so good, please click on the “Compile” button or press F7 on your keyboard, you should see no errors and we have 2 warnings because we could use the return value for “OrderSend” but for this simple example everything is okay, we don’t use that so now please click here or press F4 to go back to Metatrader. In Metatrader we click on “View/ Strategy Tester” or press CTRL and R, here in the Strategy Tester please select the “SimpleStandaloneSMA.ex4” file, mark the option for the visual mode and start your test. …and here is our little Expert Advisor at work, we have already made a sell trade because the price is bel...
    6 min

About MQL4TUTORIAL.COM

From the publisher's feed

MQL4 Tutorial for Metatrader 4 or MT4 about automated trading, trading robots, MQL 4 and MQL programming