MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 Tutorial -Simple Object Oriented Label
    In this video we are going to create a simple object for a label. This is a dynamic label that will show us the Ask Price and this label is an object and we are going to use the Object Create function and a few objects set functions to create the exact label you see here. So let’s create an MQL 5 Expert Advisor that is going to create an object for us. To do that, please click on the little button over here or press the F4 key on your keyboard. And now you should see the MetaEditor and in the Meta Editor you want to click on File, New, Expert Advisor from Template, Continue, call it Simple Object Label, Continue, Continue. Now we can remove everything above the Ontick function and we want to delete the two comment lines here. And because we need something to show, we are going to get the Ask Price. That can be done by using the function symbolinfodouble, for the current symbol and the current Ask Price and we use that digit here, because some symbols have only three digits after the dot, while other symbols have five digits. And now we want to create an object by using ObjectCreate for the current symbol. We will call it Label One and we will use the constant OBJ_Label, because that is the constant we need to use to create a label and we could choose a lot of options here. And we don’t need a sub window, we don’t need a specific time, and we don’t need a specific anchor point for a price. So that’s it and after we have created our label we want to use the function ObjectSetString for the current chart and the project we have called Label One. And with OBJProp_Font, we can set the font to any kind of font we like. In the next step we want to set the font size by using ObjectSetInteger on the current chart for the Label One object by using OBJProp_FontSize and the size of the font should be twenty four, and with ObjectSetString and OBJProp_Text, we will assign this text here and the value of the Ask variable we have calculated here. And now we want to set the distance from the left border by using ObjectSetInteger for Label One and we will use OBJProp_Xdistance and a five and with OBJProp_Y distance ten we would set the distance from the upper border. Okay that’s it, now please click on compile. We have zero errors and three warnings here, because we used implicit conversion from String to Number, Number to String and String to Number but for this simple example it will be okay. So please click on the little button over here or press your F4 key to go back to Meta Trader. And in Meta Trader please click on View, Strategy Tester or press the Control and the R button. And in the Strategy Tester you want to select the Simple Object Label.ex5 file, please make sure to enable the visualization mode here and click on start. And now you should see the Ask Price in the upper left corner here. And now I want to change the X distance here, that’s the distance from the left border and after I recompile the code and start a new test you see that our object has moved. Now let’s change the font and the font size, recompile the code. Start a new test and now we have a bigger font and a completely changed font type. Okay, now you know how to create an object for a label, to use it in your MQL5 Expert Advisor for your Meta Trader5 and you have created it in less than five minutes with a few lines of MQL5 code.
    6 min
  • MQL5 Tutorial – Simple Triple Moving Average Robot
    This is the Trix Indicator. It is an Oscillator, that means that most of the time it is used with an indicator to predict if a trend is going to continue. If the value of the Trix Oscillator is above zero that could be a sign for long trend, and if it’s below zero, that could mean that we have a short trend. According to Wikipedia, the Trix Oscillator was developed in the 1980s by a guy called Jack Hutson and a rising or falling line is an uptrend or downtrend. And Trix shows the slope of that line so it’s positive for a steady uptrend, negative for a downtrend and a crossing through zero is a trend change. And if we zoom in, we see that the crossing here actually was the start of a big short trend. Now how can we create an Expert Advisor that is going to use the Trix Indicator? To do that, please click on the little button here or press the F4 key. That would show the Meta Editor and here you want to click on File, New, Expert Advisor from template, Continue. I will call it SimpleTrix, Continue, Continue and Finish. And now you can remove everything above the Ontick function and we are going to delete the comment lines. And now we want to create an Array for several prices. It will be double Array with the name My Price Array. And now we want to define the properties for the iTrixEA. For our iTrix Definition we will use iTrix, that is a command that comes with MQL 5, we will use it for the current currency pair on the chart and the selected period on the chart and it will calculate a value based on the last fourteen candles based on the close price. Because that is the default value when you click on properties. Here we have the period of fourteen candles and here is the Close Price. Now that we have made our definition, we want to sort the Price Array from the current candle downwards that is done by using Array Set as Series for our Price Array we have created here. Now we want to fill it with data by using CopyBuffer according to the iTrix Definition we have created here. Our indicator has just one line, that’s the zero here and we want to fill it with values from the current candle, that’s candle zero, for three candles, that’s the three here and we want to store the results in our Price Array. And now we want to get the value of the current candle. I will use a float variable here, I call it iTrix value and it will hold the value of candle zero. That’s the current candle of our Price Array. So let’s create a chart output depending on the value and if our calculated iTrix value is greater than zero, we want to use the comment function to show the text iTrix is long and the iTrix value directly on our chart. In the other case the iTrix value is below zero, we use comment to show the text iTrix is short and the iTrix value. Okay that’s it, please click on compile or press the F7 key on your keyboard, that should work without any errors. We have one warning here, it says we could have a possible loss of data and that’s because we have used the float type here. But that’s okay for this simple example. So if your compilation process was successful, please click on the little button here or press F4 to go back to MetaTrader. And in Meta Trader you want to click on View, Strategy Tester or press Control and R. And in the Strategy Tester you want to select the file Simple iTrix.ex5, mark the visualization option here and click on start. And here we are, our chart says iTrix is long and we have a little long trend here and when it crosses the line here it says iTrix is short. Okay now you know how to create an Expert Advisor that is using the iTrix Indicator to show you a a long or a short trend and you have coded it yourself with a few lines of MQL5 code.
    6 min
  • MQL5 Tutorial – Simple MacD Trading Robot
    This video is about the MacD. The MacD is an old indicator, it was created in 1979 by a man called Gerald Appel. And when the MacD is above this line here, this could be a signal for a long trend and when the signal is below the line it is trending downwards. You also might notice that the crossing of these two lines here also is a good indication that it might go into the opposite direction now. We will use that in the advanced strategies but for now, we want to create a simple MacD Expert Advisor that will tell us if it is going up or if it is going down. This is the very complicated MacD example in the MQL5 reference, but we will create a very simplified version now. To create the Expert Advisor now please click on the little button here and now you should see the Meta Editor and here you want to select File, New, Expect Advisor from template, Continue. I will call it Simple MacD, Continue, Continue, Finish. Now you can remove everything above the Ontick function and I will delete the two comment lines here. So let’s create an array that can hold several prices and I will call it my price array. And we also need to create a MacD definition by using the function iMacD. It will use the current currency pair on the chart and currently on the chart selected period. And if you click on Insert, Indicators, Oscillators, MacD, you will see that it is calculated for a fast E.M.A. That is an Exponential Moving Average for twelve candles, that is what we use here. It also calculates a slow Exponential Moving Average for twenty six candles so we use twenty six for the next parameter. The MacD Simple Moving Average is calculated for nine candles, that’s the nine here and we want to calculate our value for the close price so we use Price_Close for the last parameter. Okay let’s sort the price array from the current candle downwards by using the ArraysetasSeries function for the price array we have created here. And now we use CopyBuffer for the defined moving average in our MacD definition. We want to calculate one line, so we use a zero for the first buffer here and from the current candle that’s candle zero, for three candidates that’s the three here we want to store our results in the price array. And now we want to get the MacD value of the current candle, that is candle zero in our price array. And we will assign the value to the variable called MacD value. I use a float instead of a double value here because a float type will save us some memory and it’s precise enough to find out if we are above or below the zero line. So let’s create a chart output depending on the value of the MacD, and if our calculated MacD value is greater than zero, we want to use the comment function to print the text, “Trending Upwards” and the MacD value directly on our chart. In the other case the MacD value is less than zero; we want our output to be “Trending Downwards” followed by the MacD value we have calculated here. Okay that’s it, please click on the compile button here or press the F7 key on your keyboard. That should compile your Export Advisor. If everything is okay please press F4 on your keyboard or click on the little button here to go back to Meta Trader. And in MetaTrader you want to click on View, Strategy Tester or press control and R to bring up the strategy tester panel. And here we want to select the simple MacD.ex5 file, pick any currency pair and mark the checkbox for the visualisation option before you start your test. The MacD is below the zero line when we have a bearish trend and it is above the line when we have a bullish trend. Depending on the value it will show you the words “Trending Downwards” below the line or “Trending Upwards” above the line. Okay now you know how to create an Expert Advisor for the MacD Indicator and you have created it yourself with just a few lines of MQL5 code.
    7 min
  • MQL5 Tutorial – Simple RVI Trading Robot
    This time we are using the RVI indicator to spot trends and corrections. Well, you know in every up or down trend you will always have the main trend and you will have corrections. This line here stands for the average price, right now we are in a down trend and if the two lines cross, we have a little up correction. And if the green line crosses the dotted line here the downturn might change and become an up-trend. The RVI indicator is integrated into your MetaTrader and if you click on Insert, Indicator, Oscillator, and you select the relative Vigor index you will get the RVI for ten candles. And this is how it looks like and it is also what we use here and you can use it to find out when the direction changes. So right now we are in a little up-trend and when the green line crosses the dotted line, that might be the end of the trend. Now how can we create an Expert Advisor that is going to use the RVI index? To do that, please click on the little button here or press the F4 key, that will bring up the Meta Editor. And here you want to click on File, New, Expert Advisor from Template. Please click on Continue, I will call it simple RVI, now click on Continue, Continue and Finish, and delete everything above the Ontick function. Let’s remove the two comment lines here and this time we are going to start with an empty chart output. And we need to create the price array for several prices, I will call this one myPriceArray0 and I create another one that will be called myPriceArray1. I do this because our indicator has two buffers, and now I will create an RVI definition using the function iRVI that is built in with MQL 5 for the current symbol and the selected period on the chart, for ten candles. Because if you right click on the properties of the relative Vigor index you will see that it also uses ten candles here. And now I use Array SetAsSeries for myPriceArray0 and myPriceArray1, to sort the price from the current candle downwards. And now we want to use Copy Buffer with the RVI Definition we have created. For the first line that is buffer zero and the other line that is buffer one to fill the arrays with prices from the current candle zero for three candles. And now we want to get the value for the current candle, that’s candle zero in our PriceArray0 or in our PriceArray1 and store the results in two different variables here to calculate the green line and the red line. And now we want to create a chart output, the first pair is for the main trend and if the RVI value zero is below the RVI value one and the RVI value one is also below zero that would mean we have a downtrend. In the other case the RVI value zero is greater than the RVI value one and the RVI value zero is greater than zero, that would be an up-trend. Now let’s insert the values for the correction part, if the RVI value zero is greater than the RVI value one. And if the RVI value one is still below the dotted zero line, we have an up correction in the down trend. And if the RVI value is below the RVI value one but the RVI value zero is above the dotted zero line, that would indicate a down correction in the bullish trend. Now let’s click on the compile button or press the F7 key on your keyboard and with F4 or a click on this little symbol here, you can go back to your MetaTrader. Back in Meta Trader please click on View, Strategy Tester and in the Strategy Test panel please select the simpleRVI.ex5 file, mark the visualisation option here, click on Start. And this is how it looks like, we just have crossed the dotted line here so that would be a sign for a down-trend. Now we have an upwards correction in the down-trend because both lines here are still below the dotted line and now the upwards correction became an upwards trend and that may go on for a while. Okay, now you know how you can use the RVI indicator to create an MQL 5 Expert Advisor that will tell you if you are in an upwards or in a downwards trend,
    7 min
  • MQL5 Tutorial 2017 Step 1 – Download the powerful MQL5 trading environment
    Okay before we can start we need the right piece of software. So let’s go to Google and look for MetaTrader5 download. Now you should find this link for MetaTrader5.com, and it’s the English version. From time to time the download link might change. So here we are we can download the Meta Trader 5 platform. It can be used for automated trading and here is a new big shiny thing, you now can use the hedging mode. One of the most annoying things when MQL 5 and MetaTrader 5 came out, was the fact that you could only hold one position for a currency pair. With the new hedging mode that has changed. And that makes the former messed up other handling system much better and they say they have a powerful algorithmic trading with the built-in MQL 5 development environment, and that MQL 5 language is really very powerful as you might see in the next few videos. And it’s also possible to check your trades on the iPhone or iPad or on your Android device. So you even can check if your automated trading system is trading exactly according to your rules, even when you’re not at home. So let’s click on that button here and download the MetaTrader5. In the next video we are going to install it.” splash=”https://d8wyob5mxqc1u.cloudfront.net/MQL5Tutorial/Download the MQL5 Trading Tools.png”] Okay before we can start we need the right piece of software. So let’s go to Google and look for MetaTrader5 download. Now you should find this link for MetaTrader5.com, and it’s the English version. From time to time the download link might change. So here we are we can download the Meta Trader 5 platform. It can be used for automated trading and here is a new big shiny thing, you now can use the hedging mode. One of the most annoying things when MQL 5 and MetaTrader 5 came out, was the fact that you could only hold one position for a currency pair. With the new hedging mode that has changed. And that makes the former messed up other handling system much better and they say they have a powerful algorithmic trading with the built-in MQL 5 development environment, and that MQL 5 language is really very powerful as you might see in the next few videos. And it’s also possible to check your trades on the iPhone or iPad or on your Android device. So you even can check if your automated trading system is trading exactly according to your rules, even when you’re not at home. So let’s click on that button here and download the MetaTrader5. In the next video we are going to install it.
    2 min
  • MQL5 Tutorial 2017 Step 5 – How to adjust MQL5 Charts
    Okay right now I don’t see anything on my chart and the reason is: I have to adjust some settings first. Because I changed my broker here, I will not see any price on this chart because it was automatically added by the MetaTrader set up. And here we have something called Market Watch, where we see several currency pairs. And if you right click here and click on show all, you will see even more. So let’s drag the first entry on the chart and this is how it looks like by default. You could now immediately open any trade by clicking on this button here but because we are going to trade automatically, we don’t need that. You can hide it by clicking this little button over here. This is a bar chart. You can zoom in by pressing the plus key on your keyboard, and you can zoom out by pressing the minus key or you could use one of these two buttons. Some traders might like bar charts. Because we are only looking at charts if we need to check something, line charts will be more than enough. I also don’t like the grid so I can right click on the chart, select properties or hit the F8 key and disable the show grid option here. You can also change the colors of your chart if you like, and if your chart doesn’t move you might want to remember that there is a chart auto scroll option here that can be enabled. So now I’m satisfied with my settings and I click on the okay button. And this is the simple kind of view I really like, so I would like to have this as the default template. Now I click with the right mouse button, select templates, and save the template. And here is a little trick, I use the name default because this is the template that is going to be loaded by MetaTrader5 each time I open a new chart. So let’s do that and here it is. But this is still a one hour chart so I once again right click on it, select time frames, one minute, right click, save template as default template. Yes I want to replace it. Now I open another chart and this one is right from the start of the one minute chart. And you can also save it as Tester.tpl if you want to have the same kind of chart for your back testing. So in the next step we are going to check out back testing with the strategy tester.
    3 min
  • MQL5 Tutorial 2017 Step 4 – How to adjust the MQL5 settings
    Okay by now we have downloaded MetaTrader5, we have done the set up and we have also created a demo account that supports hedging, so now it should be easy to connect Meta Trader 5 with our newly created demo account. So let’s see, MetaTrader 5 automatically scanned for servers and found this one. So if you click on continue, you can type in the account number: 2343699 and the password, and now you would click on finish. And if everything went well you should see that you are connected. Did you hear that annoying sound? The MetaTrader is going to spam you right from the start, so let’s turn off that annoying sound. In the first step I’m going to change the language to English and that will cause a restart. If you can’t get connected here, the reason might be that you have chosen the wrong server. In this case you see we have two different names here, so please double check the server name if you experience connection problems. We have to adjust a few settings before we can start to trade automatically so let’s click on Tools, Options and the very first thing I always disable is that annoying sound event. Unless you disable this option your PC will make sounds like a cartoon. On the tab for server, we disable the news because we don’t need them, and for the charts we only need five thousand bars. That’s more than enough for automated trading. The trade settings are also important because the last used volume here is much too high. I would change it to ten Micro-Lot. Hit the Expert Advisors tab and allow automated trading. I always remove the two marks here, because I don’t want my Expert Advisors to be disabled when I change anything. With the notification option you would be able to send push notifications to your mobile device. This works for iPhone and Android devices. You could also send email or upload your results to an FTP server. The options for community and signals can also be left as they are. When you are satisfied with all your settings please click on okay. Now this auto trading option should be enabled and in the next step we are going to set up our charts.
    3 min
  • MQL5 Tutorial 2017 Step 3 – How to create an independant MQL5 Demo Account
    Okay we have already downloaded and installed Meta Trader 5 and now we are going to create a demo account that we can connect to our trading environment. This is one of the brokers I like to use for demo accounts, because it does support Meta Trader 4 and Meta Trader 5, and I have created several trading systems in both languages. I have also already made deposits and withdrawals and so far I’m happy with the performance and with the fees. Another thing you need to consider is if your demo account is going to expire. I have had several demo accounts that expired within a few weeks, sometimes without any kind of warning and that is a real pain! So let’s open a new demo account, in my case the trading platform will be MetaTrader5, now I choose an account type and very important, you should absolutely make sure that you can use the hedge system. If your broker doesn’t support the hedge system for MetaTrader 5 and MQL5 you will only be able to open a single position for a currency pair. So make sure that you double check that it supports the hedge system, its brand new only a few weeks old. So I guess the majority of the MQL 5 brokers might not already support it. This is the leverage. I always choose the highest possible leverage basically the higher the leverage, the more money you can move with a smaller account. My start deposit will be one hundred thousand bucks. Now I will need to type in a password and an investor password. With the investor password you can have somebody look into your account and see all the trades you made, but you will not be able to trade anything in your account. So after everything is set and done, we will click on confirm. And here are our account settings. We have to save them immediately in a safe place to keep them secure. Now back in the MetaTrader we need to type in the broker name here and scan for the server, and there it is. In the next step we are going to connect the Meta Trader 5 environment with our newly created demo account.
    3 min
  • MQL5 2017 Tutorial Step 2 – Install MQL5 and Metatrader5
    Okay we have already found the download site for the Meta Trader 5, and now you should see this little mt5setup.exe file here, if you don’t please go to your download folder, right click on it and start it in administrative mode. Now you should see the screen with the terms and conditions. I will install the 64 bit version here. I have to agree to the license and I will change the settings. Now you can adjust your installation folder and I will remove this mark, because I don’t want my browser to pop up after the installation is finished. Let’s click on continue and MetaTrader5 is starting to download files, depending on your bandwidth that might take a little while. After all the files have been downloaded, please click on finish. And now nothing happens for about thirty seconds and suddenly you will see this window where you can get a free demo account for Meta Trader5 and because this is the default option I guess that most people will take this demo account here, but I’m a little bit suspicious. So I want to set up my own broker account. In the past I have made bad experiences with several brokers. So I would like to have somebody I trust, even for my demo account and you might prefer that too because once your demo account is profitable and successful, it will be a real pain to switch the broker and to start all the testing from the beginning. So in the next video we are going to set up an independent demo account and connect it to the MetaTrader5 environment.
    2 min
  • MQL5 Tutorial – Simple Adaptive Moving Average Robot
    Okay, this time we are talking about an Adaptive Moving Average Indicator. It was created (I think) by Perry Kaufman and you can use it to find entries or exits like you could with a simple moving average. Now if that is almost the same, why should we prefer to use an Adaptive Moving Average? Well Perry J. Kaufmann created this fascinating formula here for a reason. The Adaptive Moving Average has less lagging and generates less false signals. Let’s see if that is true. Now how can we create an Adaptive Moving Average Expert Advisor for Meta Trader 5? First please click on the little button here, that should bring up the MetaEditor. And here you want to select File, New, Expert Advisor from template, Continue and we will call that Simple Adaptive Moving Average. Now click on Continue, Continue, finish, and you can remove everything above the Ontick function here and I will also delete the two comment lines. So let’s create an array that will hold our prices, I will call it my price array and it is a double array because double and float variables can hold floating point types. And the adaptive moving average has six digits behind the dot and now we want to use the integrated MQL5 function iAMA for the current symbol on the chart and the current period, we want to calculate our prices based on the last nine candles. The Fast Moving Average has a value of two because if you Insert, Indicators, Trend, Adaptive Moving Average, you will see the values here, these are the nine candles. The fast EMA, that’s an Exponential Moving Average, is calculated for two candles and the slow EMA is calculated for thirty candles. And we do not need any shift, so we will use the exact same values here: nine candles, two candles for the fast EMA, thirty candles for the slow EMA. Zero for no shift and I would like to have the values calculated based on the close prices because the close price is also the default setting here. Okay let’s move on and sort the price array from the current candle downwards by using the array set as Series function for the price array we have created here. And now we want to fill it with data, that is done by using Copy Buffer for the Defined Moving Average, that’s the Adaptive Moving Average Definition we have created in this line. We just need one line, that’s the zero here, and we want to copy values from the current candle that’s candle zero for three candles that’s the three here, and we want to store the results in our price array. And now what we have done that, we want to get the value of the current candle, that’s candles zero of our price array and we use the normalized double function to get six digits behind the dot and the calculated value should be stored in a variable that is called Adaptive Moving Average Value. And the last step is to create a chart output by using the comment function. It will output the text AMA value and the calculated value for the Adaptive Moving Average Value we have extracted here. Okay that’s it, now please click the Compile bottom and you should get no errors and no warnings here so now you can click the button or hit the F4 key to go back to MetaTrader. And in MetaTrader you want to click on View, Strategy Tester, and in the Strategy Test panel you want to select the Simple Adaptive Moving Average.ex5 file. Select any currency pair you like and please make sure to enable the check mark for the visualisation option before you start your test. And here we go! if you move your mouse to the current value here, you’ll see that we have a value of 1.544031, that’s exactly what we have calculated here. And in your Expert Advisor you would now compare the value of the adaptive moving average to the current price. And if the price is above the moving average you would buy, and if the price is below the moving average you would sell. Okay now you can create an Expert Advisor that uses the Adaptive Moving Average Indicator to find out if you want to buy or if you want to sell and you ha...
    7 min